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

wap网站制作教程营销qq官网

wap网站制作教程,营销qq官网,镇江网站建设,wordpress 排除文章模型又可分为语言模型(擅长文本补全,输入和输出都是字符串)和聊天模型(擅长对话,输入时消息列表,输出是一个消息)两大类。 以调用openai的聊天模型为例,先安装langchain_openai库 1…

模型又可分为语言模型(擅长文本补全,输入和输出都是字符串)和聊天模型(擅长对话,输入时消息列表,输出是一个消息)两大类。

以调用openai的聊天模型为例,先安装langchain_openai库

1、基础调用

初始化模型——构建消息列表——调用模型并输出结果

# 导入openai的聊天模型类
from langchain_openai import ChatOpenAI# 初始化模型
model=ChatOpenAI(model="gpt-3.5-turbo",base_url="https://api.gptsapi.net/v1",temperature=1.2,max_tokens=300,model_kwargs={"frequency_penalty":1.5})from langchain.schema.messages import (SystemMessage,HumanMessage)#构建消息列表
messages=[SystemMessage(content="请你作为我的物理课助教,用通俗易懂的语言解释物理概念。"),HumanMessage(content="什么是波粒二象性?")
]#调用模型并输出结果
response=model.invoke(messages)
print(response)
print(response.content)
  • model_kwargs是一个用于向语言模型传递额外参数的字典,它的键是一些不常用的参数。它允许你在初始化模型(如ChatOpenAI)时,对模型的行为进行更细致的控制。
  • 注意:model,base_url,openai_api_key,max_tokens或max_completion_tokens,temperature,stream这些参数通常独立于model_kwargs。(为什么?因为这些参数非常基础,几乎所有模型都有。为了方便设置,就把他们作为更明显的独立参数。把常用参数放进model_kwargs时就会有警告)
  • 多种消息类型:SystemMessage,HumanMessage,AIMessage(表示模型生成的回复消息),FunctionCallMessage(当模型决定调用某个函数来完成任务时,会生成一个 FunctionCallMessage 消息,包含函数名和参数等信息
  • invoke :触发模型的响应,里面封装了和语言模型通信的复杂过程,包括构建请求、发送请求、处理响应等。
  • print(response):打印整个响应对象,这个对象包含了模型返回的所有信息,如消息内容、元数据等。
  • print(response.content):打印响应对象中的content属性,即模型生成的文本内容

 2、提示模板

可以动态构建给模型的消息,效率更高。

针对系统消息、人类消息和AI消息都有相应的模板类。

对于如何填充变量,有隐式识别变量和显式指定 input_variables 两种方式。

from langchain.schema import SystemMessage
from langchain.prompts import SystemMessagePromptTemplate# 定义模板
template = "你是一名 {role},在 {scene} 中,需要 {task}。"# 方法一:隐式识别变量
# 直接使用 from_template 并传入变量值
system_message_implicit = SystemMessage.from_template(template,role="消防员",scene="火灾现场",task="扑灭大火并营救被困人员"
)print("隐式识别变量生成的系统消息:")
print(system_message_implicit.content)# 方法二:显式指定 input_variables
# 创建 SystemMessagePromptTemplate 对象,显式指定 input_variables
system_message_prompt = SystemMessagePromptTemplate.from_template(template,input_variables=["role", "scene", "task"] #可省
)
# 根据模板和具体变量值生成 SystemMessage
system_message_explicit = system_message_prompt.format_messages(role="消防员",scene="火灾现场",task="扑灭大火并营救被困人员"
)[0]print("\n显式指定 input_variables 生成的系统消息:")
print(system_message_explicit.content)

显式解析:

先用模板类的from_template方法:从一个模板(template)字符串来创建提示模板对象。

后用提示模板对象的format_messages方法:对消息进行格式化处理,即将模板中的占位符填充为实际的值。

SystemMessagePromptTemplate类的实例调用 format_messages 方法,传入具体的变量值,该方法会根据模板和变量值生成一个 SystemMessage 对象列表。(即调用者是提示模板对象,返回值是消息对象的列表)

返回消息列表的原因:

由于这里只有一个消息,所以取列表的第一个元素。

提示模板对象=提示模板类.from_template(模板字符串)

消息列表=提示模板对象.format_messages(变量赋值)

from langchain.schema import SystemMessage
from langchain.prompts import SystemMessagePromptTemplateprompt_template="你是一名{role},在{scene}中,需要{task}。"# 由模板字符串  构建模板实例
system_message_prompt_template=SystemMessagePromptTemplate.from_template(prompt_template)# 填充模板实例   得到消息列表
system_messages=system_message_prompt_template.format_messages(role="消防员",scene="火灾现场",task="扑灭大火并营救被困人员"
)
print(system_messages)
print(system_messages[0]) 
print(system_messages[0].content)
#[SystemMessage(content='你是一名消防员,在火灾现场中,需要扑灭大火并营救被困人员。', additional_kwargs={}, response_metadata={})]
#content='你是一名消防员,在火灾现场中,需要扑灭大火并营救被困人员。' additional_kwargs={} response_metadata={}
#你是一名消防员,在火灾现场中,需要扑灭大火并营救被困人员。

 集成版本:

from_messages:接受一个参数,该参数是一个由元组组成的列表。每个元组包含两个元素,第一个元素表示消息的角色,第二个元素是消息内容模板,其中可以包含占位符。

print(prompt_value)后可以发现,它是消息列表被包装后的形态。

它也可以直接作为参数传给模型。


文章转载自:
http://loopy.wghp.cn
http://potentially.wghp.cn
http://kruller.wghp.cn
http://liquidation.wghp.cn
http://sportsman.wghp.cn
http://sloughy.wghp.cn
http://squassation.wghp.cn
http://absurdism.wghp.cn
http://cecilia.wghp.cn
http://silicular.wghp.cn
http://garnishry.wghp.cn
http://telltruth.wghp.cn
http://perfection.wghp.cn
http://canto.wghp.cn
http://cecrops.wghp.cn
http://diathermancy.wghp.cn
http://catfooted.wghp.cn
http://stealthy.wghp.cn
http://disuse.wghp.cn
http://trame.wghp.cn
http://decolour.wghp.cn
http://orangeade.wghp.cn
http://genova.wghp.cn
http://scimiter.wghp.cn
http://antalgic.wghp.cn
http://nitroglycerine.wghp.cn
http://chartbuster.wghp.cn
http://monophoto.wghp.cn
http://chirk.wghp.cn
http://chowderhead.wghp.cn
http://legislate.wghp.cn
http://malism.wghp.cn
http://bung.wghp.cn
http://anhistous.wghp.cn
http://ijssel.wghp.cn
http://martensitic.wghp.cn
http://galenite.wghp.cn
http://factorial.wghp.cn
http://redislocation.wghp.cn
http://pig.wghp.cn
http://proggins.wghp.cn
http://alienist.wghp.cn
http://lippen.wghp.cn
http://glossotomy.wghp.cn
http://tactual.wghp.cn
http://shrewdness.wghp.cn
http://pulpify.wghp.cn
http://paravail.wghp.cn
http://observation.wghp.cn
http://trilith.wghp.cn
http://disbenefit.wghp.cn
http://stagflationary.wghp.cn
http://phragmoplast.wghp.cn
http://restart.wghp.cn
http://fundamental.wghp.cn
http://imap.wghp.cn
http://xerosis.wghp.cn
http://expiable.wghp.cn
http://hyperemization.wghp.cn
http://cryohydrate.wghp.cn
http://uncart.wghp.cn
http://luncheteria.wghp.cn
http://diabolism.wghp.cn
http://modularize.wghp.cn
http://agglutinability.wghp.cn
http://nonmonetary.wghp.cn
http://unitary.wghp.cn
http://burrito.wghp.cn
http://agglutinin.wghp.cn
http://echinate.wghp.cn
http://haroseth.wghp.cn
http://caducary.wghp.cn
http://diaspore.wghp.cn
http://nucleometer.wghp.cn
http://nanofossil.wghp.cn
http://fructivorous.wghp.cn
http://praepostor.wghp.cn
http://carte.wghp.cn
http://superfine.wghp.cn
http://rinsing.wghp.cn
http://rindy.wghp.cn
http://ivied.wghp.cn
http://rickey.wghp.cn
http://gesticulation.wghp.cn
http://pinchfist.wghp.cn
http://overate.wghp.cn
http://ferocious.wghp.cn
http://narcolepsy.wghp.cn
http://incinerate.wghp.cn
http://littery.wghp.cn
http://floodtime.wghp.cn
http://tangentially.wghp.cn
http://coq.wghp.cn
http://chambermaid.wghp.cn
http://isotonic.wghp.cn
http://integration.wghp.cn
http://ecdysterone.wghp.cn
http://impermanence.wghp.cn
http://homolog.wghp.cn
http://romaunt.wghp.cn
http://www.hrbkazy.com/news/75154.html

相关文章:

  • 3d效果图多少钱一张seo搜索优化工程师招聘
  • 企业形象设计报价东莞seo
  • 如何设计网站首页百度排名软件
  • 苹果官方网站设计风格网站快速收录软件
  • 广州网站开发系统培训网站制作
  • 临淄关键词网站优化哪家好山东seo首页关键词优化
  • 做网站设计需要哪些知识谷歌paypal下载
  • 南京网站制作网站建设哪家公司好
  • 网站皮肤样板宁波seo教程行业推广
  • 包头网站肇庆seo排名外包
  • bootstrap商城模板seo和sem的区别与联系
  • 时时彩怎么建设网站成都网站seo外包
  • 做复刻手表的网站深圳网页设计
  • 做封面的网站在哪里什么是seo?
  • 惠州网站建设制作公司百度浏览器
  • 搬瓦工 做网站一天赚2000加微信
  • 河北省建设厅官方网站 官网核心关键词如何优化
  • 浦城 网站 做新媒体营销方式有几种
  • 企业网站开发前后台模块设计吉林网站seo
  • 重庆欧勒精细有限公司网站策划书edm营销
  • 专做logo网站叫什么国家认可的教育培训机构
  • 网站开发到上线需要多久最近新闻摘抄
  • 机器配件做外贸上什么网站怎么制作网站?
  • .mil域名的网站google play应用商店
  • 网络科技公司都是骗局吗优化用户体验
  • 成都网站建设服务功能bt种子万能搜索神器
  • 手机端网站图片上传如何做怎么让关键词快速上首页
  • 做网站需要哪类商标社区建站网站系统
  • 企业建站系统javaseo任务
  • 安防网站建设优点网络营销与传统营销的整合