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

wordpress 网站域名免费外贸电商平台

wordpress 网站域名,免费外贸电商平台,网站网站是怎么建设的,百度商业账号登录文章目录 一、 聊天信息提示词模板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(…

文章目录

  • 一、 聊天信息提示词模板
    • 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"}}]}
)
http://www.dtcms.com/wzjs/838756.html

相关文章:

  • 佛山网站建设服务器wordpress自定义链接不能用
  • 购物网站建设容易出现的问题建设网站需要申请
  • 提供龙岗网站建设免费建筑设计软件
  • 哪个网站有淘宝做图的素材贵州百度推广优化报告
  • 百度推广关键词价格查询网络seo招聘
  • 取名网站开发wordpress 积分系统
  • 网站营销单页怎么做吉安建设网站
  • 企业网站建设费用摊销中国交通建设集团
  • 电商网站 技术自助建站网站建设设计公司
  • 金属材料网站建设网页预览手机网站效果
  • 石景山网站建设公司排行建设 云服务器 网站
  • 酒店设计的网站建设wordpress 小工具 修改
  • 蛋糕网站建设淘宝上网站建设是什么
  • 优惠券网站要怎么做推广网页版梦幻西游是网易的吗
  • 营销型网站建设公司哪家好如何注册一个网站域名备案
  • 山西省建设执业资格注册中心网站手机设计
  • 铜陵建设行业培训学校网站熊猫办公ppt模板下载
  • 虚拟电子商务网站建设前期规划方案网页版游戏哪个好玩
  • 网站域名注册要多少钱wordpress手机版设置
  • 保定公司做网站wordpress 评分
  • 免费建站平台哪个稳定专题类响应式网站建设
  • 建筑网上招工平台哪个好网站百度推广和优化
  • 记录网站 自己做上海市建筑网
  • 青岛网站排名优化公司哪家好静安网站建设关键词优化seo
  • 网站建设的网站定位个人官网网站源码
  • 花都网站制作公司广东东莞职业技术学院
  • 免费建网站 建站之星全国企业工商信息查询官网
  • 佛山顺德网站设计公司江苏城乡建设职业学院网站
  • 做电影网站投资多少应用网站模板
  • 福建住房和城乡建设部网站首页做一个门户网站要多少钱