提示词模板

提示词模板

2026年9月21日·#编程学习/langchain学习笔记LangChain/AI·4724 字 24 分钟
浏览量加载中...
AI 摘要

PromptTemplate 与 ChatPromptTemplate 的用法与取舍,以及从"字符串提示词"到"消息列表"的机制演进

为什么要用提示词模板#

构造提示词有两种方式:Python 字符串拼接(f-string、format()、+)和 LangChain 的 PromptTemplate / ChatPromptTemplate

# 方式一:字符串拼接
topic = "Python"
difficulty = "初学者"
prompt_str = f"你是一个{difficulty}级别的编程导师。请用简单易懂的语言解释{topic}。"
response = model.invoke(prompt_str)
字符串拼接提示词模板
上手成本简单直接,零学习成本有一点学习成本,初期写法略复杂
可读性变量一多就混乱结构清晰(变量占位)
可维护性修改容易出错易维护、可复用
变量校验❌ 无(容易漏/拼错)✅ 自动校验(更安全)
复杂场景❌ 多轮对话 / RAG / Few-shot 难做✅ 支持对话 / RAG / Agent
生态集成与 LangChain 生态无缝集成,便于调试与日志追踪
Tip

开发建议:小项目 / 临时 demo 用字符串拼接;正式开发做 AI 应用,提示词模板是必选

PromptTemplate:生成字符串#

from langchain.prompts import PromptTemplate
topic = "Python"
difficulty = "初学者"
template = PromptTemplate.from_template(
"你是一个{difficulty}级别的编程导师。请用简单易懂的语言解释{topic}。"
)
# 用 format() 填变量,得到最终提示词字符串
prompt = template.format(difficulty=difficulty, topic=topic)
response = model.invoke(prompt)

ChatPromptTemplate:生成消息列表(1.x 首选)#

from langchain_core.prompts import ChatPromptTemplate
prompt_template = ChatPromptTemplate([
("system", "你是一个AI开发工程师. 你的名字是 {name}."),
("human", "{user_input}"),
])
# invoke() 返回的是【消息列表】(可直接传给模型)
prompt = prompt_template.invoke({"name": "小谷AI", "user_input": "你能帮我做什么?"})
print(prompt)

实例化有两种方式:from_messages()(推荐) 和 初始化方法传 messages 参数;传入的都是”元组构成的列表”,每个元组是 (role, content)

角色字符串对照#

角色字符串含义用途
"system"系统消息设定 AI 的行为、角色、规则
"user" / "human"用户消息用户的输入/问题
"assistant" / "ai"AI 消息AI 的回复(用于对话历史)
Note

PromptTemplateformat() 返回字符串ChatPromptTemplateinvoke() 返回消息列表——这个差别决定了它能直接喂给聊天模型,也因此成为 1.x 的主力工具。

模板调用的 3 种方式#

同一个模板,三个方法三副面孔,先记住返回类型,就知道该在什么场合用哪个

方式返回类型用途
invoke({...})ChatPromptValue最常用,里面装着 messages,可直接传给模型
format(...)str想看”拼出来长什么样”,或要落库/打日志
format_messages(...)list要一个真正的 List[BaseMessage](自己再拼装)
from langchain_core.prompts import ChatPromptTemplate
prompt_template = ChatPromptTemplate([
("system", "你是一个AI开发工程师. 你的名字是 {name}."),
("human", "你能开发哪些AI应用?"),
("ai", "我能开发很多AI应用, 比如聊天机器人, 图像识别, 自然语言处理等."),
("human", "{user_input}"),
])
# 方式1:invoke() → ChatPromptValue
prompt = prompt_template.invoke({"name": "小谷AI", "user_input": "你能帮我做什么?"})
print(type(prompt)) # <class 'langchain_core.prompt_values.ChatPromptValue'>
print(len(prompt.messages)) # 4
# 方式2:format() → 字符串
prompt_str = prompt_template.format(name="小谷AI", user_input="你能帮我做什么?")
print(type(prompt_str)) # <class 'str'>

format() 打印出来长这样——角色名 + 内容一行行拼好,就是个”给人看”的预览:

