LangChain 的提示词模板(Prompt Templates)是一组预定义或可自定义的模板,用于动态生成提示词(Prompts),帮助开发者更高效地与语言模型交互。
官网:LangChain #prompts
什么是提示词
提示词模板
基础提示词模板
PromptTemplate:基础提示模板
用途:根据变量动态生成提示词。
示例:生成包含用户输入变量的提示。
1 2 3 4 5 6 7 8 9 10 11 12 13
| from langchain.prompts import PromptTemplate
template = "请写一篇关于{theme}的简短文章。" prompt = PromptTemplate( input_variables=["theme"], template=template, )
formatted_prompt = prompt.format(theme="气候变化") print(formatted_prompt)
|
小样本提示词模板
FewShotPromptTemplate:小样本提示模板
用途:通过示例指导模型生成符合要求的回答。
示例:提供翻译示例,引导模型完成翻译任务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| from langchain.prompts import FewShotPromptTemplate, PromptTemplate
examples = [ {"input": "Hello", "output": "你好"}, {"input": "Goodbye", "output": "再见"}, ]
example_template = """ 输入:{input} 输出:{output} """ example_prompt = PromptTemplate( input_variables=["input", "output"], template=example_template, )
few_shot_prompt = FewShotPromptTemplate( examples=examples, example_prompt=example_prompt, prefix="将以下英文翻译成中文:", suffix="输入:{input}\n输出:", input_variables=["input"], )
formatted_prompt = few_shot_prompt.format(input="Thank you") print(formatted_prompt)
|
聊天提示词模板
ChatPromptTemplate:聊天提示模板
用途:为聊天模型(如 GPT-3.5/4)生成多角色对话格式的提示。
示例:模拟系统、用户和 AI 的对话。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| from langchain.prompts import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, )
system_template = "你是一个擅长{subject}的助手。" system_prompt = SystemMessagePromptTemplate.from_template(system_template)
human_template = "请解释什么是{concept}。" human_prompt = HumanMessagePromptTemplate.from_template(human_template)
chat_prompt = ChatPromptTemplate.from_messages([system_prompt, human_prompt])
formatted_prompt = chat_prompt.format_prompt( subject="机器学习", concept="神经网络" ).to_messages()
print(formatted_prompt)
|
自定义组合提示词模板
用途:通过管道(Pipeline)或自定义函数组合多个模板。
示例:将多个步骤合并为一个复杂提示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| from langchain.prompts import PipelinePromptTemplate
base_template = """{introduction}
{example}
{question}""" base_prompt = PromptTemplate.from_template(base_template)
introduction_template = "你是一个{role}。" introduction_prompt = PromptTemplate.from_template(introduction_template)
example_template = "示例:{example}" example_prompt = PromptTemplate.from_template(example_template)
question_template = "问题:{question}" question_prompt = PromptTemplate.from_template(question_template)
pipeline_prompt = PipelinePromptTemplate( final_prompt=base_prompt, pipeline_prompts=[ ("introduction", introduction_prompt), ("example", example_prompt), ("question", question_prompt), ], )
formatted_prompt = pipeline_prompt.format( role="翻译官", example="Hello -> 你好", question="How are you?" )
print(formatted_prompt)
|
总结
- PromptTemplate:基础变量替换。
- FewShotPromptTemplate:通过示例指导模型行为。
- ChatPromptTemplate:生成多角色对话提示。
- 自定义模板:灵活组合复杂逻辑。
实际使用时,可根据需求选择模板类型,并通过 input_variables
和 format()
方法动态替换内容。对于高级场景,还可结合 ExampleSelector
动态选择示例,或通过自定义函数生成提示。