大家好,我是小锋老师,最近更新《2027版 基于LangChain的RAG与Agent智能体 开发视频教程》专辑,感谢大家支持。
本课程主要介绍和讲解RAG,LangChain简介,接入通义千万大模型 ,Ollama简介以及安装和使用,OpenAI库介绍和使用,以及最重要的基于LangChain实现RAG与Agent智能体开发技术。

视频教程+课件+源码打包下载:
链接:https://pan.baidu.com/s/1_NzaNr0Wln6kv1rdiQnUTg
提取码:0000
基于LangChain的RAG与Agent智能体开发 - 使用LangChain调用大语言模型
我们首先来安装下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等模型。 | 官方合作伙伴包 |

比如我们现在要调用的通义千问大模型,就需要使用到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)
运行输出:

我们使用LangChain来调用Ollama本地大模型
from langchain_ollama import OllamaLLM
# 创建模型
model = OllamaLLM(model="qwen3:4b")
# 调用模型
result = model.invoke(input="你是谁")
print(result)
运行输出:
