首页
视频
资源
登录
原
LangChain 模型(学习笔记)
1619
人阅读
2023/9/7 11:01
总访问:
2654987
评论:
0
收藏:
0
手机
分类:
python
![](https://img.tnblog.net/arcimg/hb/dcbb080269134178b1048148e5315d8f.png) >#LangChain 模型(学习笔记) [TOC] Langchain 模型简介 ------------ tn2>Langchain所封装的模型分为两类: ——大语言模型 (LLM) ——聊天模型 (Chat Models) ——在后续的内容中,为简化描述,我们将使用 LLM 来指代大语言模型。<br/> Langchain的支持众多模型供应商,包括OpenAI、ChatGLM、HuggingFace等。 本教程中,我们将以OpenAI为例,后续内容中提到的模型默认为OpenAI提供的模型。<br/> Langchain的封装,比如,对OpenAI模型的封装,实际上是指的是对OpenAI API的封装。 ### LLM tn2>LLM 是一种基于统计的机器学习模型,用于对文本数据进行建模和生成。LLM学习和捕捉文本数据中的语言模式、语法规则和语义关系,以生成连贯并合乎语言规则的文本。<br/> 在Langchain的环境中,LLM特指文本补全模型(text completion model)。 tn>注:文本补全模型是一种基于语言模型的机器学习模型,根据上下文的语境和语言规律,自动推断出最有可能的下一个文本补全。 | 输入 | 输出 | | ------------ | ------------ | | 一条文本内容 | 一条文本内容 | ### 聊天模型 (Chat Models) tn2>聊天模型是语言模型的一种变体。聊天模型使用语言模型,并提供基于"聊天消息"的接口。 | 输入 | 输出 | | ------------ | ------------ | | 一组聊天消息 | 一条聊天消息 | Langchain与OpenAI模型 ------------ tn2>参考OpenAI Model endpoint compatibility 文档,gpt模型都归为了聊天模型,而davinci, curie, babbage, ada模型都归为了文本补全模型。 |ENDPOINT| MODEL NAME| | ------------ | ------------ | |/v1/chat/completions| gpt-4, gpt-4-0613, gpt-4-32k, gpt-4-32k-0613, gpt-3.5-turbo, gpt-3.5-turbo-0613, gpt-3.5-turbo-16k, gpt-3.5-turbo-16k-0613| |/v1/completions| (Legacy) text-davinci-003, text-davinci-002, text-davinci-001, text-curie-001, text-babbage-001, text-ada-001, davinci, curie, babbage, ada| tn2>Langchain提供接口集成不同的模型。 为了便于切换模型,Langchain将不同模型抽象为相同的接口 `BaseLanguageModel`,并提供 `predict` 和 `predict_messages` 函数来调用模型。 当使用LLM时推荐使用`predict`函数,当使用聊天模型时推荐使用`predict_messages`函数。 示例代码 ------------ ### 与LLM的交互 tn2>接下来我们来看看如何在Langchain中使用LLM和聊天模型。 ```python 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) ``` tn2>你应该能看到类似如下输出: ![](https://img.tnblog.net/arcimg/hb/c6f54f013f68453880a38a1693c1816d.png) ### 与聊天模型的交互 tn2>与聊天模型的交互,我们需要使用 `langchain.chat_models` 模块中的 ChatOpenAI 类。 ```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) response = chat.predict_messages([ HumanMessage(content="What is AI?") ]) print(response) ``` tn2>你应该能看到类似如下输出: ```python 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 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 ``` tn2>通过以下代码我们查看一下 `response` 变量的类型: ```python response.__class__ ``` tn2>可以看到,它是一个 `AIMessage` 类型的对象。 ![](https://img.tnblog.net/arcimg/hb/666497d238a446d6aa0b6cfde012eab4.png) tn2>接下来我们使用 `SystemMessage` 指令来指定模型的行为。如下代码指定模型对AI一无所知,在回答AI相关问题时,回答`I don't know`。 ```python 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) ``` ![](https://img.tnblog.net/arcimg/hb/6712ff7fca54496b99b1c6ce58cdf24c.png) tn2>3个消息类 Langchain框架提供了三个消息类,分别是 `AIMessage`、`HumanMessage` 和 `SystemMessage`。它们对应了OpenAI聊天模型API支持的不同角色 `assistant`、`user` 和 `system`。请参考 [OpenAI API文档 - Chat - Role](https://platform.openai.com/docs/api-reference/chat/create#chat/create-role)。 | Langchain类 | OpenAI角色 | 作用 | | -------- | ------- | ------- | | AIMessage | assistant | 模型回答的消息 | | HumanMessage | user | 用户向模型的请求或提问 | | SystemMessage | system | 系统指令,用于指定模型的行为 | ## 总结 本节课程中,我们学习了模型的基本概念,LLM与聊天模型的差异,并基于 `Langchain` 实现了分别与OpenAI LLM和聊天模型的交互。 学习链接 ------------ tn2>更多学习内容,可以关注一下五里墩茶社大佬 https://space.bilibili.com/615957867?spm_id_from=333.337.0.0
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
171篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
4篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
Halcon
5篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术