System: 你是一个AI开发工程师. 你的名字是 小谷AI.
Human: 你能开发哪些AI应用?
AI: 我能开发很多AI应用, 比如聊天机器人, 图像识别, 自然语言处理等.
Human: 你能帮我做什么?
# 方式3:format_messages() → 消息列表
messages = prompt_template.format_messages(name="小谷AI", user_input="你能帮我做什么?")
print(type(messages)) # <class 'list'>
Note

三种方式参数写法不一样invoke() 收一个字典(invoke({...})),format() / format_messages() 收关键字参数(format(name=..., user_input=...))。另外还有个 format_prompt(),返回的也是 ChatPromptValue——和 invoke() 等价,只是名字更”老派”。 实测(本机 langchain-core 1.2.18)invokeChatPromptValueformatstrformat_messageslistformat_promptChatPromptValue,与课程完全一致。

结合 LLM 调用#

模板 + 模型连起来的完整流程——“提供大模型 → 提供提示词 → 结合调用”三步

from dotenv import load_dotenv
from langchain_core.prompts import ChatPromptTemplate
import os
from langchain.chat_models import init_chat_model
###### 1、提供大模型 #########
load_dotenv(override=True)
model = init_chat_model(
model="gpt-5.4-mini",
model_provider="openai",
api_key=os.getenv("CLOSEAI_API_KEY"),
base_url=os.getenv("CLOSEAI_BASE_URL"),
)
###### 2、提供提示词 #########
chat_prompt = ChatPromptTemplate.from_messages([
("system", "你是一个数学家,你可以计算任何算式"),
("human", "{text}"),
])
# 输入提示
prompt_value = chat_prompt.invoke({
"text": "我今年18岁,我的舅舅今年38岁,我的爷爷今年72岁,我和舅舅一共多少岁了?"
})
###### 3、结合提示词,调用大模型 #########
# 得到模型的输出
output = model.invoke(prompt_value)
# 打印输出内容
print(output.content)

输出:

你今年 18 岁,舅舅今年 38 岁。
一共是:
18 + 38 = **56 岁**
所以,你和舅舅一共 **56 岁**。
Important

chat_prompt.invoke({...}) 的结果可以直接塞给 model.invoke(...)——因为前者给的就是一个消息列表。这就是”模板 ↔ 模型”之间最顺的接口:模板负责填变量,模型负责生成,中间不需要你做任何转换

更丰富的初始化参数类型#

前面一直用”元组构成的列表”,其实那只是六种合法元素里的第一种。看源码签名:

def __init__(
self,
messages: Sequence[
BaseMessagePromptTemplate | BaseMessage | BaseChatPromptTemplate
| tuple[str | type, str | list[dict] | list[object]] | str | dict[str, Any]
],
*,
template_format: Literal["f-string", "mustache", "jinja2"] = "f-string",
**kwargs: Any,
) -> None: ...

from_messages() 的签名一模一样(from_messages() 的底层也是调用类的 __init__() 方法)。结论:参数是列表类型,列表的元素可以是字符串、字典、元组、Message 对象、MessagePromptTemplate、ChatPromptTemplate

类型1:str 列表(不推荐)——因为默认角色全是 human

chat_template = ChatPromptTemplate.from_messages([
"Hello, {name}!" # 等价于 ("human", "Hello, {name}!")
])
print(chat_template.invoke({"name": "小谷AI"}))
# messages=[HumanMessage(content='Hello, 小谷AI!', ...)]

类型2:tuple 列表——最常用((role, content)):

prompt = ChatPromptTemplate.from_messages([
("system", "你的名字是{role}."),
("human", "很高兴认识你"),
])
print(prompt.invoke({"role": "小智"}))

类型3:dict 列表——和 JSON 消息格式一致,适合从配置/文件里读模板:

prompt = ChatPromptTemplate.from_messages([
{"role": "system", "content": "你的名字是{role}."},
{"role": "human", "content": "很高兴认识你"},
])
print(prompt.invoke({"role": "小智"}))

类型4:Message 列表——已经实例化好的 SystemMessage / HumanMessage 直接放进去(等于”写死的消息”):

