微信扫码
添加专属顾问
我要投稿
提示模板的类型
提示模板有助于将用户输入和参数转换为语言模型的指令。用于指导模型的响应,帮助其理解上下文并生成相关且连贯的基于语言的输出。
一般提示模板以字典作为输入,其中每个key代表提示模板中要填写的变量。下面介绍两种提示模板:
PromptTemplate
该模板的输出为一个字符串,也可以转换为字符串或消息列表。此方法的存在是为了便于在字符串和消息之间切换。
from langchain_core.prompts import PromptTemplateprompt_template = PromptTemplate.from_template("回答下面问题:{question}")pt = prompt_template.invoke({"question": "今天是几号?"})print(pt.to_string())# result# '回答下面问题:今天是几号?'
ChatPromptTemplate
这个提示模板用于格式化消息列表,由模板本身的列表组成。构建和使用 ChatPromptTemplate 的常见方法如下:
from langchain_core.prompts import ChatPromptTemplatechat_prompt_template = ChatPromptTemplate.from_messages([("system", "You are a helpful assistant"),("user", "回答下面问题:{question}")])cpt = chat_prompt_template.invoke({"question": "今天是几号?"})print(cpt.to_messages())# result# [SystemMessage(content='You are a helpful assistant'), HumanMessage(content='回答下面问题:今天是几号?')]
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholderfrom langchain_core.messages import HumanMessagechat_prompt_template = ChatPromptTemplate.from_messages([("system", "You are a helpful assistant"),MessagesPlaceholder("msgs")# ("placeholder", "{msgs}")])print(chat_prompt_template.invoke({"msgs": pt.to_messages()}))# result# messages=[SystemMessage(content='You are a helpful assistant'), HumanMessage(content='回答下面问题:今天是几号?')]
例子的结果可以看出,把上面PromptTemplate生成的消息也传进去了,这在消息传输中非常有用,以后构建ChatBot会用到。当然MessagesPlaceholder也可以不显示表示,使用("placeholder","{msgs}")占位。
在PromptTemplate中插入Few-Shot
from langchain_core.prompts import PromptTemplateexample_prompt = PromptTemplate.from_template("Question: {question}\nAnswer: {answer}")
下面给出few-shot的候选集:
examples = [{"question": "这组数中的奇数加起来是偶数:4、8、9、15、12、2、1。","answer": "将所有奇数相加(9、15、1)得到25。答案为False。"},{"question": "这组数中的奇数加起来是偶数:17、10、19、4、8、12、24。","answer": "将所有奇数相加(17、19)得到36。答案为True。"},{"question": "这组数中的奇数加起来是偶数:16、11、14、4、8、13、24。","answer": "将所有奇数相加(11、13)得到24。答案为True。"},{"question": "这组数中的奇数加起来是偶数:17、9、10、12、13、4、2。","answer": "将所有奇数相加(17、9、13)得到39。答案为False。"},{"question": "中国首都是哪里?","answer": "北京"}]
print(example_prompt.invoke(examples[0]).to_string())# resultQuestion: 这组数中的奇数加起来是偶数:4、8、9、15、12、2、1。Answer: 将所有奇数相加(9、15、1)得到25。答案为False。
上面打印出了第一个样例放入prompt后的输出。后面展示使用FewShotPromptTemplate方法拼接完整few-shot到模板中:
from langchain_core.prompts import FewShotPromptTemplateprompt = FewShotPromptTemplate(examples=examples,example_prompt=example_prompt,suffix="Question: {input}\nAnswer: ",input_variables=["input"],)print(prompt.invoke({"input": "这组数中的奇数加起来是偶数:15、32、5、13、82、7、1。"}).to_string())
结果如下:
Question: 这组数中的奇数加起来是偶数:4、8、9、15、12、2、1。Answer: 将所有奇数相加(9、15、1)得到25。答案为False。Question: 这组数中的奇数加起来是偶数:17、10、19、4、8、12、24。Answer: 将所有奇数相加(17、19)得到36。答案为True。Question: 这组数中的奇数加起来是偶数:16、11、14、4、8、13、24。Answer: 将所有奇数相加(11、13)得到24。答案为True。Question: 这组数中的奇数加起来是偶数:17、9、10、12、13、4、2。Answer: 将所有奇数相加(17、9、13)得到39。答案为False。Question: 中国首都是哪里?Answer: 北京Question: 这组数中的奇数加起来是偶数:15、32、5、13、82、7、1。Answer:
这样我们就把提供的例子都按格式拼接到Prompt中了;可以看出上面答案都用到了思维链的方式(Chain-of-Thought Prompting)这也是有用的prompt优化方案,目前大模型微调数据都采用了此方法。
添加更好的few-shot
from langchain_chroma import Chromafrom langchain_core.example_selectors import SemanticSimilarityExampleSelectorfrom langchain_openai import OpenAIEmbeddingsexample_selector = SemanticSimilarityExampleSelector.from_examples(# 候选示例集.examples,# 向量模型,用于计算相似度.OpenAIEmbeddings(),# 向量数据库,用于储存向量和检索.Chroma,# 检索k个示例.k=1,)# Select the most similar example to the input.question = "这组数中的奇数加起来是偶数:15、32、5、13、82、7、1。"selected_examples = example_selector.select_examples({"question": question})print(f"Examples most similar to the input: {question}")for example in selected_examples:print("\n")for k, v in example.items():print(f"{k}: {v}")
Examples most similar to the input: 这组数中的奇数加起来是偶数:15、32、5、13、82、7、1。answer:将所有奇数相加(11、13)得到24。答案为True。question: 这组数中的奇数加起来是偶数:16、11、14、4、8、13、24。
下面我来构建FewShotPrompt,prefix可以输入问题的描述,suffix可以输入问题的前缀(上篇文章有介绍,挺重要的。大模型Prompt提示设计简介(2):有效的建议)
prompt = FewShotPromptTemplate(example_selector=example_selector,example_prompt=example_prompt,suffix="\n待判断问题:\nQuestion: {input}\nAnswer: ",prefix="判断下面句子的对错,按下面例子的格式进行输出:\n例子:",input_variables=["input"],)print(prompt.invoke({"input": "这组数中的奇数加起来是偶数:15、32、5、13、82、7、1。"}).to_string())
判断下面句子的对错,按下面例子的格式进行输出:例子:Question: 这组数中的奇数加起来是偶数:16、11、14、4、8、13、24。Answer: 将所有奇数相加(11、13)得到24。答案为True。待判断问题:Question: 这组数中的奇数加起来是偶数:15、32、5、13、82、7、1。Answer:
这样我们很好的召回了相似的示例作为上下文,这样可以准确的约束模型进行推理,也能对输出的格式进行约束。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-12-08
让AI智能体拥有像人类的持久记忆:基于LangGraph的长短期记忆管理实践指南
2025-12-04
Agentic RAG这样用LangChain解决复杂问题
2025-12-01
Deep Agent 进化论:基于文件系统的 Context Engineering 深度解析
2025-11-27
langgraph 1.0.4 最新发布:功能优化与修复详解
2025-11-25
LangChain 最新agent框架deepagents测评:长任务友好,高可控
2025-11-25
被 LangChain 全家桶搞晕了?LangGraph、LangSmith、LangFlow 一文读懂
2025-11-21
如何用 LangGraph 构建高效的 Agentic 系统
2025-11-19
LangChain v1.0 模型选型:静态还是动态?一文看懂 Agent 的正确打开方式
2025-09-21
2025-11-03
2025-10-23
2025-10-19
2025-10-31
2025-11-06
2025-11-05
2025-09-19
2025-10-23
2025-11-01
2025-11-03
2025-10-29
2025-07-14
2025-07-13
2025-07-05
2025-06-26
2025-06-13
2025-05-21