Skip to content

提示模板Prompt Templates

提示模板(Prompt Templates)是LangChain中的一个核心概念,用于生成结构化的提示(prompts),以便与语言模型(如GPT-3、GPT-4等)进行交互。提示模板可以帮助开发者更高效地构建和管理复杂的提示,尤其是在需要动态生成内容或处理多轮对话时。

提示模板的核心功能

  1. 动态内容插入:提示模板允许你定义带有占位符的文本,这些占位符可以在运行时被具体的值替换。例如,你可以定义一个模板,其中包含{name},然后在运行时将{name}替换为具体的用户名。

  2. 结构化提示:通过提示模板,你可以将复杂的提示分解为多个部分,每个部分可以独立管理。例如,你可以将系统指令、用户输入和上下文信息分开定义,然后在需要时组合起来。

  3. 多轮对话管理:提示模板可以用于管理多轮对话中的上下文信息。你可以将历史对话记录插入到模板中,以便模型能够理解当前的对话状态。

提示模板的基本用法

在LangChain中,提示模板通常通过PromptTemplate类来创建和使用。以下是一个简单的示例:

python
from langchain.prompts import PromptTemplate

# 定义一个简单的提示模板
template = "你好,{name}!你今天感觉如何?"
prompt = PromptTemplate(input_variables=["name"], template=template)

# 使用模板生成提示
formatted_prompt = prompt.format(name="小明")
print(formatted_prompt)

输出:

你好,小明!你今天感觉如何?

提示模板的高级用法

  1. 多变量模板:你可以定义包含多个变量的模板,并在运行时为这些变量赋值。
python
template = "{greeting}{name}!今天是{day},你计划做什么?"
prompt = PromptTemplate(input_variables=["greeting", "name", "day"], template=template)

formatted_prompt = prompt.format(greeting="早上好", name="小红", day="星期一")
print(formatted_prompt)

输出:

早上好,小红!今天是星期一,你计划做什么?
  1. 条件提示:你可以根据不同的条件生成不同的提示。例如,根据用户的情绪生成不同的问候语。
python
template = "你好,{name}{emotion_message}"
prompt = PromptTemplate(input_variables=["name", "emotion_message"], template=template)

emotion = "开心"
if emotion == "开心":
    emotion_message = "看起来你今天心情不错!"
else:
    emotion_message = "希望你今天过得愉快!"

formatted_prompt = prompt.format(name="小刚", emotion_message=emotion_message)
print(formatted_prompt)

输出:

你好,小刚!看起来你今天心情不错!
  1. 多轮对话管理:你可以使用提示模板来管理多轮对话中的上下文信息。
python
from langchain.prompts import ChatPromptTemplate

# 定义系统指令和用户输入模板
system_template = "你是一个有帮助的助手。"
user_template = "用户说:{user_input}"

# 创建聊天提示模板
chat_prompt = ChatPromptTemplate.from_messages([
    ("system", system_template),
    ("user", user_template)
])

# 生成提示
formatted_prompt = chat_prompt.format(user_input="我想知道今天的天气。")
print(formatted_prompt)

输出:

你是一个有帮助的助手。
用户说:我想知道今天的天气。

Few-shot Prompt Template

这是一种通过提供少量示例(few-shot examples)来指导模型生成响应的方法

python
from langchain import PromptTemplate

# 示例数据
examples = [
    {"input": "巴黎", "output": "法国"},
    {"input": "东京", "output": "日本"}
]

# 创建一个模板
template = PromptTemplate(input_variables=["input"], template="城市: {input}\n国家: ")

# 构建 few-shot 提示
prompt = ""
for example in examples:
    # 根据模板格式化每个示例
    example_prompt = template.format(input=example["input"]) + example["output"] + "\n"
    prompt += example_prompt

# 添加最终需要模型回答的问题
final_question = "城市: 北京\n国家: "
prompt += final_question

print(prompt)

输出

城市: 巴黎
国家: 法国
城市: 东京
国家: 日本
城市: 北京
国家:

调用大模型

python
from langchain_community.llms.tongyi import Tongyi
model = Tongyi(model_name="qwen-turbo")
model.invoke(prompt)

输出

国家: 中国

提示模板的最佳实践

  1. 保持简洁:提示模板应尽量简洁明了,避免过于复杂的逻辑。复杂的逻辑可以通过代码来处理,而不是直接嵌入到模板中。

  2. 模块化设计:将提示模板分解为多个小的、可重用的部分,这样可以提高代码的可维护性和可扩展性。

  3. 动态调整:根据模型的表现和用户反馈,动态调整提示模板的内容和结构,以获得更好的交互效果。

  4. 测试和验证:在使用提示模板时,务必进行充分的测试,确保生成的提示能够正确地引导模型生成预期的输出。

提示模板是LangChain中一个非常强大的工具,能够帮助开发者更高效地管理和生成提示。通过合理地使用提示模板,你可以构建出更加灵活和智能的对话系统。