langchain实战-hello world

一、LangChain简介

LangChain是一个用于开发由语言模型支持的应用程序的框架。它使应用程序能够:
  • 具有上下文感知能力:将语言模型与上下文源连接起来(提示说明、几个镜头示例、响应的内容等)
  • 推理:依靠语言模型进行推理(关于如何根据提供的上下文回答、采取什么操作等)
该框架由几个部分组成。
  • LangChain 库:Python 和 JavaScript 库。包含无数组件的接口和集成、将这些组件组合成链和代理的基本运行时,以及链和代理的现成实现。
  • LangChain 模板:一系列易于部署的参考架构,适用于各种任务。
  • LangServe:用于将 LangChain 链部署为 REST API 的库。
  • LangSmith:一个开发者平台,可让您调试、测试、评估和监控基于任何 LLM 框架构建的链,并与 LangChain 无缝集成。

二、为什么学习LangChain

         作为一种专为开发基于语言模型的应用而设计的框架,通过 LangChain,我们不仅可以通过 API 调用如 ChatGPT、GPT-4、Llama 2 等大型语言模型,还可以实现更高级的功能。
         真正有潜力且具有创新性的应用,不仅仅在于能通过 API 调用语言模型,更重要的是能够具备以下两个特性:
数据感知:能够将语言模型与其他数据源连接起来,从而实现对更丰富、更多样化数据的理解和利用。
具有代理性:能够让语言模型与其环境进行交互,使得模型能够对其环境有更深入的理解,并能够进行有效的响应。
       LangChain 框架的设计目标,是使这种 AI 类型的应用成为可能,并帮助我们最大限度地释放大语言模型的潜能。

三、hello world

安装langchain和openai python包。
pip3 install langchain -i https://pypi.tuna.tsinghua.edu.cn/simple/

pip3 install langchain -i https://pypi.tuna.tsinghua.edu.cn/simple/
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting langchain
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/15/f9/e79403efb880425babaa91f8ab19e1b6218dc694551fa6e7f40197b5a72a/langchain-0.0.335-py3-none-any.whl (2.0 MB)
     ---------------------------------------- 2.0/2.0 MB 5.5 MB/s eta 0:00:00