from langchain_core.messages import SystemMessage, HumanMessage
chat_prompt_template = ChatPromptTemplate.from_messages([
SystemMessage(content="我是一个贴心的智能助手"),
HumanMessage(content="我的问题是:人工智能英文怎么说?"),
])
messages = chat_prompt_template.invoke({})
print(messages)
Warning

XxxMessage 里不能有占位符!写 HumanMessage(content="我的问题是:{word}英文怎么说?")invoke({"word": "人工智能"}),结果是原样透传

messages=[..., HumanMessage(content='我的问题是:{word}英文怎么说?', ...)]

{word} 不会被替换、也不会报错——就这么静静地错下去了。要填变量就用元组 / PromptTemplate / 下面的 MessagePromptTemplate

类型5:MessagePromptTemplate 列表——SystemMessagePromptTemplateHumanMessagePromptTemplateAIMessagePromptTemplate 分别用来”生成”对应的消息:

  • 模板化:支持变量占位符,运行时填充
  • 格式化:能把模板 + 输入变量合成最终消息
  • 输出类型:生成对应角色对象(HumanMessagePromptTemplateHumanMessagecontent + role="human"
  • 设计目的:简化模板化构造,避免重复定义角色
from langchain_core.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
)
system_message_prompt = SystemMessagePromptTemplate.from_template("你是一个{role}")
human_message_prompt = HumanMessagePromptTemplate.from_template("给我解释{concept},用浅显易懂的语言")
chat_prompt = ChatPromptTemplate.from_messages([
system_message_prompt,
human_message_prompt,
])
formatted_messages = chat_prompt.invoke({"role": "物理学家", "concept": "相对论"})
print(formatted_messages)
# messages=[SystemMessage(content='你是一个物理学家', ...), HumanMessage(content='给我解释相对论,用浅显易懂的语言', ...)]

类型6:嵌套的 ChatPromptTemplate——可以理解为”ChatPromptTemplate 里嵌套了 ChatPromptTemplate”:

nested_prompt_template1 = ChatPromptTemplate.from_messages([
("system", "我是一个人工智能助手,我的名字叫{name}")
])
nested_prompt_template2 = ChatPromptTemplate.from_messages([
("human", "很高兴认识你,我的问题是{question}")
])
prompt_template = ChatPromptTemplate.from_messages([
nested_prompt_template1, nested_prompt_template2,
])
prompt_template.invoke({"name": "小智", "question": "你为什么这么帅?"})

两种实例模板的变量会被合并到同一次 invoke 里(上面 namequestion 就是分别填进两个嵌套模板的)。

综合使用——六种元素可以混着放:

from langchain_core.prompts import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from langchain_core.messages import SystemMessage, HumanMessage
# 示例 1: 使用 BaseMessage(已实例化的消息)
system_msg = SystemMessage(content="你是一个AI工程师。")
human_msg = HumanMessage(content="你好!")
# 示例 2: 使用 BaseMessagePromptTemplate
system_prompt = SystemMessagePromptTemplate.from_template("你是一个{role}.")
human_prompt = HumanMessagePromptTemplate.from_template("{user_input}")
# 示例 3: 使用 BaseChatPromptTemplate(嵌套的 ChatPromptTemplate)
nested_prompt = ChatPromptTemplate.from_messages([("system", "嵌套提示词")])
prompt = ChatPromptTemplate.from_messages([
system_msg, # MessageLike (BaseMessage)
human_msg, # MessageLike (BaseMessage)
system_prompt, # MessageLike (BaseMessagePromptTemplate)
human_prompt, # MessageLike (BaseMessagePromptTemplate)
nested_prompt, # MessageLike (BaseChatPromptTemplate)
])
prompt.invoke({"role": "人工智能专家", "user_input": "介绍一下大模型的应用场景"})
Tip

template_format 还有三种取值"f-string"(默认)、"mustache""jinja2"。默认的 f-string 用 {变量};换 mustache 或 jinja2 时同样写 {{ name }} 也能正确渲染——处理”用户提供的模板”时,jinja2/mustache 语法更不容易和别的占位符冲突

