一架梯子,一头程序猿,仰望星空!
LangChain教程(Python版本) > 内容正文

LangChain Prompt templates(提示词模板)


Prompt templates(提示词模板)

语言模型以文本作为输入 - 这个文本通常被称为提示词(prompt)。在开发过程中,对于提示词通常不能直接硬编码,不利于提示词管理,而是通过提示词模板进行维护,类似开发过程中遇到的短信模板、邮件模板等等。

什么是提示词模板?

提示词模板本质上跟平时大家使用的邮件模板、短信模板没什么区别,就是一个字符串模板,模板可以包含一组模板参数,通过模板参数值可以替换模板对应的参数。

一个提示词模板可以包含下面内容:

  • 发给大语言模型(LLM)的指令。
  • 一组问答示例,以提醒AI以什么格式返回请求。
  • 发给语言模型的问题。

提示词模板例子:

from langchain import PromptTemplate

# 提示词模板,内嵌了一个变量product
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="鸡")
# -> "告诉我一个好笑的关于鸡的笑话。"

如果你不希望手动指定“input_variables”模板参数,你也可以使用“from_template”方法创建“PromptTemplate”提示词模板实例。 langchain根据传递的template参数自动推断“input_variables”对应的模板参数,参考下面例子。

template = "告诉我一个{adjective}的笑话,和{content}相关的"

# 通过from_template创建提示词模板
prompt_template = PromptTemplate.from_template(template)
# 打印模板参数,from_template会自动根据模板内容推断出具体关联的模板参数,不需要手动定义
prompt_template.input_variables
# -> ['adjective', 'content']
prompt_template.format(adjective="好笑的", content="鸡")
# -> 告诉我一个好笑的笑话,和鸡相关的。

聊天消息提示词模板

聊天模型(Chat Model)以聊天消息列表作为输入,这个聊天消息列表的消息内容也可以通过提示词模板进行管理。这些聊天消息与原始字符串不同,因为每个消息都与“角色(role)”相关联。

例如,在OpenAI的Chat Completion API中,聊天消息可以与AI、人类(human)或系统(system)角色相关联。Openai的聊天模型,给不同的聊天消息定义了三种角色类型分别是AI、人类(human)或系统(system)角色:

  • AI消息指的是当前消息是AI回答的内容
  • 人类(human)消息指的是你发给AI的内容
  • 系统(system)消息通常是用来给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。您可以使用ChatPromptTemplateformat_prompt —— 这将返回一个PromptValue,您可以将其转换为字符串或消息对象,具体取决于您是否想将格式化值用作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={})]


关联主题