Collecting PyYAML>=5.3 (from langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/24/97/9b59b43431f98d01806b288532da38099cc6f2fea0f3d712e21e269c0279/PyYAML-6.0.1-cp310-cp310-win_amd64.whl (145 kB)
Collecting SQLAlchemy<3,>=1.4 (from langchain)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2d/ca/1ad0106300c9cbd14e2b88bffe9f52f03ce2abe48f3b88747e520180014e/SQLAlchemy-2.0.23-cp310-cp310-win_amd64.whl (2.1 MB)
     ---------------------------------------- 2.1/2.1 MB 9.4 MB/s eta 0:00:00
Collecting aiohttp<4.0.0,>=3.8.3 (from langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/1d/60/63b8a1e05956ff92916128c192c9317c8eaaaffacc8fef4ef95db0f84264/aiohttp-3.8.6-cp310-cp310-win_amd64.whl (325 kB)
Collecting anyio<4.0 (from langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/19/24/44299477fe7dcc9cb58d0a57d5a7588d6af2ff403fdd2d47a246c91a3246/anyio-3.7.1-py3-none-any.whl (80 kB)
Collecting async-timeout<5.0.0,>=4.0.0 (from langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/a7/fa/e01228c2938de91d47b307831c62ab9e4001e747789d0b05baf779a6488c/async_timeout-4.0.3-py3-none-any.whl (5.7 kB)
Collecting dataclasses-json<0.7,>=0.5.7 (from langchain)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8d/e2/528c52001a743a7faa28e6d3095d9f01b472d3efee62d62101403bf1a70a/dataclasses_json-0.6.2-py3-none-any.whl (28 kB)
Collecting jsonpatch<2.0,>=1.33 (from langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/73/07/02e16ed01e04a374e644b575638ec7987ae846d25ad97bcc9945a3ee4b0e/jsonpatch-1.33-py2.py3-none-any.whl (12 kB)
Collecting langsmith<0.1.0,>=0.0.63 (from langchain)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/15/9e/e3153e6c94b73ff52ab7feed40801e15ed5a7c1e7f43422703469b4b42e6/langsmith-0.0.64-py3-none-any.whl (45 kB)
     ---------------------------------------- 45.5/45.5 kB 2.3 MB/s eta 0:00:00
Collecting numpy<2,>=1 (from langchain)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/24/b5/fed6f7e582937eb947369dccf6c94602598a25f23e482d1b1f2299159328/numpy-1.26.2-cp310-cp310-win_amd64.whl (15.8 MB)
     ---------------------------------------- 15.8/15.8 MB 3.8 MB/s eta 0:00:00
Collecting pydantic<3,>=1 (from langchain)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d7/10/ddfb9539a6e55f7dfd6c2b9b81d86fcba2761ba87eeb81f8b1012957dcdc/pydantic-2.5.0-py3-none-any.whl (407 kB)
     ---------------------------------------- 407.5/407.5 kB 4.2 MB/s eta 0:00:00
Collecting requests<3,>=2 (from langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/70/8e/0e2d847013cb52cd35b38c009bb167a1a26b2ce6cd6965bf26b47bc0bf44/requests-2.31.0-py3-none-any.whl (62 kB)
Collecting tenacity<9.0.0,>=8.1.0 (from langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/f4/f1/990741d5bb2487d529d20a433210ffa136a367751e454214013b441c4575/tenacity-8.2.3-py3-none-any.whl (24 kB)
Collecting attrs>=17.3.0 (from aiohttp<4.0.0,>=3.8.3->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/f0/eb/fcb708c7bf5056045e9e98f62b93bd7467eb718b0202e7698eb11d66416c/attrs-23.1.0-py3-none-any.whl (61 kB)
Collecting charset-normalizer<4.0,>=2.0 (from aiohttp<4.0.0,>=3.8.3->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/a2/a0/4af29e22cb5942488cf45630cbdd7cefd908768e69bdd90280842e4e8529/charset_normalizer-3.3.2-cp310-cp310-win_amd64.whl (100 kB)
Collecting multidict<7.0,>=4.5 (from aiohttp<4.0.0,>=3.8.3->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/24/d1/56b6d5eb964161c55a8a7ad53fe4c93a694e44d04fd1405f3c1b98de5627/multidict-6.0.4-cp310-cp310-win_amd64.whl (28 kB)
Collecting yarl<2.0,>=1.0 (from aiohttp<4.0.0,>=3.8.3->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/98/21/9ef4adf36cfac771518c3bf687bc9b92451bdaf01ec770879f19e7e5b3c7/yarl-1.9.2-cp310-cp310-win_amd64.whl (61 kB)
Collecting frozenlist>=1.1.1 (from aiohttp<4.0.0,>=3.8.3->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/ad/1a/0035d6dd5e51580c060f80ac6819f0ea55da2b1f2dad8ad15f5d3e50d91c/frozenlist-1.4.0-cp310-cp310-win_amd64.whl (44 kB)
Collecting aiosignal>=1.1.2 (from aiohttp<4.0.0,>=3.8.3->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/76/ac/a7305707cb852b7e16ff80eaf5692309bde30e2b1100a1fcacdc8f731d97/aiosignal-1.3.1-py3-none-any.whl (7.6 kB)
Collecting idna>=2.8 (from anyio<4.0->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/fc/34/3030de6f1370931b9dbb4dad48f6ab1015ab1d32447850b9fc94e60097be/idna-3.4-py3-none-any.whl (61 kB)
Collecting sniffio>=1.1 (from anyio<4.0->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/c3/a0/5dba8ed157b0136607c7f2151db695885606968d1fae123dc3391e0cfdbf/sniffio-1.3.0-py3-none-any.whl (10 kB)
Collecting exceptiongroup (from anyio<4.0->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/ad/83/b71e58666f156a39fb29417e4c8ca4bc7400c0dd4ed9e8842ab54dc8c344/exceptiongroup-1.1.3-py3-none-any.whl (14 kB)
Collecting marshmallow<4.0.0,>=3.18.0 (from dataclasses-json<0.7,>=0.5.7->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/ed/3c/cebfdcad015240014ff08b883d1c0c427f2ba45ae8c6572851b6ef136cad/marshmallow-3.20.1-py3-none-any.whl (49 kB)
Collecting typing-inspect<1,>=0.4.0 (from dataclasses-json<0.7,>=0.5.7->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/65/f3/107a22063bf27bdccf2024833d3445f4eea42b2e598abfbd46f6a63b6cb0/typing_inspect-0.9.0-py3-none-any.whl (8.8 kB)
Collecting jsonpointer>=1.9 (from jsonpatch<2.0,>=1.33->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/12/f6/0232cc0c617e195f06f810534d00b74d2f348fe71b2118009ad8ad31f878/jsonpointer-2.4-py2.py3-none-any.whl (7.8 kB)
Collecting annotated-types>=0.4.0 (from pydantic<3,>=1->langchain)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/28/78/d31230046e58c207284c6b2c4e8d96e6d3cb4e52354721b944d3e1ee4aa5/annotated_types-0.6.0-py3-none-any.whl (12 kB)
Collecting pydantic-core==2.14.1 (from pydantic<3,>=1->langchain)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c0/80/f01ea5b4520d30286838a152f9d1b9569bf650c46b66723b3bf43ff8253c/pydantic_core-2.14.1-cp310-none-win_amd64.whl (1.9 MB)
     ---------------------------------------- 1.9/1.9 MB 5.2 MB/s eta 0:00:00
Collecting typing-extensions>=4.6.1 (from pydantic<3,>=1->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/24/21/7d397a4b7934ff4028987914ac1044d3b7d52712f30e2ac7a2ae5bc86dd0/typing_extensions-4.8.0-py3-none-any.whl (31 kB)
Collecting urllib3<3,>=1.21.1 (from requests<3,>=2->langchain)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/96/94/c31f58c7a7f470d5665935262ebd7455c7e4c7782eb525658d3dbf4b9403/urllib3-2.1.0-py3-none-any.whl (104 kB)
     ---------------------------------------- 104.6/104.6 kB 5.9 MB/s eta 0:00:00
Collecting certifi>=2017.4.17 (from requests<3,>=2->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/4c/dd/2234eab22353ffc7d94e8d13177aaa050113286e93e7b40eae01fbf7c3d9/certifi-2023.7.22-py3-none-any.whl (158 kB)
Collecting greenlet!=0.4.17 (from SQLAlchemy<3,>=1.4->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/02/72/36fb2c35547fdf473629579fc35d9a2034592ea3f01710702d81ef596e16/greenlet-3.0.1-cp310-cp310-win_amd64.whl (287 kB)
Collecting packaging>=17.0 (from marshmallow<4.0.0,>=3.18.0->dataclasses-json<0.7,>=0.5.7->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/ec/1a/610693ac4ee14fcdf2d9bf3c493370e4f2ef7ae2e19217d7a237ff42367d/packaging-23.2-py3-none-any.whl (53 kB)
Collecting mypy-extensions>=0.3.0 (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/2a/e2/5d3f6ada4297caebe1a2add3b126fe800c96f56dbe5d1988a2cbe0b267aa/mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)
Installing collected packages: urllib3, typing-extensions, tenacity, sniffio, PyYAML, packaging, numpy, mypy-extensions, multidict, jsonpointer, idna, greenlet, frozenlist, exceptiongroup, charset-normalizer, certifi, attrs, async-timeout, annotated-types, yarl, typing-inspect, SQLAlchemy, requests, pydantic-core, marshmallow, jsonpatch, anyio, aiosignal, pydantic, dataclasses-json, aiohttp, langsmith, langchain
Successfully installed PyYAML-6.0.1 SQLAlchemy-2.0.23 aiohttp-3.8.6 aiosignal-1.3.1 annotated-types-0.6.0 anyio-3.7.1 async-timeout-4.0.3 attrs-23.1.0 certifi-2023.7.22 charset-normalizer-3.3.2 dataclasses-json-0.6.2 exceptiongroup-1.1.3 frozenlist-1.4.0 greenlet-3.0.1 idna-3.4 jsonpatch-1.33 jsonpointer-2.4 langchain-0.0.335 langsmith-0.0.64 marshmallow-3.20.1 multidict-6.0.4 mypy-extensions-1.0.0 numpy-1.26.2 packaging-23.2 pydantic-2.5.0 pydantic-core-2.14.1 requests-2.31.0 sniffio-1.3.0 tenacity-8.2.3 typing-extensions-4.8.0 typing-inspect-0.9.0 urllib3-2.1.0 yarl-1.9.2
pip3 install openai  -i https://pypi.tuna.tsinghua.edu.cn/simple/

(langchain) D:\>pip3 install openai  -i https://pypi.tuna.tsinghua.edu.cn/simple/
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting openai
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/38/ae/0a6b73156176c10ff52b94f5444712bcdb8d22dddf68f106c14f0937e390/openai-1.2.4-py3-none-any.whl (220 kB)
     ---------------------------------------- 220.2/220.2 kB 1.3 MB/s eta 0:00:00
Requirement already satisfied: anyio<4,>=3.5.0 in c:\users\chenjd\.conda\envs\langchain\lib\site-packages (from openai) (3.7.1)
Collecting distro<2,>=1.7.0 (from openai)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f4/2c/c90a3adaf0ddb70afe193f5ebfb539612af57cffe677c3126be533df3098/distro-1.8.0-py3-none-any.whl (20 kB)
Collecting httpx<1,>=0.23.0 (from openai)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/82/61/a5fca4a1e88e40969bbd0cf0d981f3aa76d5057db160b94f49603fc18740/httpx-0.25.1-py3-none-any.whl (75 kB)
     ---------------------------------------- 75.0/75.0 kB 4.3 MB/s eta 0:00:00
Requirement already satisfied: pydantic<3,>=1.9.0 in c:\users\chenjd\.conda\envs\langchain\lib\site-packages (from openai) (2.5.0)
Collecting tqdm>4 (from openai)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/00/e5/f12a80907d0884e6dff9c16d0c0114d81b8cd07dc3ae54c5e962cc83037e/tqdm-4.66.1-py3-none-any.whl (78 kB)
Requirement already satisfied: typing-extensions<5,>=4.5 in c:\users\chenjd\.conda\envs\langchain\lib\site-packages (from openai) (4.8.0)
Requirement already satisfied: idna>=2.8 in c:\users\chenjd\.conda\envs\langchain\lib\site-packages (from anyio<4,>=3.5.0->openai) (3.4)
Requirement already satisfied: sniffio>=1.1 in c:\users\chenjd\.conda\envs\langchain\lib\site-packages (from anyio<4,>=3.5.0->openai) (1.3.0)
Requirement already satisfied: exceptiongroup in c:\users\chenjd\.conda\envs\langchain\lib\site-packages (from anyio<4,>=3.5.0->openai) (1.1.3)
Requirement already satisfied: certifi in c:\users\chenjd\.conda\envs\langchain\lib\site-packages (from httpx<1,>=0.23.0->openai) (2023.7.22)
Collecting httpcore (from httpx<1,>=0.23.0->openai)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/56/ba/78b0a99c4da0ff8b0f59defa2f13ca4668189b134bd9840b6202a93d9a0f/httpcore-1.0.2-py3-none-any.whl (76 kB)
     ---------------------------------------- 76.9/76.9 kB 2.2 MB/s eta 0:00:00
Requirement already satisfied: annotated-types>=0.4.0 in c:\users\chenjd\.conda\envs\langchain\lib\site-packages (from pydantic<3,>=1.9.0->openai) (0.6.0)
Requirement already satisfied: pydantic-core==2.14.1 in c:\users\chenjd\.conda\envs\langchain\lib\site-packages (from pydantic<3,>=1.9.0->openai) (2.14.1)
Collecting colorama (from tqdm>4->openai)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Collecting h11<0.15,>=0.13 (from httpcore->httpx<1,>=0.23.0->openai)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/95/04/ff642e65ad6b90db43e668d70ffb6736436c7ce41fcc549f4e9472234127/h11-0.14.0-py3-none-any.whl (58 kB)
Installing collected packages: h11, distro, colorama, tqdm, httpcore, httpx, openai
Successfully installed colorama-0.4.6 distro-1.8.0 h11-0.14.0 httpcore-1.0.2 httpx-0.25.1 openai-1.2.4 tqdm-4.66.1


(langchain) D:\>

运行hello world

(langchain) D:\>python
Python 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:34:57) [MSC v.1936 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ["OPENAI_API_KEY"]="你的open ai apikey"
>>> from langchain.llms import OpenAI
>>> llm = OpenAI(model_name="text-davinci-003",max_tokens=200)
>>> text = llm("请给我写一句情人节红玫瑰的中文宣传语")
>>> print(text)
"让满满的爱意萦绕,红玫瑰给你最美的浪漫祝福!"
>>> text = llm("请给我写一句情人节红玫瑰的中文宣传语")
>>> print(text)
"情深缘浓,玫瑰爱相随!"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农丁丁

你的认可是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值