【LangChain】Prompts之Prompt templates |
您所在的位置:网站首页 › prompt指令模板 › 【LangChain】Prompts之Prompt templates |
LangChain学习文档
【LangChain】向量存储(Vector stores)【LangChain】向量存储之FAISS【LangChain】Prompts之Prompt templates
Prompts
编程模型的新方法是通过提示(prompts)。 prompts是指模型的输入。该输入通常由多个组件构成。 LangChain 提供了多个类和函数,使构建和使用prompts变得容易。 Prompt templates(提示模板): 参数化模型输入Example selectors(选择器示例): 动态选择要包含在提示中的示例prompt 翻译:提示 Prompt templates语言模型将文本作为输入 - 该文本通常称为prompt。 通常,这不仅仅是一个硬编码字符串,而是模板、一些示例和用户输入的组合。 LangChain 提供了多个类和函数,使构建和使用prompts变得容易。 什么是提示模板?(What is a prompt template?)prompt template是指生成提示的可重复的方式。它包含一个文本字符串(“模板”),可以接收来自最终用户的一组参数并生成提示。 提示模板包含: 对语言模型的指令,一组几个镜头示例来帮助语言模型生成更好的响应,对语言模型的一个问题。这是最简单的例子: from langchain import PromptTemplate template = """\ 您是新公司的命名顾问。 生产{product}的公司起什么好名字? """ prompt = PromptTemplate.from_template(template) prompt.format(product="彩色袜子")结果: 您是新公司的命名顾问。 一家生产彩色袜子的公司起什么名字好呢? 创建提示模板(Create a prompt template)您可以使用 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="给我讲一个关于{content}的{adjective}笑话。" ) multiple_input_prompt.format(adjective="funny", content="chickens") # -> "给我讲一个关于鸡的有趣笑话。"如果您不想手动指定 input_variables,您还可以使用 from_template 类方法创建 PromptTemplate。 langchain 将根据传递的模板自动推断 input_variables。 template = "给我讲一个关于{content}的{adjective}笑话。" prompt_template = PromptTemplate.from_template(template) prompt_template.input_variables # -> ['adjective', 'content'] prompt_template.format(adjective="funny", content="chickens") # -> 给我讲一个关于鸡的有趣笑话。您可以创建自定义提示模板,以您想要的任何方式格式化提示。有关更多信息,请参阅自定义提示模板。 聊天提示模板(Chat prompt template)聊天模型将聊天消息列表作为输入 - 该列表通常称为提示(prompt)。这些聊天消息与原始字符串(您将传递到 LLM 模型中)不同,因为每条消息都与一个角色关联。 例如,在 OpenAI 的Chat Completion API中,聊天消息可以与 AI、人类或系统角色相关联。该模型会更紧密地遵循系统聊天消息的指令。 LangChain 提供了多种提示模板,可以轻松构建和使用提示。官方鼓励在查询聊天模型时使用这些聊天相关的提示模板而不是 PromptTemplate,以充分利用底层聊天模型的潜力。 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之后,您可以从一个或多个 MessagePromptTemplate 构建 ChatPromptTemplate。 我们可以使用 ChatPromptTemplate 的 format_prompt ——这会返回一个 PromptValue,您可以将其转换为字符串或 Message 对象,具体取决于您是否想要使用格式化值作为 llm 或聊天模型的输入。 chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) # 从格式化消息中获取聊天完成信息 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={})] 总结本篇主要讲述: 如何创建模板提示:方式一:PromptTemplate(input_variables=[], template="Tell me a joke.") 方式二:template = "Tell me a {adjective} joke about {content}." prompt_template = PromptTemplate.from_template(template),这种不用写input_variables。 如何创建messageTemplate,我们常常需要与角色相关联:角色有: AI(AIMessagePromptTemplate)、人类(HumanMessagePromptTemplate)、系统(SystemMessagePromptTemplate)。最后利用ChatPromptTemplate.from_messages(xxx)方法,整合这些角色,就构造出了,聊天机器人。 总结https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/ |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |