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和聊天模型的对话。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值