云服务器 Flexus X 实例:开源项目 LangChain 部署,实例测试

电子说

1.3w人已加入

描述

本篇文章主要通过 Flexus 云服务器 X 实例 部署开源项目 LangChain,LangChain 能够为大模型相关开发提供支持,是一个应用程序开发框架。Flexus 云服务器 X 实例 能够支持电商店铺管理、企业建站、小程序后台、小游戏、开发测试环境、云端学习、搭建博客等多种场景,而且,Flexus 云服务器 X 实例 能够提供完备的产品能力,可以基于业务诉求灵活自定义拓扑组网,大模型 QoS 保障,计算性能强劲稳定,满足更高负载业务诉求等优势,需要的小伙伴赶紧用起来吧!

华为云

一、LangChain 介绍

LangChain 是一个由大型语言模型(LLM)驱动的应用程序开发框架。对于这些应用程序,LangChain 简化了整个应用程序生命周期:

开源库:使用 LangChain 的开源构建块、组件和第三方集成构建您的应用程序。使用 LangGraph 构建具有一流流式传输和人机交互支持的状态代理。

生产化:使用 LangSmith 检查、监控和评估您的应用程序,以便您可以不断优化和自信地部署。

部署:使用 LangGraph Cloud 将您的 LangGraph 应用程序转变为可用于生产的 API 和助手。

开源库 langchain-core:基本抽象和 LangChain 表达语言。

langchain-community:第三方集成。 一些集成已进一步拆分为仅依赖 langchain-core 的合作伙伴包。示例包括 langchain_openai 和 langchain_anthropic。

langchain:构成应用程序认知架构的链、代理和检索策略。

LangGraph:一个库,用于通过将步骤建模为图中的边和节点,使用 LLM 构建强大且有状态的多参与者应用程序。与 LangChain 顺利集成,但可以在没有它的情况下使用。

二、部署 LangChain

2.1 安装 langchain

安装 langchain 软件包,执行如下命令安装。

root@flexusx-7305:~# pip install langchain

Collecting langchain

  Downloading langchain-0.3.0-py3-none-any.whl (1.0 MB)

     |████████████████████████████████| 1.0 MB 963 kB/s

Collecting aiohttp<4.0.0,>=3.8.3

  Downloading aiohttp-3.10.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB)

     |████████████████████████████████| 1.2 MB 77.1 MB/s

Collecting langchain-core<0.4.0,>=0.3.0

  Downloading langchain_core-0.3.2-py3-none-any.whl (399 kB)

     |████████████████████████████████| 399 kB 92.2 MB/s

Collecting async-timeout<5.0.0,>=4.0.0; python_version < "3.11"

  Downloading async_timeout-4.0.3-py3-none-any.whl (5.7 kB)

Collecting numpy<2,>=1; python_version < "3.12"

  Using cached numpy-1.26.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.2 MB)

Requirement already satisfied: PyYAML>=5.3 in /usr/lib/python3/dist-packages (from langchain) (5.3.1)

Collecting pydantic<3.0.0,>=2.7.4

  Downloading pydantic-2.9.2-py3-none-any.whl (434 kB)

     |████████████████████████████████| 434 kB 115.1 MB/s

Requirement already satisfied: requests<3,>=2 in /usr/lib/python3/dist-packages (from langchain) (2.22.0)

Collecting langsmith<0.2.0,>=0.1.17

  Downloading langsmith-0.1.125-py3-none-any.whl (290 kB)

     |████████████████████████████████| 290 kB 125.3 MB/s

Collecting tenacity!=8.4.0,<9.0.0,>=8.1.0

  Downloading tenacity-8.5.0-py3-none-any.whl (28 kB)

Collecting langchain-text-splitters<0.4.0,>=0.3.0

  Downloading langchain_text_splitters-0.3.0-py3-none-any.whl (25 kB)

Collecting SQLAlchemy<3,>=1.4

  Downloading SQLAlchemy-2.0.35-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB)

     |████████████████████████████████| 3.1 MB 127.6 MB/s

Collecting multidict<7.0,>=4.5

  Downloading multidict-6.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (124 kB)

     |████████████████████████████████| 124 kB 128.8 MB/s

通过 pip 查看 langchain 是否安装成功。

root@flexusx-7305:~# pip list | grep langchain

langchain                          0.3.0               

langchain-core                     0.3.2               