ChatPromptTemplate.from_messages([("human", "Hello {{ name }}!")], template_format="mustache")
# messages=[HumanMessage(content='Hello 小谷!', ...)]

提示词机制演进:字符串 → 消息列表#

LangChain 1.0 的核心演进之一:一个结构化、富含元数据的消息列表,取代了单一字符串,成为与模型交互的标准数据格式

旧时代:LLM + PromptTemplate新时代:ChatModel + ChatPromptTemplate
模型接口LLM(文本进文本出)ChatModel(主流接口)
输入/输出都是纯文本字符串都是消息列表
角色支持❌ 无✅ system / user / assistant
对话历史❌ 不支持✅ 支持
适用场景简单提示聊天、对话、多轮交互

旧时代的痛苦:要用字符串模拟多轮聊天,开发者必须手动拼接、伪造对话角色

"Human:你好\nAI:你好!有什么我能帮忙的吗?\nHuman:..."

这样不仅结构混乱、难以维护,还极易让模型混淆对话的边界与上下文,影响生成质量。现代聊天模型 API 已原生支持角色概念,不再接受单一字符串,而是要求结构化的消息列表——这也为构建复杂、可靠的多轮对话智能体系统打下了基础。

高级特性#

模板会用了,接下来四个特性解决的是”真实项目里反复出现的四种麻烦”:变量老是重复填、历史消息要动态插、模板散落各处、提示词要拼装。

预填充变量:partial()#

痛点:某些变量在所有调用里都相同(比如”你是客服专员”),每次 invoke 都要重复写一遍。

做法:用 partial() 预填充固定变量,创建模板的变体。适合”部分变量固定 + 需要为不同用户/场景定制模板”。

from langchain_core.prompts import ChatPromptTemplate
# 原始模板
template = ChatPromptTemplate.from_messages([
("system", "你是{role},目标用户是{audience}"),
("user", "{task}"),
])
# 部分填充:把 role 和 audience 固定下来
customer_support_template = template.partial(
role="客服专员",
audience="普通用户",
)
# 现在只需要提供 task
messages = customer_support_template.invoke({"task": "解释退款政策"})
print(messages)
# messages=[SystemMessage(content='你是客服专员,目标用户是普通用户', ...),
# HumanMessage(content='解释退款政策', ...)]

典型场景:一套基础模板,派生出多个部门的变体——这就是”提示词的多态”:

base_template = ChatPromptTemplate.from_messages([
("system", "你是{department}{role}"),
("user", "{task}"),
])
# IT 部门
it_template = base_template.partial(department="IT 部门", role="技术支持")
# 销售部门
sales_template = base_template.partial(department="销售部门", role="销售顾问")
sales_template.invoke({"task": "为什么每年年底汽车会促销"})
# ChatPromptValue(messages=[SystemMessage(content='你是销售部门的销售顾问', ...),
# HumanMessage(content='为什么每年年底汽车会促销', ...)])
Tip

partial() 返回的是新模板,原模板不受影响——所以 base_template 还能继续派生出第三、第四个变体。被预填充的变量从”必填”变成”已填”,invoke 时再传也不会报错(以预填值为准)。

消息占位符:往指定位置塞一串消息#

痛点:多轮对话的历史、Agent 的中间步骤,都是”一段条数不定的消息”——它们的角色也不知道(human 还是 ai 混着来),没法用 ("human", "{x}") 这种写法。

做法:用消息占位符,在特定位置插入整个消息列表。多轮对话系统存历史、Agent 处理中间步骤时非常有用。

方式1:JSON 形式(("placeholder", "{变量}")

from langchain_core.prompts import ChatPromptTemplate
template = ChatPromptTemplate.from_messages([
("system", "你是一个有用的AI助手"),
("placeholder", "{conversation}"),
])
prompt_value = template.invoke({
"conversation": [
("human", "你好!"),
("ai", "今天我能帮你做什么?"),
("human", "你能给我做一个冰激凌吗?"),
("ai", "抱歉,我没有这样的能力"),
]
})
print(prompt_value)

填充进去的元组被逐个翻译成真正的消息对象("human", ...)HumanMessage("ai", ...)AIMessage):

