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

如何创建模板提示prompt

定义模型

from langchain_ollama import ChatOllamallm =  ChatOllama(base_url="http://ip:11434",model="qwen2",temperature=0,tool_choice="auto"
)

什么是提示模板?

它的目的是根据不同的输入动态生成特定格式的文本,以便为大语言模型(如GPT)提供更清晰、结构化的指令或上下文。

提示模板的结构:

一个提示模板通常包含以下部分:

静态文本部分:不会随输入变化的文本。这部分通常是用于引导模型的固定指令。

动态占位符部分:用于插入不同输入数据的占位符,随实际输入变化。例如, {input_text} 或 {user_question} 就是占位符,稍后会被替换为实际输入。

from langchain import PromptTemplatetemplate = """\
您是新公司的命名顾问。
生产{product}的公司起什么好名字?
"""prompt = PromptTemplate.from_template(template)
input=prompt.format(product="无添加剂的饼干")
response = llm.invoke(input)
print(response.content)

创建提示模板

您可以使用 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}笑话。"
)
input=multiple_input_prompt.format(adjective="funny", content="chickens")
# -> "给我讲一个关于鸡的有趣笑话。"
response = llm.invoke(input)
print(response.content)

如果您不想手动指定 input_variables,您还可以使用 from_template 类方法创建 PromptTemplate。 langchain 将根据传递的模板自动推断 input_variables。

template = "给我讲一个关于{content}的{adjective}笑话。"prompt_template = PromptTemplate.from_template(template)
prompt_template.input_variables
# -> ['adjective', 'content']
input=prompt_template.format(adjective="funny", content="chickens")
# -> 给我讲一个关于鸡的有趣笑话。
response = llm.invoke(input)
print(response.content)

聊天提示模板(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
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])# 从格式化消息中获取聊天完成信息
prompt=chat_prompt.format_prompt(input_language="English", output_language="Chinese", text="I love programming.").to_messages()
response = llm.invoke(prompt)
print(response.content)

总结

  1. 如何创建模板提示:
    方式一:PromptTemplate(input_variables=[], template=“Tell me a joke.”),这种要写input_variables。
    方式二:template = “Tell me a {adjective} joke about {content}.” prompt_template = PromptTemplate.from_template(template),这种不用写input_variables。
  2. 如何创建messageTemplate,我们常常需要与角色相关联:
    角色有:
  • AI(AIMessagePromptTemplate)、
  • 人类(HumanMessagePromptTemplate)、
  • 系统(SystemMessagePromptTemplate)。
    最后利用ChatPromptTemplate.from_messages([xxx])方法,整合这些角色,就构造出了chat prompt。
http://www.lryc.cn/news/443345.html

相关文章:

  • C语言 | Leetcode C语言题解之第423题从英文中重建数字
  • Jboss CVE-2017-12149 靶场攻略
  • ROS2 中令人困惑的rclpy.shutdown()
  • PHP纯离线搭建(php 8.1.7)
  • 【iOS】push和pop、present和dismiss
  • 基于51单片机的两路电压检测(ADC0808)
  • JavaScript ---案例(统计字符出现次数)
  • 切换淘宝最新npm镜像源
  • mysql时间戳格式化yyyy-mm-dd
  • 网络丢包定位记录(二)
  • 深度学习自编码器 - 自编码器的应用篇
  • Python 小工具制作 系列文章 - 总目录
  • Codeforces Round 973 (Div. 2) - D题
  • threejs性能优化之gltf文件压缩threejs性能优化之glb文件压缩
  • 设计模式 享元模式(Flyweight Pattern)
  • Leetcode 3290. Maximum Multiplication Score
  • CefSharp_Vue交互(Element UI)_WinFormWeb应用(3)---通过页面锁屏和关机(含示例代码)
  • unity UnityWebRequest 的request.downloadHandler 空应用
  • 使用 UWA Gears 定位游戏内存问题
  • OpenRestry(一个Nginx集成工具)的安装与使用
  • linux操作系统的基本命令
  • 通过UV快速计算品牌独立站网络流量
  • 使用Kong开源API网关的保姆级教程
  • 浅谈Spring Cloud:认识微服务
  • mac命令行分卷压缩与合并
  • 在 Linux (aarch64) 编译 OpenJDK 8
  • 如何有效检测住宅IP真伪?
  • springboot acuturator
  • 什么是SaaS软件?有哪些常用的SaaS软件?
  • QT Layout布局,隐藏其中的某些部件后,不影响原来的布局