langchain-text-splitters           0.3.0               

root@flexusx-7305:~#

2.2 安装 langchain_community

安装 langchain_community,执行如下命令安装。

root@flexusx-7305:~/langchain# pip install langchain_community

Collecting langchain_community

  Downloading langchain_community-0.3.0-py3-none-any.whl (2.3 MB)

     |████████████████████████████████| 2.3 MB 983 kB/s

Collecting pydantic-settings<3.0.0,>=2.4.0

  Downloading pydantic_settings-2.5.2-py3-none-any.whl (26 kB)

Requirement already satisfied: requests<3,>=2 in /usr/lib/python3/dist-packages (from langchain_community) (2.22.0)

Requirement already satisfied: PyYAML>=5.3 in /usr/lib/python3/dist-packages (from langchain_community) (5.3.1)

Requirement already satisfied: langchain<0.4.0,>=0.3.0 in /usr/local/lib/python3.10/dist-packages (from langchain_community) (0.3.0)

Requirement already satisfied: langsmith<0.2.0,>=0.1.112 in /usr/local/lib/python3.10/dist-packages (from langchain_community) (0.1.125)

Requirement already satisfied: numpy<2,>=1; python_version < "3.12" in /usr/local/lib/python3.10/dist-packages (from langchain_community) (1.26.4)

Requirement already satisfied: langchain-core<0.4.0,>=0.3.0 in /usr/local/lib/python3.10/dist-packages (from langchain_community) (0.3.2)

Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain_community) (8.5.0)

Collecting dataclasses-json<0.7,>=0.5.7

  Downloading dataclasses_json-0.6.7-py3-none-any.whl (28 kB)

Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.10/dist-packages (from langchain_community) (2.0.35)

Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.10/dist-packages (from langchain_community) (3.10.5)

Requirement already satisfied: pydantic>=2.7.0 in /usr/local/lib/python3.10/dist-packages (from pydantic-settings<3.0.0,>=2.4.0->langchain_community) (2.9.2)

Collecting python-dotenv>=0.21.0

  Using cached python_dotenv-1.0.1-py3-none-any.whl (19 kB)

Requirement already satisfied: langchain-text-splitters<0.4.0,>=0.3.0 in /usr/local/lib/python3.10/dist-packages (from langchain<0.4.0,>=0.3.0->langchain_community) (0.3.0)

Requirement already satisfied: async-timeout<5.0.0,>=4.0.0; python_version < "3.11" in /usr/local/lib/python3.10/dist-packages (from langchain<0.4.0,>=0.3.0->langchain_community) (4.0.3)

Requirement already satisfied: orjson<4.0.0,>=3.9.14 in /usr/local/lib/python3.10/dist-packages (from langsmith<0.2.0,>=0.1.112->langchain_community) (3.10.7)

Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from langsmith<0.2.0,>=0.1.112->langchain_community) (0.27.2)

Requirement already satisfied: packaging<25,>=23.2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.4.0,>=0.3.0->langchain_community) (24.1)

Requirement already satisfied: typing-extensions>=4.7 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.4.0,>=0.3.0->langchain_community) (4.12.2)

Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.4.0,>=0.3.0->langchain_community) (1.33)

Collecting typing-inspect<1,>=0.4.0

  Using cached typing_inspect-0.9.0-py3-none-any.whl (8.8 kB)

Collecting marshmallow<4.0.0,>=3.18.0

  Downloading marshmallow-3.22.0-py3-none-any.whl (49 kB)

     |████████████████████████████████| 49 kB 16.5 MB/s

Requirement already satisfied: greenlet!=0.4.17; python_version < "3.13" and (platform_machine == "aarch64" or (platform_machine == "ppc64le" or (platform_machine == "x86_64" or (platform_machine == "amd64" or (platform_machine == "AMD64" or (platform_machine == "win32" or platform_machine == "WIN32")))))) in /usr/local/lib/python3.10/dist-packages (from SQLAlchemy<3,>=1.4->langchain_community) (3.1.0)

Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain_community) (2.4.0)

Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain_community) (1.3.1)

Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain_community) (1.11.1)

Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain_community) (6.1.0)

Requirement already satisfied: attrs>=17.3.0 in /usr/lib/python3/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain_community) (19.3.0)

Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain_community) (1.4.1)