messages=[SystemMessage(content='你是一个有用的AI助手', ...),
HumanMessage(content='你好!', ...),
AIMessage(content='今天我能帮你做什么?', ...),
HumanMessage(content='你能给我做一个冰激凌吗?', ...),
AIMessage(content='抱歉,我没有这样的能力', ...)]

方式2:MessagesPlaceholder 实例

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import HumanMessage
prompt_template = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant"),
MessagesPlaceholder("msgs"),
])
prompt_template.invoke({"msgs": [HumanMessage(content="hi!")]})
# 也可以用 format_messages:
# prompt_template.format_messages(msgs=[HumanMessage(content="hi!")])

生成的是”系统消息 + 你传进来的那些消息”:传 1 条就是 2 条,传 5 条就是 6 条——这正是”把一串消息插到指定位置”的价值。

Note

MessagesPlaceholder("msgs")位置参数就是变量名(等价于 MessagesPlaceholder(variable_name="msgs")),两种写法都可以。

实战:存对话历史——system 固定、history 动态、question 每次不同:

prompt_template = ChatPromptTemplate.from_messages([
("system", "你是一个非常友好的AI助手"),
MessagesPlaceholder(variable_name="history"),
("human", "{question}"),
])
prompt_template.invoke({
"history": [
("human", "5 + 2 = ?"),
("ai", "5 + 2 = 7"),
],
"question": "结果再乘以4呢?",
})
# ChatPromptValue(messages=[SystemMessage('你是一个非常友好的AI助手'),
# HumanMessage('5 + 2 = ?'), AIMessage('5 + 2 = 7'),
# HumanMessage('结果再乘以4呢?')])
Tip

这就是”手动维护对话历史”的正规写法:以前要在列表里手工拼接 {"role": ..., "content": ...},现在模板本身就留好了历史的坑位——每次只需把 history 换成最新的消息列表。第 7 篇最后那台聊天机器人,改用这个模板会清爽很多。

可复用模板库#

痛点:模板一多就散落在各个文件里。实际项目中建议创建模板库,统一收集、集中管理。

举例1:templates.py 用一个类当”模板仓库”

from langchain_core.prompts import ChatPromptTemplate
class PromptLibrary:
"""可复用的提示词模板库"""
TRANSLATOR = ChatPromptTemplate.from_messages([
("system", "你是专业翻译,精通{source_lang}{target_lang}"),
("user", "翻译以下文本:\n{text}"),
])
CODE_REVIEWER = ChatPromptTemplate.from_messages([
("system", "你是{language}代码审查专家,重点关注{focus}"),
("user", "审查代码:\n```{language}\n{code}\n```"),
])
SUMMARIZER = ChatPromptTemplate.from_messages([
("system", "你是内容摘要专家"),
("user", "将以下内容总结为{num}个要点:\n{content}"),
])
TUTOR = ChatPromptTemplate.from_messages([
("system", "你是{subject}导师,学生水平:{level}"),
("user", "{question}"),
])

其它文件里直接取用,一行调用、变量照填

from templates import PromptLibrary
messages = PromptLibrary.TRANSLATOR.format_messages(
source_lang="英语",
target_lang="中文",
text="Hello World",
)

举例2:按功能拆成包——模板多了以后,比”一个大类”更好维护:

templates/
# ├── __init__.py
# ├── common.py # 通用模板
# ├── translation.py # 翻译相关
# └── coding.py # 编程相关
common.py
from langchain_core.prompts import ChatPromptTemplate
FRIENDLY_ASSISTANT = ChatPromptTemplate.from_messages([
("system", "你是一个友好的助手"),
("user", "{input}"),
])

模板组合:拼出复杂提示词#

痛点:一个 system 提示词往往由”角色 + 风格 + 限制”几段拼成,想复用其中某一段。

方法1:字符串组合——在 Python 层面把片段先拼好,再交给模板:

# 定义可复用的部分
role_part = "你是一个{domain}专家。"
style_part = "回答风格:{style}。"
constraint_part = "限制:{constraint}。"
# 组合
full_system = role_part + style_part + constraint_part
template = ChatPromptTemplate.from_messages([
("system", full_system),
("user", "{question}"),
])

