欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > 【LangChain入门 3 Prompts组件】聊天提示词模板 ChatPromptTemplate

【LangChain入门 3 Prompts组件】聊天提示词模板 ChatPromptTemplate

2025/3/28 9:45:26 来源:https://blog.csdn.net/hjxu2016/article/details/146372978  浏览:    关键词:【LangChain入门 3 Prompts组件】聊天提示词模板 ChatPromptTemplate

文章目录

  • 一、 聊天信息提示词模板
    • 1.1 使用关键字
    • 1.2 使用SystemMessage, HumanMessage, AIMessage来定义消息
    • 1.3 使用MessagesPlaceholder 在特定未知添加消息列表
  • 二、关键类介绍
    • 2.1 ChatPromptTemplate 类
      • 2.1.1 from_messages()
      • 2.1.2 format_messages()
      • 2.1.3 format_prompt()
    • 2.2 SystemMessage, HumanMessage,AIMessage类

一、 聊天信息提示词模板

聊天信息提示词模板(char prompt template)

聊天模型以聊天信息作为输入,这个聊天消息列表的内容也可以通过提示词模板进行管理。
这些聊天消息与原始字符不同,因为每个消息都与“角色role”关联。

列如,在OpenAI的Chat Completion API中,OpenAI的聊天模板,给不同的聊天信息定义了三种角色类型,分别是助手(Assisant)、人类(human)、或系统(System)角色:

  • 助手(Assisant)消息指当前消息是AI回答的内容
  • 人类(user)消息指的是你发给AI的内容
  • 系统(system)消息通常是用来给AI身份进行描述

1.1 使用关键字

以下是创建聊天信息模板的例子
这个例子是通过文本描述来定义系统、助手等,关键字必须
Use one of 'human', 'user', 'ai', 'assistant', or 'system'

from langchain_ollama import ChatOllama
from langchain_core.prompts import ChatPromptTemplate
llm = ChatOllama(model="deepseek-r1:7b", 
)
chat_prompt = ChatPromptTemplate.from_messages([("system", "你是西天取经的人工智能助手,你的名字叫齐天智能"),("human",  "你好"),("ai", "您好,我是西天取经的人工智能助手,请问有什么可以帮助您?"),("human", "{user_input}")
])message = chat_prompt.format(user_input="你叫什么")
response = llm.invoke(message)
print(llm.invoke(message).content)

1.2 使用SystemMessage, HumanMessage, AIMessage来定义消息

实际开发中,这个方式多一些,比较清晰

from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from langchain_core.prompts import HumanMessagePromptTemplate
chat_template = ChatPromptTemplate.from_messages([SystemMessage(content=("你是西天取经的人工智能助手,你的名字叫齐天智能")),HumanMessage(content=("你好")),AIMessage(content=("您好,我是西天取经的人工智能助手,请问有什么可以帮助您?")),HumanMessagePromptTemplate.from_template("{text}"),]
)message = chat_template.format_messages(text="你叫什么")
print(message)
print("----------------------")
print(llm.invoke(message).content)

1.3 使用MessagesPlaceholder 在特定未知添加消息列表

这个提示词模板负责在特定位置添加消息列表。
在前面两段中,我们看到了如何格式化两条消息,每条消息都是一个字符串,但是我们希望用户传入一个消息列表,我们将其插入到特定位置,该怎么办?
这里可以使用MessagesPlaceholder的方式

如下代码,这将会生成两条消息,第一条是系统消息,第二条是我们传入的HumanMessage。 如果我们传入了5条消息,那么总共会生成6条消息(系统消息加上传入的5条消息)、这对于将一系列消息插入到特定位置非常有用。

from langchain_core.messages import SystemMessage, HumanMessage
from langchain_core.prompts import MessagesPlaceholder, MessagesPlaceholder
prompt_template = ChatPromptTemplate.from_messages([SystemMessage(content=("你是西天取经的人工智能助手,你的名字叫齐天智能")),# 你可以传入一组消息MessagesPlaceholder("msgs"),HumanMessagePromptTemplate.from_template("{text}"),]
)message = prompt_template.invoke({"msgs":[HumanMessage(content="你好"),AIMessage(content="您好,我是西天取经的人工智能助手,请问有什么可以帮助您?")],"text": "你叫什么"})# print("----------------")
print(llm.invoke(message).content )

二、关键类介绍

2.1 ChatPromptTemplate 类

LangChain 中用于构建聊天模型提示的类,它允许用户通过定义一系列消息模板来生成对话内容。
主要用于:创建聊天模型的输入提示,这些提示由多个消息组成,每个消息都有一个角色(如系统、用户或 AI)。它支持动态填充变量,能够根据输入参数生成具体的聊天消息列表。

2.1.1 from_messages()

从消息列表创建 ChatPromptTemplate 实例

  • 输入参数为messages: 一个包含消息模板的列表。每个消息模板可以是一个元组(角色,模板字符串),也可以是一个 MessagePromptTemplate 的实例。

2.1.2 format_messages()

根据输入参数格式化消息模板,生成具体的聊天消息列表。

  • 输入参数为一个字典,包含模板中需要填充的变量及其值。
  • 返回值:一个包含具体消息的列表,每个消息都是 SystemMessageHumanMessageAIMessage 的实例。

2.1.3 format_prompt()

格式化提示模板,返回一个 PromptValue 对象,可以转换为字符串或消息列表。
**参数:**一个字典,包含模板中需要填充的变量及其值。

prompt_value = chat_template.format_prompt(name="Bob", user_input="What is your name?")
print(prompt_value.to_messages())

2.2 SystemMessage, HumanMessage,AIMessage类

AIMessage类 为例:
AIMessage 是 LangChain 中的一种消息类型,表示由 AI 模型生成的消息。一般LLM的回答,都是AIMessage

记一下以下几个参数

  • content:表示消息的内容,通常是字符串形式的文本。
AIMessage(content="Hello, how can I help you today?")
  • role: 指定消息的角色。在 AIMessage 中,role 通常固定为 “assistant”,表示消息是由 AI 助手生成的。
AIMessage(content="Here is the answer.", role="assistant")
  • additional_kwargs:Dict格式,用于存储额外的关键字参数,例如工具调用信息。可以用于扩展消息的功能,例如存储工具调用信息、元数据等。
AIMessage(content="I need to call the weather API.",additional_kwargs={"tool_calls": [{"type": "weather_api", "args": {"location": "Beijing"}}]}
)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词