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

如何创建模板提示prompt

定义模型

from langchain_ollama import ChatOllama

llm =  ChatOllama(
    base_url="http://ip:11434",
    model="qwen2",
    temperature=0,
    tool_choice="auto"
)


什么是提示模板?

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

提示模板的结构:

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

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

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

from langchain import PromptTemplate


template = """\
您是新公司的命名顾问。
生产{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.kler.cn/news/315638.html

相关文章:

  • Linux移植之系统烧写
  • Redis 中 String 字符串类型详解
  • 2024年中国研究生数学建模竞赛B题(华为题目)WLAN组网中网络吞吐量建模一
  • libtorch落地AI项目的一些总结
  • 面试题(八)
  • OpenCV_图像膨胀腐蚀与形态学操作及具体应用详解
  • 分布式安装LNMP
  • [OpenCV] 数字图像处理 C++ 学习——16直方图均衡化、直方图比较 详细讲解+附完整代码
  • 超详细的XML介绍【附带dom4j操作XML】
  • 口腔检测系统源码分享
  • php怎么连接使用kafka
  • 【AI算法岗面试八股面经【超全整理】——NLP】
  • 学生管理系统1.0版本
  • Kotlin 基本介绍(一)
  • 如何确保消息只被消费一次:Java实现详解
  • Python 中的 HTTP 编程入门,如何使用 Requests 请求网络
  • 实现人体模型可点击
  • Kotlin 枚举和 when 表达式(六)
  • 关于Python sklearn CountVectorizer使用详解
  • 多模态论文串讲-学习笔记(上)
  • docker配置镜像加速器
  • 亚马逊IP关联揭秘:发生ip关联如何处理
  • 【BEV 视图变换】Ray-based(2): 代码复现+画图解释 基于深度估计、bev_pool
  • MoveIt控制机械臂的运动实现——机器人抓取系统基础系列(二)
  • 带你0到1之QT编程:十七、Http协议实战,实现一个简单服务器和一个客户端进行http协议通信
  • 校园美食发现:Spring Boot技术的美食社交平台
  • Flyway 版本迁移文件
  • 【Kubernetes】常见面试题汇总(三十二)
  • Docker 系列完结
  • SparkSQL和Spark常用语句