Requirement already satisfied: pydantic-core==2.23.4 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.7.0->pydantic-settings<3.0.0,>=2.4.0->langchain_community) (2.23.4)

Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.7.0->pydantic-settings<3.0.0,>=2.4.0->langchain_community) (0.7.0)

Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.112->langchain_community) (1.3.1)

Requirement already satisfied: idna in /usr/lib/python3/dist-packages (from httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.112->langchain_community) (2.8)

Requirement already satisfied: certifi in /usr/lib/python3/dist-packages (from httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.112->langchain_community) (2019.11.28)

Requirement already satisfied: anyio in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.112->langchain_community) (4.5.0)

Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.112->langchain_community) (1.0.5)

Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.10/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.4.0,>=0.3.0->langchain_community) (3.0.0)

Collecting mypy-extensions>=0.3.0

  Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)

Requirement already satisfied: exceptiongroup>=1.0.2; python_version < "3.11" in /usr/local/lib/python3.10/dist-packages (from anyio->httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.112->langchain_community) (1.2.2)

Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.112->langchain_community) (0.14.0)

Installing collected packages: python-dotenv, pydantic-settings, mypy-extensions, typing-inspect, marshmallow, dataclasses-json, langchain-community

Successfully installed dataclasses-json-0.6.7 langchain-community-0.3.0 marshmallow-3.22.0 mypy-extensions-1.0.0 pydantic-settings-2.5.2 python-dotenv-1.0.1 typing-inspect-0.9.0

通过 pip 查看 langchain_commity 是否安装成功。

root@flexusx-7305:~/langchain# pip list | grep langchain

langchain                          0.3.0               

langchain-community                0.3.0               

langchain-core                     0.3.2               

langchain-text-splitters           0.3.0               

root@flexusx-7305:~/langchain#

2.3 安装 qianfan

安装 qianfan 软件包,以便访问千帆相关大模型,执行如下命令。

root@flexusx-7305:~/langchain# pip install qianfan

Collecting qianfan

  Downloading qianfan-0.4.8.1-py3-none-any.whl (463 kB)

     |████████████████████████████████| 463 kB 955 kB/s

Collecting pyyaml<7.0.0,>=6.0.1

  Downloading PyYAML-6.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (751 kB)

     |████████████████████████████████| 751 kB 23.3 MB/s

Collecting prompt-toolkit>=3.0.38

  Downloading prompt_toolkit-3.0.47-py3-none-any.whl (386 kB)

     |████████████████████████████████| 386 kB 103.7 MB/s

Collecting rich>=13.0.0

  Using cached rich-13.8.1-py3-none-any.whl (241 kB)

Collecting multiprocess>=0.70.12

  Downloading multiprocess-0.70.16-py310-none-any.whl (134 kB)

     |████████████████████████████████| 134 kB 73.5 MB/s

Requirement already satisfied: pydantic>=1.0 in /usr/local/lib/python3.10/dist-packages (from qianfan) (2.9.2)

Collecting typer>=0.9.0

  Using cached typer-0.12.5-py3-none-any.whl (47 kB)

Requirement already satisfied: tenacity<9.0.0,>=8.2.3 in /usr/local/lib/python3.10/dist-packages (from qianfan) (8.5.0)

Collecting bce-python-sdk>=0.8.79

  Downloading bce_python_sdk-0.9.22-py3-none-any.whl (336 kB)

     |████████████████████████████████| 336 kB 104.5 MB/s

Collecting diskcache>=5.6.3

  Using cached diskcache-5.6.3-py3-none-any.whl (45 kB)

Collecting cachetools>=5.0.0

  Using cached cachetools-5.5.0-py3-none-any.whl (9.5 kB)

Requirement already satisfied: aiohttp>=3.7.0 in /usr/local/lib/python3.10/dist-packages (from qianfan) (3.10.5)

Requirement already satisfied: python-dotenv>=1.0; python_version >= "3.8" in /usr/local/lib/python3.10/dist-packages (from qianfan) (1.0.1)

Collecting requests>=2.24

  Using cached requests-2.32.3-py3-none-any.whl (64 kB)

Collecting aiolimiter>=1.1.0

  Downloading aiolimiter-1.1.0-py3-none-any.whl (7.2 kB)

Collecting wcwidth

  Downloading wcwidth-0.2.13-py2.py3-none-any.whl (34 kB)