方法2:用 + 运算符——模板对象直接相加(LangChain 1.0 支持),合并后的模板包含两边所有消息:

template1 = ChatPromptTemplate.from_messages([("system", "你是助手")])
template2 = ChatPromptTemplate.from_messages([("user", "{input}")])
# 组合(LangChain 1.0 支持)
combined = template1 + template2
Note

两种方法的差别:字符串组合拼的是同一条消息的内容+ 拼的是消息与消息template1 的 system + template2 的 user)。 实测(本机 langchain-core 1.2.18)template1 + template2 返回的还是 ChatPromptTemplatecombined.invoke({"input": "hi"}) 得到 [SystemMessage('你是助手'), HumanMessage('hi')]——两边的变量会自动合并,一次 invoke 全填上。

相关#

练习题#

一、回忆填空(写完再展开对答案)#

  1. 构造提示词两种方式:Python ____ 拼接 与 LangChain 的 ____ 模板
  2. 字符串拼接的四个缺点:可读性差、不易维护、无 、难支持复杂场景(多轮/RAG/
  3. PromptTemplate.from_template(...) 之后用 ____() 填变量,返回 ____
  4. ChatPromptTemplate____() 填变量,返回 ____,可直接传给聊天模型
  5. ChatPromptTemplate 的两种实例化方式:____(推荐)和初始化方法传 messages 参数
  6. 角色字符串:"system" 系统消息、"user" 或 ____ 用户消息、"assistant" 或 ____ AI 消息
  7. 机制演进:旧时代是 LLM + PromptTemplate(____ 进字符串出);新时代是 ____ + ChatPromptTemplate(消息列表进消息列表出)
  8. 旧时代模拟多轮对话要在字符串里手动 ____ 对话角色,容易让模型混淆对话边界
填空答案(做完再点开)
  1. 字符串 / 提示词 2. 变量校验 / Few-shot 3. format / 字符串 4. invoke / 消息列表(List[BaseMessage]) 5. from_messages() 6. "human" / "ai" 7. 字符串 / ChatModel 8. 拼接、伪造

二、裸写题#

  • 2-1 两种方式写同一个提示词 用”字符串拼接”和”PromptTemplate”分别构造”你是一个{difficulty}级别的编程导师,请解释{topic}“,各调用一次模型,打印回复。

    提示(先自己想,实在想不出再点开)

    一级 · 思路:模板的价值在于”变量占位 + 自动校验”,两种方式结果应该一样 二级 · 方法PromptTemplate.from_template("...{topic}...") + .format(topic=..., difficulty=...) 三级 · 骨架prompt = template.format(topic="装饰器", difficulty="初学者")

  • 2-2 用 ChatPromptTemplate 构造多角色提示ChatPromptTemplate 构造”system 设定名字 + human 提问”两条消息,填变量后打印结果,并把 prompt 直接传给模型。

    提示

    一级 · 思路:模板返回的就是消息列表,可以原样喂给模型 二级 · 方法ChatPromptTemplate([("system", "...{name}"), ("human", "{user_input}")]) 三级 · 骨架prompt = prompt_template.invoke({"name": "小谷AI", "user_input": "..."}) 然后 model.invoke(prompt)

  • 2-3 把模板做成可复用的形态 把”编程导师”提示词封装成一个函数 build_prompt(topic, difficulty),循环给三个不同知识点生成提示词并调用模型。

    提示

    一级 · 思路:模板是”可复用的”,把变量做成函数参数最能体现这一点 二级 · 方法:函数内部 return template.format(...) 三级 · 骨架for topic in ["列表", "字典", "装饰器"]: ...

参考答案(做完再点开)
import os
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
from langchain.prompts import PromptTemplate
from langchain_core.prompts import ChatPromptTemplate
load_dotenv(override=True)
model = init_chat_model(
model="deepseek-v4-flash",
model_provider="openai",
base_url=os.getenv("DEEPSEEK_BASE_URL"),
api_key=os.getenv("DEEPSEEK_API_KEY"),
)
# 2-1 字符串拼接 vs PromptTemplate
topic, difficulty = "装饰器", "初学者"
r1 = model.invoke(f"你是一个{difficulty}级别的编程导师。请用简单易懂的语言解释{topic}。")
print("[拼接]", r1.content)
template = PromptTemplate.from_template(
"你是一个{difficulty}级别的编程导师。请用简单易懂的语言解释{topic}。"
)
r2 = model.invoke(template.format(difficulty=difficulty, topic=topic))
print("[模板]", r2.content)
# 2-2 ChatPromptTemplate 生成消息列表
chat_template = ChatPromptTemplate([
("system", "你是一个有帮助的AI机器人,你的名字是{name}。"),
("human", "{user_input}"),
])
prompt = chat_template.invoke({"name": "小谷AI", "user_input": "你能帮我做什么?"})
print("[消息列表]", prompt)
print("[模型回复]", model.invoke(prompt).content)
# 2-3 封装成函数复用
def build_prompt(topic: str, difficulty: str = "初学者") -> str:
return template.format(topic=topic, difficulty=difficulty)
for t in ["列表", "字典", "装饰器"]:
print(f"\n--- {t} ---")
print(model.invoke(build_prompt(t)).content)

评论区

[ 标签 ]
# AI37# AI 编程2# AI工具1# Ajax2# Apifox1# AstrBot3# Astro2# CC Switch1# CDN2# Claude Code1# claudecode2# ClaudeCode1# Cloudflare2# CloudFlare2# CloudFlare-ImgBed3# coc3# CSS6# DeepSeek6# deepseek2# DELETE1# Docker1# EdgeOne3# Gist1# git1# GitHub1# hexo-circle-of-friends1# HTML6# HTTP5# ImageManager1# Java23# java13# JavaScript5# JDBC3# JSON2# JUnit1# LangChain25# Logback1# Maven6# Muse Spark1# Mybatis1# MyBatis4# MySQL28# MySql1# NapCat1# Node.js1# obsidian2# Obsidian5# OpenCode4# ORM1# PathVariable1# PicGo1# PyCharm1# Python65# RequestBody1# RequestMapping1# RESTful风格1# skills1# Slf4j1# SpringBoot11# SQL2# Streamlit5# Svelte2# TailwindCSS1# Telegram3# Tlias2# Vercel1# vscode2# Vue7# Waline3# WebDAV1# Web基础6# Web开发6# WinSCP1# YAML1# 三层架构1# 中二宣言1# 书籍1# 使用文档10# 写作1# 函数2# 刷步数1# 前端32# 动态1# 动漫1# 包1# 单词2# 博客7# 博客工作流1# 博客开发2# 参数接收1# 友链1# 反思2# 图床6# 地图1# 备份2# 大模型1# 奇思妙想1# 存储1# 学习方法6# 学校1# 宝塔面板3# 宝宝10# 对象1# 导航栏1# 工具2# 开发1# 开发工具1# 开发规范1# 开心1# 异常处理1# 影视2# 微信1# 性能优化2# 总结1# 想法15# 感受1# 感悟11# 指南1# 提示词工程1# 插件5# 故障排除1# 效率工具2# 教程10# 数据分析9# 数据库27# 数据结构1# 文件操作2# 斩神1# 日常92# 日志框架1# 朋友圈1# 朱元璋1# 模块1# 模板1# 正则表达式2# 测试1# 游戏2# 爬虫7# 生活迁移1# 电影2# 电脑1# 碎碎念1# 视觉识别1# 类1# 类型注解1# 网络基础2# 网络教室1# 羊毛2# 脚本2# 脚本工具1# 自动化2# 蓝奏云1# 订阅推荐2# 记录2# 评论系统1# 词根1# 词缀1# 说说1# 足迹1# 跑步2# 路径参数1# 转载2# 运动1# 部落冲突1# 配置1# 随机图1# 面向对象5# 音乐3# 音标1# 饮食1# 驼峰命名1# 高德地图1
[ 公告 ]

如果你喜欢,那么欢迎来到我的世界!

了解更多
[ 音乐 ]
封面

音乐

暂未播放

0:000:00
暂无歌词
找不到相关结果。
[ contents ]
[ 全部文章 ]