LangChain 二 模型

上一篇我们写了LangChain的hello world,这一篇我们来看LangChain的Model模块。

前言

python 复制代码
from langchain.chat_models import ChatOpenAI 
from langchain.schema import HumanMessage
chat = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo") 
response = chat([ HumanMessage(content="Hello Langchain!") ]) 
print(response)

  上文的Hello 例子中, 展示了LangChain包含chat_models聊天模型。别外,LangChain还封装了大语言模型。我们可以这样理解:LangChain为AI应用框架,封装大语言模型肯定是核心功能,之所以还单独提供一个chat_model,是因为AI聊天机器人是主要的AI应用形式,为了方便,单独拎了聊天模型出来。

LLM 大模型

  LangChain支持的模型有OpenAI、ChatGLM、Llama、Hugging Face。其中,Hugging Face是目前最成功的大模型社区,我们可以方便的在这里挑选相应的大模型完成各种NLP任务。最近,Google推出的Gemini,号称比OpenAI 4.0还优秀,期待中....

Completion

  OpenAI为代表的AIGC大模型,LangChain在与之共舞时,主要以文本补全为主。LangChain将Prompt喂给大模型,生成文本补全。我们常用的文本补全模型有text-davinci-003等

聊天模型

  聊天模型是语言模型的一种变体。其聊天消息的接口会涉及一些参数操作,比如聊天记忆、角色等。我们常用的聊天模型有gpt-4、gpt-3.5-turbo

接口

LangChain提供接口集成不同的模型,并抽象了BaseLanguageModel对接这些模型,可以使用predict、predict_messages调用模型。

  • predict demo
python 复制代码
!pip install langchain==0.0.235 openai

from langchain.llms import OpenAI 
import os os.environ['OPENAI_API_KEY'] = '您的有效OpenAI API Key' 
llm = OpenAI(model_name="text-davinci-003") response = llm.predict("What is AI?") print(response)

  LangChain这次调用OpenAI的过程,首先由langchain.llms 大模型中引入OpenAI。接着实例化OpenAI,并选择model_name="text-davinci-003",最后调用实例上的predict方法,让它生成以下回答。

vbnet 复制代码
AI (Artificial Intelligence) is the ability of a computer or machine to think and learn, and to act independently of humans. It includes the use of algorithms, data analysis, and robotics to carry out complex tasks that would normally require human intelligence. AI can be used to automate mundane tasks, improve decision-making processes, and create new products and services.
  • predict_messages demo
python 复制代码
from langchain.chat_models import ChatOpenAI from langchain.schema import AIMessage, HumanMessage, SystemMessage 
import os
os.environ['OPENAI_API_KEY'] = '您的有效OpenAI API Key'
chat = ChatOpenAI(temperature=0,model_name='gpt-3.5-turbo') 
response = chat.predict_messages([ HumanMessage(content="What is AI?") ]) 

print(response)

  这个例子和上篇的例子差不多,区别在于chat.predict_messages,而上篇直接chat。下面是聊天内容的返回。

vbnet 复制代码
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 typically require human intelligence, such as visual perception, 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.' additional_kwargs={} example=False
markdown 复制代码
response.__class__
langchain.schema.messages.AIMessage
scss 复制代码
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 deep learning?") ]) 

print(response)

  这个例子我们使用SystemMessage指定了聊天机器人的角色,假设他不懂AI, 再使用HumanMessage问了相关AI的问题,得到的答案是NO!

总结

  • LangChain提供了聊天和文本补全两类模型,即Completion和Chat。
  • 不管是补全式还是聊天式大模型,都有predict 的概念 即推理。
  • LangChain在聊天模型中提供了三个消息类,分别是AIMessage、HumanMessage、SystemMessage
  • 下次花时间探索下LangChain调用Hugging Face 上其它模型的例子。

参考资料

相关推荐
霸道流氓气质几秒前
AI 聊天机器人组件:ChatMemory 对话记忆、SSE 心跳保活与可嵌入组件封装的 Vue3 + Spring AI 生产级实战
人工智能·spring·机器人
是枚小菜鸡儿吖1 分钟前
听歌截图太普通?我把MusicCard部署到极空间,把歌曲做成随时可开的音乐海报页
服务器·人工智能·后端·ai·大模型
sqc233sqc3 分钟前
第 J8 周:ResNeXt-50 算法解析
人工智能·算法
哈__7 分钟前
260+异构同步链路:DDL语法自动翻译在大规模数据同步中的实现原理
人工智能·自然语言处理·机器翻译
虎鲸不是鱼11 分钟前
【虎鲸进化论 13】CI/CD:它在服务器上铺了条流水线
服务器·人工智能·ci/cd·大模型·自进化agent
voipmaker19 分钟前
融合通信调度录音的困局:基于 MediaX 网关实现对讲语音 AI 事件萃取
人工智能·ai·音视频·语音识别·实时音视频·信息与通信·iot
SkyWalking中文站21 分钟前
LLM 回答得好不好?SkyWalking 用 LLM-as-Judge 评估线上回答
后端·aigc
明志数科27 分钟前
机器人数据采集行业供给侧出清:重资产模式的结构性约束与质量工程转向
大数据·人工智能
龙亘川1 小时前
电子签章服务与核验业务建模:从在线签署、可信核验到归档追溯的全流程闭环
大数据·人工智能·智慧城市·开源软件
杨杨杨大侠7 小时前
RAG、记忆、上下文,这几个东西到底怎么串起来?
人工智能·agent