模型输入/输出
任何语言模型应用程序的核心要素都是......模型。LangChain 为您提供了与任何语言模型接口的构建模块。
提示: 模板化、动态选择和管理模型输入
语言模型: 通过通用接口调用语言模型
输出解析器: 从模型输出中提取信息

提示
对模型进行编程的新方法是通过提示。提示指的是对模型的输入。这种输入通常由多个组件构成。LangChain 提供了多个类和函数,使提示的构建和使用变得简单。
提示模板: 参数化模型输入
示例选择器: 动态选择要包含在提示中的示例
提示模板
语言模型将文本作为输入--这些文本通常被称为提示。通常,提示不是简单的硬编码字符串,而是模板、一些示例和用户输入的组合。LangChain 提供了多个类和函数,可以轻松构建和使用提示。
什么是提示模板?
提示模板指的是生成提示的可重现方式。它包含一个文本字符串("模板"),可接收终端用户的一组参数并生成提示。
提示模板可以包含
对语言模型的说明、
帮助语言模型生成更好回复的一些示例、
向语言模型提出的问题。
下面是一个最简单的例子:
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
# An example prompt with no input variables
no_input_prompt = PromptTemplate(input_variables=[], template="Tell me a joke.")
no_input_prompt.format()
# -> "Tell me a joke."
# An example prompt with one input variable
one_input_prompt = PromptTemplate(input_variables=["adjective"], template="Tell me a {adjective} joke.")
one_input_prompt.format(adjective="funny")
# -> "Tell me a funny joke."
# An example prompt with multiple input variables
multiple_input_prompt = PromptTemplate(
input_variables=["adjective", "content"],
template="Tell me a {adjective} joke about {content}."
)
multiple_input_prompt.format(adjective="funny", content="chickens")
# -> "Tell me a funny joke about chickens."
如果不想手动指定输入变量,也可以使用 from_template 类方法创建 PromptTemplate。
template = "Tell me a {adjective} joke about {content}."
prompt_template = PromptTemplate.from_template(template)
prompt_template.input_variables
# -> ['adjective', 'content']
prompt_template.format(adjective="funny", content="chickens")
# -> Tell me a funny joke about chickens.
您可以创建自定义提示模板,以您想要的任何方式格式化提示。更多信息,请参阅自定义提示模板。
聊天提示模板
聊天模型将聊天信息列表作为输入,该列表通常称为提示。这些聊天信息不同于原始字符串(您将把原始字符串传入 LLM 模型),因为每条信息都与一个角色相关联。
例如,在 OpenAI 聊天完成 API 中,聊天信息可以与人工智能、人类或系统角色相关联。该模型应该更紧密地遵循系统聊天信息的指令。
LangChain 提供了多个提示模板,可以轻松构建和使用提示。我们鼓励您在查询聊天模型时使用这些与聊天相关的提示模板,而不是 PromptTemplate,以充分挖掘底层聊天模型的潜力。
from langchain.prompts import (
ChatPromptTemplate,
PromptTemplate,
SystemMessagePromptTemplate,
AIMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from langchain.schema import (
AIMessage,
HumanMessage,
SystemMessage
)
要创建与角色相关联的消息模板,需要使用 MessagePromptTemplate。
为方便起见,模板上有一个 from_template 方法。如果您要使用这个模板,它会是这个样子:
template="You are a helpful assistant that translates {input_language} to {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="You are a helpful assistant that translates {input_language} to {output_language}.",
input_variables=["input_language", "output_language"],
)
system_message_prompt_2 = SystemMessagePromptTemplate(prompt=prompt)
assert system_message_prompt == system_message_prompt_2
然后,你可以从一个或多个 MessagePromptTemplate 创建一个 ChatPromptTemplate。您可以使用 ChatPromptTemplate 的 format_prompt -- 它会返回一个 PromptValue,您可以将其转换为字符串或 Message 对象,具体取决于您是否想将格式化后的值作为 llm 或聊天模型的输入。
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
# get a chat completion from the formatted messages
chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages()
[SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}),
HumanMessage(content='I love programming.', additional_kwargs={})]
Comments
Post a Comment