【技术专题】基于LangChain的RAG与Agent智能体 - 使用LangChain调用大语言模型

0 阅读1分钟

大家好,我是锋哥。最近连载更新《基于LangChain的RAG与Agent智能体》技术专题。

QQ截图20260324143646.jpg 本课程主要介绍和讲解RAG,LangChain简介,接入通义千万大模型,Ollama简介以及安装和使用,OpenAI库介绍和使用,以及最重要的基于LangChain实现RAG与Agent智能体开发技术。同时也配套视频教程 《2027版 基于LangChain的RAG与Agent智能体开发视频教程》

我们首先来安装下LangChain库

pip install langchain langchain-community langchain-ollama  -i https://pypi.tuna.tsinghua.edu.cn/simple  --trusted-host pypi.tuna.tsinghua.edu.cn
库名核心定位主要职责来源 / 状态
langchain框架核心与高层逻辑提供构建AI应用的链、代理、检索策略等“认知架构”,是框架的“大脑”和“骨架。官方核心库
langchain-community第三方集成汇总包含由社区维护的所有第三方集成(如各种文档加载器、向量存储、工具等),是一个“百宝箱”。官方社区版
langchain-ollama特定服务集成专门用于连接Ollama服务的“专用工具包”,让你能调用本地的Llama 3、Qwen等模型。官方合作伙伴包

image.png

比如我们现在要调用的通义千问大模型,就需要使用到langchain-community社区库。当然内部还需要dashscope库的支持,也需要安装下。

pip install dashscope -i https://pypi.tuna.tsinghua.edu.cn/simple  --trusted-host pypi.tuna.tsinghua.edu.cn

DashScope(模型服务灵积)是阿里云推出的一个模型服务平台。它就像阿里云版的“OpenAI API 平台”,旨在通过标准化的API接口,为开发者提供丰富、易用的AI模型服务 。

下面是LangChain调用通义千问大语言模型的参考代码:

from langchain_community.llms.tongyi import Tongyi
​
# 创建模型
model = Tongyi(model="qwen-plus")
​
# 调用模型
result = model.invoke(input="你是谁")
print(result)

运行输出:

image.png

我们使用LangChain来调用Ollama本地大模型

from langchain_ollama import OllamaLLM
​
# 创建模型
model = OllamaLLM(model="qwen3:4b")
​
# 调用模型
result = model.invoke(input="你是谁")
print(result)

运行输出:

image.png