LangChain:模型I/O之提示词模板

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)
# 输出:
# 将以下英文翻译成中文:
#
# 输入:Hello
# 输出:你好
#
# 输入:Goodbye
# 输出:再见
#
# 输入:Thank you
# 输出:

聊天提示词模板

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)
# 输出:
# [
# SystemMessage(content='你是一个擅长机器学习的助手。'),
# HumanMessage(content='请解释什么是神经网络。')
# ]

自定义组合提示词模板

用途:通过管道(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)
# 输出:
# 你是一个翻译官。
#
# 示例:Hello -> 你好
#
# 问题:How are you?

总结

  • PromptTemplate:基础变量替换。
  • FewShotPromptTemplate:通过示例指导模型行为。
  • ChatPromptTemplate:生成多角色对话提示。
  • 自定义模板:灵活组合复杂逻辑。

实际使用时,可根据需求选择模板类型,并通过 input_variablesformat() 方法动态替换内容。对于高级场景,还可结合 ExampleSelector 动态选择示例,或通过自定义函数生成提示。

作者

光星

发布于

2025-04-10

更新于

2025-04-13

许可协议

评论