LangChain-Learning_new/README.md
2026-04-16 01:02:24 +08:00

184 lines
4.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# LangChain Learning Project
> 基于 LangChain 1.0.3 的 AI Agent 学习项目,展示如何使用 MCP (Model Context Protocol) 构建智能代理
## 项目概述
本项目是一个 LangChain 学习与实践项目,演示了如何构建具有外部工具调用能力的 AI Agent。通过集成 MCP 服务器,实现了对外部服务的调用,如天气查询、数学运算、学术搜索等功能。
## 技术栈
| 技术 | 版本 | 用途 |
|------|------|------|
| **LangChain** | 1.0.3 | 核心框架 |
| **LangChain-Community** | 0.4.1 | 社区组件 |
| **LangChain-MCP-Adapters** | ≥0.1.11 | MCP 客户端 |
| **FastMCP** | ≥2.13.0 | MCP 协议实现 |
| **LangChain-Ollama** | ≥1.0.0 | Ollama 本地模型 |
| **LangGraph** | - | Agent 记忆组件 |
## 项目结构
```
langchain-learning/
├── agent/
│ ├── langchain_agent.py # MCP Agent (Ollama + weather + math)
│ └── langchain_agent_tool_mcp.py # 扩展 Agent (+ arxiv + wikipedia + 自定义工具)
├── mcp/
│ ├── math_server.py # 数学运算 MCP 服务器 (stdio)
│ └── get_weather_server.py # 天气查询 MCP 服务器 (HTTP)
├── ollama/
│ └── ollama_demo.py # Ollama 本地模型调用示例
├── main.py # 程序入口
├── pyproject.toml # 项目配置
└── .env # 环境变量 (API Keys)
```
## 功能特性
### MCP 服务器
| 服务器 | 功能 | 传输方式 | 端口 |
|--------|------|----------|------|
| `math_server` | 加法、乘法运算 | stdio | - |
| `weather_server` | 城市天气查询 | HTTP/SSE | 9000 |
### 支持的 LLM
| 模型 | 类型 | 调用方式 |
|------|------|----------|
| `gemma4:e2b` / `gemma4:26b` | Ollama 本地 | `langchain_ollama` |
| `Qwen/Qwen3-14B` | SiliconFlow 云端 | OpenAI 兼容接口 |
### 工具集成
| 来源 | 工具 |
|------|------|
| **MCP Servers** | `get_weather`, `add`, `multiply` |
| **内置 (load_tools)** | `arxiv`, `wikipedia` |
| **自定义** | `greet(name)` |
## 快速开始
### 1. 安装依赖
```bash
uv sync
```
### 2. 配置环境变量
编辑 `.env` 文件:
```env
# Ollama (本地)
OLLAMA_API_KEY=ollama
OLLAMA_BASE_URL=http://localhost:11434/v1
# SiliconFlow (可选云端)
SILICONFLOW_API_KEY=your_siliconflow_key
SILICONFLOW_BASE_URL=https://api.siliconflow.cn/v1
# 天气API
WEATHER_API_KEY=your_weatherapi_key
```
### 3. 启动 MCP 服务器
```bash
# 终端 1: 启动天气服务器 (HTTP)
python mcp/get_weather_server.py
# 终端 2: 启动数学服务器 (stdio)
python mcp/math_server.py
```
### 4. 运行 Agent
```bash
# Agent 1: Ollama + MCP (weather + math)
python agent/langchain_agent.py
# Agent 2: Ollama + MCP + 内置工具 + 自定义工具
python agent/langchain_agent_tool_mcp.py
```
### 5. Ollama 直接调用示例
```bash
python ollama/ollama_demo.py
```
## 使用示例
```
请输入你的问题输入exit则退出 > 北京天气怎么样
计算过程:
- 调用工具: get_weather({'city': '北京'})
- get_weather 的结果是: {"location":{"name":"北京",...},"current":{"temp_c":22.0,...}}
最终答案: 北京的天气晴朗温度约22°C。
```
## Agent 对比
| Agent | 模型 | MCP | 内置工具 | 自定义工具 |
|-------|------|-----|----------|------------|
| `langchain_agent.py` | Ollama | ✅ weather, math | ❌ | ❌ |
| `langchain_agent_tool_mcp.py` | Ollama | ✅ weather, math | ✅ arxiv, wikipedia | ✅ greet |
## 核心代码说明
### MCP 客户端配置
```python
client = MultiServerMCPClient({
"weather": {
"url": "http://localhost:9000/mcp",
"transport": "streamable_http",
},
"math": {
"command": sys.executable, # 使用当前解释器
"args": ["./mcp/math_server.py"],
"transport": "stdio"
}
})
```
### Agent 创建
```python
from langchain.agents import create_agent
from langgraph.checkpoint.memory import InMemorySaver
agent = create_agent(
model=llm,
tools=tools,
checkpointer=InMemorySaver() # 会话记忆
)
```
## 依赖列表
核心依赖见 `pyproject.toml`
- `langchain >= 1.0.3`
- `langchain-community >= 0.4.1`
- `langchain-mcp-adapters >= 0.1.11`
- `langchain-ollama >= 1.0.0`
- `fastmcp >= 2.13.0.2`
- `wikipedia >= 1.4.0`
- `arxiv >= 2.2.0`
## 学习资源
- [LangChain 官方文档](https://python.langchain.com/)
- [MCP 协议规范](https://modelcontextprotocol.io/)
- [LangChain GitHub](https://github.com/langchain-ai/langchain)
- [FastMCP 文档](https://github.com/jlowin/fastmcp)
## License
MIT License