微信扫码
添加专属顾问
我要投稿
提示模板的类型
提示模板有助于将用户输入和参数转换为语言模型的指令。用于指导模型的响应,帮助其理解上下文并生成相关且连贯的基于语言的输出。
一般提示模板以字典作为输入,其中每个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-10-29
为什么我们选择 LangGraph 作为智能体系统的技术底座?
2025-10-27
Langchain 、 Manus 组了一个研讨会:Agent越智能,死得越快!
2025-10-23
LangChain V1.0 深度解析:手把手带你跑通全新智能体架构
2025-10-23
LangChain 与 LangGraph 双双发布 1.0:AI 智能体框架迎来里程碑时刻!
2025-10-19
AI 不再“乱跑”:LangChain × LangGraph 打造可控多阶段智能流程
2025-10-15
LangChain对话Manus创始人:顶级AI智能体上下文工程的“满分作业”首次公开
2025-10-09
Langchain回应OpenAI:为什么我们不做拖拉拽工作流
2025-09-21
告别无效检索:我用LangExtract + Milvus升级 RAG 管道的实战复盘
2025-09-13
2025-09-21
2025-10-19
2025-08-19
2025-08-17
2025-09-19
2025-09-12
2025-09-06
2025-08-03
2025-08-29
2025-10-29
2025-07-14
2025-07-13
2025-07-05
2025-06-26
2025-06-13
2025-05-21
2025-05-19