首页
视频
资源
登录
原
LangChain 链(学习笔记)
1331
人阅读
2023/9/11 14:34
总访问:
2645668
评论:
0
收藏:
0
手机
分类:
python
![](https://img.tnblog.net/arcimg/hb/dcbb080269134178b1048148e5315d8f.png) >#LangChain 链(学习笔记) [TOC] tn2>单一的LLM对于简单的应用场景已经足够,但是更复杂的应用程序需要将LLM串联在一起,需要多LLM协同工作。 简介 ------------ tn2>LangChain提出了 链 的概念,为这种“链式”应用程序提供了 Chain 接口。 Chain 定义组件的调用序列,其中可以包括其他链。 链大大简化复杂应用程序的实现,并使其模块化,这也使调试、维护和改进应用程序变得更容易。 最基础的链 LLMChain ------------ tn2>`LLMChain` 是 `LangChain` 中最基础的链。 本课就从 `LLMChain` 开始,学习链的使用。 >LLMChain 接受如下组件: - LLM - 提示词模版 tn2>`LLMChain` 返回LLM的回复。 在[第二讲](https://www.tnblog.net/hb/article/details/8221)中我们学习了OpenAI LLM的使用。 现在我们基于OpenAI LLM,利用 `LLMChain` 尝试构建自己第一个链。 1.准备必要的组件 ```python from langchain.llms import OpenAI from langchain.prompts import PromptTemplate llm = OpenAI(temperature=0, openai_api_key="您的有效openai ai key") prompt = PromptTemplate( input_variables=["color"], template="What is the hex code of color {color}?", ) ``` tn2>2.基于组件创建 `LLMChain` 实例 我们要创建的链,基于提示词模版,提供基于颜色名字询问对应的16进制代码的能力。 ```python from langchain.chains import LLMChain chain = LLMChain(llm=llm, prompt=prompt) ``` tn2>3.基于链提问 现在我们利用创建的 `LLMChain` 实例提问。 tn>注意:提问中我们只需要提供第一步中创建的提示词模版变量的值。 我们分别提问green,cyan,magento三种颜色的16进制代码。 ```python print(chain.run("green")) print(chain.run("cyan")) print(chain.run("magento")) ``` 你应该期望如下输出: ![](https://img.tnblog.net/arcimg/hb/a720d26840674735ae8ca16ba6b26227.png) LangChainHub ------------ tn2>[LangChainHub](https://github.com/hwchase17/langchain-hub) 收集并分享用于处理 `LangChain` 基本元素(提示词,链,和代理等)。 本讲,我们介绍 `LangChainHub` 中分享的链的使用。 ### Hello World链 tn2>代码仓库:[https://github.com/hwchase17/langchain-hub/blob/master/chains/hello-world/](https://github.com/hwchase17/langchain-hub/blob/master/chains/hello-world/) 链定义:[https://github.com/hwchase17/langchain-hub/blob/master/chains/hello-world/chain.json](https://github.com/hwchase17/langchain-hub/blob/master/chains/hello-world/chain.json) 定义的原始内容: ```json { "memory": null, "verbose": false, "prompt": { "input_variables": [ "topic" ], "output_parser": null, "template": "Tell me a joke about {topic}:", "template_format": "f-string", "_type": "prompt" }, "llm": { "model_name": "text-davinci-003", "temperature": 0.9, "max_tokens": 256, "top_p": 1, "frequency_penalty": 0, "presence_penalty": 0, "n": 1, "best_of": 1, "request_timeout": null, "logit_bias": {}, "_type": "openai" }, "output_key": "text", "_type": "llm_chain" } ``` tn2>这条链,使用如下组件: 1.提示词模版 - 请求LLM回答一个 `topic` 参数指定的话题的笑话 2.LLM - OpenAI的 `text-davince-003` 模型(包括模型相关参数的设置) ### 从LangChainHub加载链 tn2>本课以链[LLM-Math](https://github.com/hwchase17/langchain-hub/tree/master/chains/llm-math)为例,介绍如何从 `LangChainHub` 加载链并使用它。 这是一个使用LLM和Python REPL来解决复杂数学问题的链。 #### 加载 tn2>使用 `load_chain` 函数从hub加载。 ```python from langchain.chains import load_chain import os os.environ['OPENAI_API_KEY'] = "您的有效openai api key" chain = load_chain("lc://chains/llm-math/chain.json") ``` tn>注: 1.OpenAI类允许通过参数 `openai_api_key` 指定API Key,也可以通过环境变量 `OPENAI_API_KEY` 自动加载。 在本例中,load_chain函数完成加载,OpenAI的实例化由框架完成,因此在这里我们用了环境变量来指定API Key。 2.`load_chain` 函数的参数是hub中分享的链的json定义。参数格式:`lc://<链json文件在LangChainHub的相对路径>`。 #### 提问 tn2>现在我们可以基于这个链提问。 ```python # 半径为2的圆的面积是多少? chain.run("whats the area of a circle with radius 2?") ``` tn2>你应该期望如下输出: ![](https://img.tnblog.net/arcimg/hb/5fffeacf20744c978bf2e25fff5e7864.png) 总结 ------------ tn2>本节课程中,我们学习了`LangChain` 提出的最重要的概念 - 链(`Chain`) ,介绍了如何使用链,并分享了如何利用开源社区的力量 - 从 `LangChainHub` 加载链,让LLM开发变得更加轻松。 相关文档资料链接 ------------ tn2>[Python Langchain官方文档](https://python.langchain.com/docs/get_started/introduction.html) [LangChain Hub](https://github.com/hwchase17/langchain-hub) [五里墩茶社](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篇
嵌入式
3篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
Halcon
4篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术