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

02__models

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.lryc.cn/news/132222.html

相关文章:

  • MyBatis入门配置及CURD实现
  • 《游戏编程模式》学习笔记(五)原型模式 Prototype Pattern
  • ansible案列之LNMP分布式剧本
  • React2023电商项目实战 - 1.项目搭建
  • 数据库连接池(c3p0和德鲁伊)
  • ARM--day6(实现字符、字符串收发的代码和现象,分析RCC、GPIO、UART章节)
  • 2023牛客暑期多校训练营9 B.Semi-Puzzle: Brain Storm
  • mysql中的窗口函数
  • 【双指针】经典数组双指针题LeetCode
  • 极智嘉x吉利汽车 x京东物流,引领汽车行业智慧物流新变革!
  • RK3588平台开发系列讲解(AI 篇)RKNN C API 详细说明
  • 【基础】Android Handler
  • c语言实现MD5算法
  • Apache Doris 2.0.0 特性分析
  • 如何做H5性能测试?
  • 【Docker】Docker Desktop配置资源:cpu、内存等(windows环境下)
  • 8.2.tensorRT高级(3)封装系列-内存管理的封装,内存的复用
  • Keepalived入门指南:实现故障转移和负载均衡
  • cuOSD(CUDA On-Screen Display Library)库的学习
  • c++函数指针基本用法
  • Java创建对象的几种方式
  • Docker实战专栏简介
  • 解放数据库,实时数据同步利器:Alibaba Canal
  • 机器学习基础之《分类算法(3)—模型选择与调优》
  • Datawhale Django后端开发入门 TASK03 QuerySet和Instance、APIVIew
  • Python 网页解析中级篇:深入理解BeautifulSoup库
  • IDEA 如何制作代码补丁?IDEA 生成 patch 和使用 patch
  • Redis专题-秒杀
  • C++笔记之std::move和右值引用的关系、以及移动语义
  • ES6自用笔记