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

免费素材库大全网站solidworks永久免费版

免费素材库大全网站,solidworks永久免费版,六安网络推广哪家好,湖北企业年报网上申报入口LangChain提供两种封装的模型接口 1.大规模语言模型(LLM):输入文本字符串,返回文本字符串 2.聊天模型:基于一个语言模型,输入聊天消息列表,返回聊天消息 Langchain的支持OpenAI、ChatGLM、Hu…

LangChain提供两种封装的模型接口

1.大规模语言模型(LLM):输入文本字符串,返回文本字符串

2.聊天模型:基于一个语言模型,输入聊天消息列表,返回聊天消息

Langchain的支持OpenAI、ChatGLM、HuggingFace等众多模型。本系列中,我们将以OpenAI为例,后续内容中提到的模型默认为OpenAI提供的模型。

LangChain的封装,如对OpenAI模型的封装,实际上是指对OpenAI API的封装

LLM

LLM 是一种基于统计的机器学习模型,用于对文本数据进行建模和生成。LLM学习和捕捉文本数据中的语言模式、语法规则和语义关系,以生成连贯并合乎语言规则的文本。

在Langchain的环境中,LLM特指文本补全模型(text completion model)。

文本补全模型是一种基于语言学习的机器学习模型,根据上下文语境和语言规律,自动推断最有可能的文本补全。

聊天模型 (Chat Models)

聊天模型是语言模型的一种变体。聊天模型使用语言模型,并提供基于"聊天消息"的接口。

LangChain与OpenAI模型 

参考OpenAI官方文档https://platform.openai.com/docs/models/model-endpoint-compatibility

gpt模型都归为了聊天模型,而davinci, curie, babbage, ada模型都归为了文本补全模型。

LangChain框架支持不同模型,为了方便切换模型,提供通用接口BaseLanguageModel,并提供predict和predict_messages函数调用。

当使用LLM模型时推荐使用predict函数,当使用聊天模型时推荐使用predict_messages函数

LLM交互

需要使用langchain.llms模块中的OpenAI类

# 从langchain的llms中导入OpenAI类
from langchain.llms import OpenAI# 设置环境变量OPEN_AI_KEY
import os
os.environ['OPENAI_API_KEY'] = '您的私有OPENAI_API_KEY'# OpenAI类实例化llm对象,模型选择text-davinci-003
llm = OpenAI(model_name = 'text-davinci-003')# llm对象调用predict方法和模型交互,传入字符“What Is AI”
response = llm.predict("What Is AI")
print(response)

您应该能看到如下输出:

AI (Artificial Intelligence) is a type of computer technology that enables a machine to simulate human intelligence and behavior. AI systems can be used to automate tasks, learn from data, and make decisions. Examples of AI include natural language processing (NLP), machine learning, computer vision, robotics, and expert systems.

聊天模型的交互

需要使用langchain.chat_models模块的ChatOpenAI类

# 从langchain.chat_models中导入ChatOpenAI类
from langchain.chat_models import ChatOpenAI
from langchain.schema import AIMessage,HumanMessage,SystemMessage# 设置环境变量OPEN_AI_KEY
import os
os.environ['OPENAI_API_KEY'] = '您的私有OPENAI_API_KEY'# ChatOpenAI类实例化chat对象,temperature参数设为0,模型默认为gpt-3.5
chat = ChatOpenAI(temperature = 0)# chat对象调用predict_messages方法和模型交互,传入字符“What Is AI”
response = chat.predict_messages([HumanMessage(content = "What Is AI")])
print(response)

您应该能看到如下输出:

content='AI, or Artificial Intelligence, refers to the simulation of human intelligence in machines that are programmed to think and learn like humans. It involves the development of computer systems capable of performing tasks that would typically require human intelligence, such as speech recognition, decision-making, problem-solving, and language translation. AI can be categorized into two types: Narrow AI, which is designed for specific tasks, and General AI, which possesses the ability to understand, learn, and apply knowledge across various domains. AI has applications in various fields, including healthcare, finance, transportation, and entertainment, and is continuously evolving and advancing.' additional_kwargs={} example=False

接下来我们使用SystemMessage指令,指定模型行为。

如下代码指定模型对AI一无所知,当提问到AI相关知识,会回答“I don’t know ”

# 从langchain.chat_models中导入ChatOpenAI类
from langchain.chat_models import ChatOpenAI
from langchain.schema import AIMessage,HumanMessage,SystemMessage# 设置环境变量OPEN_AI_KEY
import os
os.environ['OPENAI_API_KEY'] = '您的私有OPENAI_API_KEY'# ChatOpenAI类实例化chat对象,temperature参数设为0,模型默认为gpt-3.5
chat = ChatOpenAI(temperature = 0)# chat对象调用predict_messages方法和模型交互,传入字符“What Is AI”
response = chat.predict_messages([SystemMessage(content = "You are a chatbot that knows nothing about AI. When you are asked about AI, you must say 'I don\'t know'"),HumanMessage(content = "What Is AI")])
print(response)
content="I don't know." additional_kwargs={} example=False

三个消息类

langchain框架提供三个消息类,是AIMessage、HumanMessage、SystemMessage,分别对应OpenAI聊天模型API支持的三种角色assitant、user、system。请参考 ​​​​​​OpenAI API文档

总结:介绍了LLM模型与聊天模型,以及两者的区别。使用langchain框架实现了与OpenAI LLM和聊天模型的对话。 

http://www.yayakq.cn/news/715890/

相关文章:

  • 电脑技术学习网站优化设计七年级上册数学答案
  • 网站数据库模板快速搭建网站前端
  • 百宝图建设工程电子网站产品营销策划
  • 上海快速建站河北邢台做移动网站
  • 李鸿星电子商务网站建设做cpc不做网站可以吗
  • 开发网站怎样注册公司模板网站的优势有哪些
  • 网站怎么发布做微商个人备案 网站内容
  • 找合作项目app平台安卓手机优化神器
  • php完整网站开发源码服装公司网站多少钱
  • 潍坊高端模板建站素材搜集网站
  • 濮阳seo网站建设网站页面排名优化
  • 企业网站设计多少钱移动终端网站建设
  • .net 免备案网站空间南京产品网站建设收费
  • 网站建设项目招标文件制作凡客诚品帮助中心页面
  • 恒通建设集团有限公司网站广告设计公司资质
  • 清溪东莞网站建设单位网站建设存在问题情况汇报
  • 货运网站源码为什么大家用wordpress建网站
  • 烟台网站建设报价wordpress 查询文章
  • 对网站有效的优化软件天津网站建设58
  • 深圳网站制作搜行者seo旅游网站网页设计模板代码
  • 英文手机商城网站建设网站开发前端简历
  • 利用电脑做网站昆明网站制作报价
  • 网站seo诊断评分45承德 网站建设
  • 基本原理网站建设番禺网站设计公司
  • 响应式网站建设的好处iis网站目录权限设置
  • 教育网站建设的雷区网页登不了wordpress
  • 网站建设人员的岗位职责计算机毕设做网站
  • 提供微网站建设wikidot怎么建设网站
  • 上海建网站计划成立网站开发公司
  • 网站建设公司模版定制化网站建设