快速参考
提示模板(Prompt Template)是为语言模型生成提示的预定义配方。
模板可以包含指令、少样本示例,以及适合特定任务的特定上下文和问题。
LangChain 提供了创建和操作提示模板的工具。
LangChain 力求创建与模型无关的模板,以便轻松地在不同语言模型之间复用现有模板。
通常,语言模型期望提示要么是一个字符串,要么是一个聊天消息列表。
PromptTemplate
使用 PromptTemplate 为字符串提示创建模板。
默认情况下,PromptTemplate 使用 Python 的 str.format (opens in a new tab) 语法进行模板化。
from langchain_core.prompts import PromptTemplate
prompt_template = PromptTemplate.from_template(
"Tell me a {adjective} joke about {content}."
)
prompt_template.format(adjective="funny", content="chickens")'Tell me a funny joke about chickens.'该模板支持任意数量的变量,包括零个变量:
from langchain_core.prompts import PromptTemplate
prompt_template = PromptTemplate.from_template("Tell me a joke")
prompt_template.format()'Tell me a joke'你可以创建自定义提示模板,以任何你想要的方式格式化提示。更多信息见 提示模板组合。
ChatPromptTemplate
每条聊天消息都与内容以及一个称为 role(角色)的额外参数相关联。例如,在 OpenAI 的 Chat Completions API (opens in a new tab) 中,一条聊天消息可以关联 AI 助手、人类或系统角色。
像这样创建聊天提示模板:
from langchain_core.prompts import ChatPromptTemplate
chat_template = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful AI bot. Your name is {name}."),
("human", "Hello, how are you doing?"),
("ai", "I'm doing well, thanks!"),
("human", "{user_input}"),
]
)
messages = chat_template.format_messages(name="Bob", user_input="What is your name?")把这些格式化后的消息传入 LangChain 的 ChatOpenAI 聊天模型类,大致相当于直接使用 OpenAI 客户端做以下操作:
%pip install openaifrom openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful AI bot. Your name is Bob."},
{"role": "user", "content": "Hello, how are you doing?"},
{"role": "assistant", "content": "I'm doing well, thanks!"},
{"role": "user", "content": "What is your name?"},
],
)ChatPromptTemplate.from_messages 静态方法接受多种消息表示形式,是一种便捷的方式,用你想要的精确消息来格式化聊天模型的输入。
例如,除了上面使用的 (type, content) 二元组表示外,你还可以传入 MessagePromptTemplate 或 BaseMessage 的实例。
from langchain_core.messages import SystemMessage
from langchain_core.prompts import HumanMessagePromptTemplate
chat_template = ChatPromptTemplate.from_messages(
[
SystemMessage(
content=(
"You are a helpful assistant that re-writes the user's text to "
"sound more upbeat."
)
),
HumanMessagePromptTemplate.from_template("{text}"),
]
)
messages = chat_template.format_messages(text="I don't like eating tasty things")
print(messages)[SystemMessage(content="You are a helpful assistant that re-writes the user's text to sound more upbeat."), HumanMessage(content="I don't like eating tasty things")]这让你在构造聊天提示时拥有很大的灵活性。
消息提示(Message Prompts)
LangChain 提供不同类型的 MessagePromptTemplate。最常用的有 AIMessagePromptTemplate、SystemMessagePromptTemplate 和 HumanMessagePromptTemplate,它们分别创建 AI 消息、系统消息和人类消息。你可以在 此处了解不同类型的消息。
在聊天模型支持接受任意角色聊天消息的情况下,你可以使用 ChatMessagePromptTemplate,它允许用户指定角色名称。
from langchain_core.prompts import ChatMessagePromptTemplate
prompt = "May the {subject} be with you"
chat_message_prompt = ChatMessagePromptTemplate.from_template(
role="Jedi", template=prompt
)
chat_message_prompt.format(subject="force")ChatMessage(content='May the force be with you', role='Jedi')MessagesPlaceholder
LangChain 还提供 MessagesPlaceholder,它让你完全控制格式化期间要渲染哪些消息。当你不确定消息提示模板应该使用什么角色,或者希望在格式化期间插入一个消息列表时,这很有用。
from langchain_core.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
)
human_prompt = "Summarize our conversation so far in {word_count} words."
human_message_template = HumanMessagePromptTemplate.from_template(human_prompt)
chat_prompt = ChatPromptTemplate.from_messages(
[MessagesPlaceholder(variable_name="conversation"), human_message_template]
)from langchain_core.messages import AIMessage, HumanMessage
human_message = HumanMessage(content="What is the best way to learn programming?")
ai_message = AIMessage(
content="""\
1. Choose a programming language: Decide on a programming language that you want to learn.
2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.
3. Practice, practice, practice: The best way to learn programming is through hands-on experience\
"""
)
chat_prompt.format_prompt(
conversation=[human_message, ai_message], word_count="10"
).to_messages()[HumanMessage(content='What is the best way to learn programming?'),
AIMessage(content='1. Choose a programming language: Decide on a programming language that you want to learn.\n\n2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.\n\n3. Practice, practice, practice: The best way to learn programming is through hands-on experience'),
HumanMessage(content='Summarize our conversation so far in 10 words.')]消息提示模板类型的完整列表包括:
- AIMessagePromptTemplate (opens in a new tab),用于 AI 助手消息;
- SystemMessagePromptTemplate (opens in a new tab),用于系统消息;
- HumanMessagePromptTemplate (opens in a new tab),用于用户消息;
- ChatMessagePromptTemplate (opens in a new tab),用于任意角色的消息;
- MessagesPlaceholder (opens in a new tab),用于容纳一个消息列表。
LCEL
PromptTemplate 和 ChatPromptTemplate 实现了 Runnable 接口,它是 LangChain 表达式语言(LCEL) 的基本构建块。这意味着它们支持 invoke、ainvoke、stream、astream、batch、abatch、astream_log 调用。
PromptTemplate 接受一个(提示变量的)字典并返回 StringPromptValue。ChatPromptTemplate 接受一个字典并返回 ChatPromptValue。
prompt_template = PromptTemplate.from_template(
"Tell me a {adjective} joke about {content}."
)
prompt_val = prompt_template.invoke({"adjective": "funny", "content": "chickens"})
prompt_valStringPromptValue(text='Tell me a funny joke about chickens.')prompt_val.to_string()'Tell me a funny joke about chickens.'prompt_val.to_messages()[HumanMessage(content='Tell me a funny joke about chickens.')]chat_template = ChatPromptTemplate.from_messages(
[
SystemMessage(
content=(
"You are a helpful assistant that re-writes the user's text to "
"sound more upbeat."
)
),
HumanMessagePromptTemplate.from_template("{text}"),
]
)
chat_val = chat_template.invoke({"text": "i dont like eating tasty things."})chat_val.to_messages()[SystemMessage(content="You are a helpful assistant that re-writes the user's text to sound more upbeat."),
HumanMessage(content='i dont like eating tasty things.')]chat_val.to_string()"System: You are a helpful assistant that re-writes the user's text to sound more upbeat.\nHuman: i dont like eating tasty things."