Langchain文档 提示模板

386 阅读3分钟

语言模型

语言模型将文本作为输入,这个文本通常被称为“提示”(prompt)。通常情况下,提示不是简单的硬编码字符串,而是由模板、示例和用户输入组成的组合。

LangChain 提供了多个类和函数,以便轻松构建和处理提示。什么是提示模板?

提示模板是指一种可复制的生成提示的方式。它包含一个文本字符串(“模板”),可以从最终用户那里接收一组参数并生成提示。

提示模板可以包含:

  • 对语言模型的指令
  • 一组 few-shot 示例,以帮助语言模型生成更好的回答
  • 对语言模型的问题

以下是一个简单的示例:

from langchain import PromptTemplate

template = """
/You are a naming consultant for new companies.
What is a good name for a company that makes {product}?
"""

prompt = PromptTemplate.from_template(template)
prompt.format(product="colorful socks")

输出:

You are a naming consultant for new companies.
What is a good name for a company that makes colorful socks?

创建提示模板

您可以使用 PromptTemplate 类创建简单的硬编码提示。提示模板可以接受任意数量的输入变量,并且可以进行格式化以生成提示。

from langchain import PromptTemplate

# 一个没有输入变量的示例提示
no_input_prompt = PromptTemplate(input_variables=[], template="告诉我一个笑话。")
no_input_prompt.format()
# -> "告诉我一个笑话。"

# 一个带有一个输入变量的示例提示
one_input_prompt = PromptTemplate(input_variables=["adjective"], template="告诉我一个{adjective}的笑话。")
one_input_prompt.format(adjective="好笑")
# -> "告诉我一个好笑的笑话。"

# 一个带有多个输入变量的示例提示
multiple_input_prompt = PromptTemplate(
    input_variables=["adjective", "content"],
    template="告诉我一个{adjective}关于{content}的笑话。"
)
multiple_input_prompt.format(adjective="好笑", content="鸡")
# -> "告诉我一个好笑关于鸡的笑话。"

如果不想手动指定输入变量,还可以使用 from_template 类方法创建 PromptTemplate。LangChain 将根据传入的模板自动推断输入变量。

template = "告诉我一个{adjective}关于{content}的笑话。"
prompt_template = PromptTemplate.from_template(template)
prompt_template.input_variables
# -> ['adjective', 'content']
prompt_template.format(adjective="好笑", content="鸡")
# -> 告诉我一个好笑关于鸡的笑话。

您可以创建自定义的提示模板,以任何您想要的方式格式化提示。有关更多信息,请参阅自定义提示模板。

聊天提示模板

聊天模型以一个聊天消息列表作为输入,这个列表通常被称为“提示”(prompt)。

与原始字符串不同的是,这些聊天消息有一个角色(role)。例如,在 OpenAI 的 Chat Completion API 中,聊天消息可以与 AI、人类或系统角色关联。模型应更加密切地遵循系统聊天消息中的指示。

LangChain 提供了几个提示模板,以便更轻松地构建和处理提示。建议在查询聊天模型时使用这些与聊天相关的提示模板,以充分发挥底层聊天模型的潜力。

from langchain.prompts import (
    ChatPromptTemplate,
    PromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
)
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage,
)

要创建与角色相关联的消息模板,您可以使用 MessagePromptTemplate。为方便起见,模板上公开了一个 from_template 方法。如果您要使用该模板,它看起来是这样的:

template="您是一个有帮助的助手,可以将 {input_language} 翻译成 {output_language}。"
system_message_prompt = SystemMessagePromptTemplate.from_template(template)

human_template="{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)

如果您希望直接构建 MessagePromptTemplate,您可以在外部创建一个 PromptTemplate,然后将其传递进去,例如:

prompt = PromptTemplate(
    template="您是一个有帮助的助手,可以将 {input_language} 翻译成 {output_language}。",
    input_variables=["input_language", "output_language"],
)
system_message_prompt_2 = SystemMessagePromptTemplate(prompt=prompt)
assert system_message_prompt == system_message_prompt_2

之后,您可以从一个或多个 MessagePromptTemplates 构建 ChatPromptTemplate。您可以使用 ChatPromptTemplate 的 format_prompt 方法,它返回一个 PromptValue,您可以将其转换为字符串或 Message 对象,具体取决于您是否希望将格式化后的值用作输入传递给 llm 或聊天模型。

chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

# 从格式化的消息中获取聊天完成
chat_prompt.format_prompt(input_language="英语", output_language="法语", text="我喜欢编程。").to_messages()
# -> [SystemMessage(content='您是一个有帮助的助手,可以将英语翻译成法语。', additional_kwargs={}), HumanMessage(content='我喜欢编程。', additional_kwargs={})]

Amazing Webpilot, telling friends!