Collecting markdown-it-py>=2.2.0

  Downloading markdown_it_py-3.0.0-py3-none-any.whl (87 kB)

通过 pip 命令查看 qianfan 是否安装成功,如下所示。

root@flexusx-7305:~# pip list | grep qianfan

qianfan                            0.4.8.1             

root@flexusx-7305:~#

如果在安装 qianfan 过程中报错如下。

ERROR: Cannot uninstall 'PyYAML'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

可以先执行如下命令后,再安装 qianfan,如下所示。

pip install --ignore-installed PyYAML

三、实例运行

3.1 Chat Models

要使用 qianfan 大模型,需要现在百度千帆平台注册,申请对应的 API Key。

下面就来看一下通过 qianfan 模型测试 langchain 的使用,执行代码如下所示。

#导入包

import os

#import qianfan

from langchain_community.chat_models import QianfanChatEndpoint

from langchain_core.language_models.chat_models import HumanMessage

#配置 API

os.environ["QIANFAN_AK"] = "62vLXXXX"

os.environ["QIANFAN_SK"] = "R5obqXXXX"

#创建 LLM

chat = QianfanChatEndpoint(streaming=False)

messages = [HumanMessage(content="你好")]

res = chat.invoke(messages)

print(res.content)

输出如下所示。 

[WARNING][2024-09-20 22:23:33.999] redis_rate_limiter.py:21 [t:140401586898752]: no redis installed, RedisRateLimiter unavailable

[INFO][2024-09-20 22:23:34.025] oauth.py:277 [t:140401586898752]: trying to refresh token for ak `62vLn7***`

[INFO][2024-09-20 22:23:34.093] oauth.py:304 [t:140401586898752]: successfully refresh token

你好!有什么需要我帮助的吗?

root@flexusx-7305:~/langchain#

如上所示,需要先设置 QIANFAN_AK 和 QIANFAN_SK 两个环境变量,都是千帆大模型的 API Key。

3.2 LLMs

下面来试下直接询问 LLM 问题,执行代码如下所示。

"""For basic init and call"""

import os

from langchain_community.llms import QianfanLLMEndpoint

#配置 API

os.environ["QIANFAN_AK"] = "62vLnXXX"

os.environ["QIANFAN_SK"] = "R5obqzXXXXX"

llm = QianfanLLMEndpoint(streaming=True)

res = llm.invoke("hi")

print(res)

输出如下所示。

(autogenstudy) E:codeexamplelangchain>python langchain_LLMs.py

[INFO][2024-09-21 09:43:00.714] oauth.py:277 [t:8748]: trying to refresh token for ak `62vLn7***`

[INFO][2024-09-21 09:43:00.924] oauth.py:304 [t:8748]: successfully refresh token

你好!有什么我可以帮助你的吗?

(autogenstudy) E:codeexamplelangchain>

3.3 Embedding Models

下面来试下嵌入模型,执行代码如下所示。

"""For basic init and call"""

import os

from langchain_community.embeddings import QianfanEmbeddingsEndpoint

#配置 API

os.environ["QIANFAN_AK"] = "62vLn7qWXXXXXXX"

os.environ["QIANFAN_SK"] = "R5obqzSXXXXXXXX"

embed = QianfanEmbeddingsEndpoint()

res = embed.embed_documents(["hi", "world"])

print(res)

输出如下所示。

(autogenstudy) E:codeexamplelangchain>python langchain_embedding.py

D:Softwareanaconda3envsautogenstudylibsite-packagespydantic_internal_fields.py:161: UserWarning: Field "model_kwargs" has conflict with protected namespace "model_".

You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.

  warnings.warn(

[INFO][2024-09-21 09:48:08.590] oauth.py:277 [t:11932]: trying to refresh token for ak `62vLn7***`

[INFO][2024-09-21 09:48:08.782] oauth.py:304 [t:11932]: successfully refresh token

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.36232587695121765, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

(autogenstudy) E:codeexamplelangchain>

四、总结

通过本篇文章,使用 Flexus 云服务器 X 实例 实例部署了开源项目 LangChain,通过 pip 可以直接安装依赖,包括:langchain、langchain_community、qianfan 等软件包,而且 Flexus 云服务器 X 实例 具有灵活自定义规格、性能稳定强劲、按需灵活计费等优势,有需要的小伙伴赶紧用起来吧!

华为云

审核编辑 黄宇

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分