175 lines
4.0 KiB
Markdown
175 lines
4.0 KiB
Markdown
# LangChain + LangGraph Scaffolding
|
|
|
|
一个使用 LangChain 和 LangGraph 构建的 AI 应用脚手架项目,提供模块化的代理和工作流管理。
|
|
|
|
## 特性
|
|
|
|
- 🚀 **模块化架构**: 基于代理和工作流的模块化设计
|
|
- 🔧 **工具集成**: 支持自定义工具和函数调用
|
|
- 💬 **多轮对话**: 内置对话状态管理和上下文维护
|
|
- 📊 **工作流管理**: 多种工作流类型,支持会话和工具使用
|
|
- ⚙️ **配置管理**: 统一的环境变量和配置管理
|
|
- 🧪 **测试支持**: 包含基础测试和示例代码
|
|
|
|
## 项目结构
|
|
|
|
```
|
|
more_dots/
|
|
├── agents/ # 代理模块
|
|
│ ├── base_agent.py # 基础代理类
|
|
│ ├── conversation_agent.py # 对话代理
|
|
│ └── tool_agent.py # 工具使用代理
|
|
├── tools/ # 工具模块
|
|
│ ├── calculator.py # 计算器工具
|
|
│ └── web_search.py # 网络搜索工具(占位符)
|
|
├── workflows/ # 工作流管理
|
|
│ └── workflow_manager.py # 工作流管理器
|
|
├── examples/ # 使用示例
|
|
│ └── basic_usage.py # 基础用法示例
|
|
├── tests/ # 测试文件
|
|
│ └── test_basic.py # 基础测试
|
|
├── config.py # 配置文件
|
|
├── requirements.txt # 依赖包列表
|
|
├── .env.example # 环境变量示例
|
|
├── main.py # 主程序入口
|
|
└── README.md # 项目说明
|
|
```
|
|
|
|
## 快速开始
|
|
|
|
### 1. 安装依赖
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
### 2. 配置环境变量
|
|
|
|
```bash
|
|
# 复制环境变量文件
|
|
cp .env.example .env
|
|
|
|
# 编辑 .env 文件,设置你的 OpenAI API 密钥
|
|
OPENAI_API_KEY=your_openai_api_key_here
|
|
```
|
|
|
|
### 3. 运行示例
|
|
|
|
```bash
|
|
# 运行基础示例
|
|
python examples/basic_usage.py
|
|
|
|
# 运行交互式 CLI
|
|
python main.py
|
|
```
|
|
|
|
## 使用指南
|
|
|
|
### 基础用法
|
|
|
|
```python
|
|
from workflows.workflow_manager import WorkflowManager, WorkflowType
|
|
|
|
# 创建工作流管理器
|
|
manager = WorkflowManager()
|
|
|
|
# 使用对话工作流
|
|
result = manager.execute_workflow(
|
|
WorkflowType.CONVERSATION,
|
|
"Hello! How can you help me?"
|
|
)
|
|
|
|
# 使用工具工作流
|
|
result = manager.execute_workflow(
|
|
WorkflowType.TOOL_USING,
|
|
"Calculate 15 * 3 + 7"
|
|
)
|
|
```
|
|
|
|
### 自定义工具
|
|
|
|
创建新的工具类:
|
|
|
|
```python
|
|
from langchain_core.tools import BaseTool
|
|
|
|
class CustomTool(BaseTool):
|
|
name = "custom_tool"
|
|
description = "A custom tool for specific tasks"
|
|
|
|
def _run(self, input: str) -> str:
|
|
# 实现工具逻辑
|
|
return f"Processed: {input}"
|
|
```
|
|
|
|
### 扩展代理
|
|
|
|
创建新的代理类型:
|
|
|
|
```python
|
|
from agents.base_agent import BaseAgent
|
|
|
|
class CustomAgent(BaseAgent):
|
|
def _build_graph(self):
|
|
# 实现自定义图结构
|
|
pass
|
|
|
|
def _custom_node(self, state):
|
|
# 自定义节点逻辑
|
|
return state
|
|
```
|
|
|
|
## 工作流类型
|
|
|
|
| 工作流类型 | 描述 | 适用场景 |
|
|
|-----------|------|----------|
|
|
| `conversation` | 多轮对话代理 | 聊天机器人、客服系统 |
|
|
| `tool_using` | 工具使用代理 | 任务执行、数据分析 |
|
|
|
|
## 开发指南
|
|
|
|
### 添加新功能
|
|
|
|
1. **新工具**: 在 `tools/` 目录下创建新的工具类
|
|
2. **新代理**: 在 `agents/` 目录下继承 `BaseAgent` 类
|
|
3. **新工作流**: 在 `workflows/` 目录下扩展工作流管理器
|
|
|
|
### 测试
|
|
|
|
```bash
|
|
# 运行所有测试
|
|
python -m pytest tests/
|
|
|
|
# 运行特定测试
|
|
python -m pytest tests/test_basic.py
|
|
```
|
|
|
|
### 调试
|
|
|
|
项目使用标准的 Python 日志系统,可以通过设置环境变量启用调试模式:
|
|
|
|
```python
|
|
import logging
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
```
|
|
|
|
## 依赖项
|
|
|
|
主要依赖包:
|
|
|
|
- `langchain-core`: LangChain 核心功能
|
|
- `langchain`: LangChain 主包
|
|
- `langgraph`: LangGraph 图工作流
|
|
- `langchain-openai`: OpenAI 集成
|
|
- `python-dotenv`: 环境变量管理
|
|
- `pydantic`: 数据验证
|
|
|
|
## 许可证
|
|
|
|
MIT License
|
|
|
|
## 贡献
|
|
|
|
欢迎提交 Issue 和 Pull Request 来改进这个项目!
|
|
|