当前位置: 首页 > news >正文

【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 PromptTemplatetemplate = """\
您是新公司的命名顾问。
生产{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

我们可以使用 ChatPromptTemplateformat_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={})]

总结

本篇主要讲述:

  1. 如何创建模板提示

方式一:PromptTemplate(input_variables=[], template="Tell me a joke.")

方式二:template = "Tell me a {adjective} joke about {content}." prompt_template = PromptTemplate.from_template(template),这种不用写input_variables

  1. 如何创建messageTemplate,我们常常需要与角色相关联:

角色有:

  • AI(AIMessagePromptTemplate)、
  • 人类(HumanMessagePromptTemplate)、
  • 系统(SystemMessagePromptTemplate)。

最后利用ChatPromptTemplate.from_messages(xxx)方法,整合这些角色,就构造出了,聊天机器人。

总结

https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/

http://www.lryc.cn/news/109963.html

相关文章:

  • 【数字IC基础】时序违例的修复
  • 深度学习实战46-基于CNN的遥感卫星地图智能分类,模型训练与预测
  • Node.js-fs模块文件创建、删除、重命名、文件内容的写入、读取以及文件夹的相关操作
  • LIN协议总结
  • Redis BigKey案例
  • ThinkPHP v6.0.8 CacheStore 反序列化漏洞
  • Spring 事务详解(注解方式)
  • 华为云waf 使用场景
  • ?.的写法 后缀修饰符
  • org.apache.hadoop.hive.ql.exec.DDLTask. show Locks LockManager not specified解决
  • Adaptive autosar 都有哪些模块?各有什么功能?
  • C++ 动态内存分配
  • 设计模式——面向对象的7大设计原则
  • 智慧防汛,数字科技的力量
  • “中国软件杯”飞桨赛道晋级决赛现场名单公布
  • JDBC处理批量数据提高效率
  • 使用css和js给按钮添加微交互的几种方式
  • react面试之context的value变化时,内部所有子组件是否变化
  • 使用okHttp不走代理问题
  • python moviepy 自动化音视频处理实践
  • 聊聊混合动力汽车和纯电骑车的优势和劣势
  • 算法训练Day39|62.不同路径 ● 63. 不同路径 II
  • react中hooks分享
  • LeetCode1207. 独一无二的出现次数
  • 【maven】构建项目前clean和不clean的区别
  • Stable Diffusion 硬核生存指南:WebUI 中的 CodeFormer
  • 从零开始理解Linux中断架构(24)软中断核心函数__do_softirq
  • 【云原生】Kubernetes中deployment是什么?
  • sk_buff操作函数学习
  • conda create时候出现JSONDecoderError解决方法