# langgraph_multi_agent **Repository Path**: yaboliu/langgraph_multi_agent ## Basic Information - **Project Name**: langgraph_multi_agent - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master-dev - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-10-05 - **Last Updated**: 2025-10-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 🤖 LangGraph 多 Agent 协作系统 基于 **LangGraph** 构建的多 Agent 协作系统,包含 Supervisor 调度、数学 Agent 和诗歌 Agent。 ## 📁 项目结构 ``` langgraph_multi_agent/ ├── .env # 环境变量 ├── langgraph.json # LangGraph 配置 ├── workflow.py # 核心 workflow └── README.md # 本文档 ``` ## 🔧 安装依赖 ```bash pip install langgraph langchain-openai python-dotenv ``` ## ⚙️ 配置环境变量 创建 `.env` 文件并配置: ```env LLM_MODEL=gemini-2.5-flash-preview-05-20 LLM_TEMPERATURE=0.3 OPENAI_API_KEY=your_api_key_here OPENAI_API_BASE=https://x666.me/v1 LLM_MAX_TOKENS=65535 LANGGRAPH_PORT=2024 ``` ## 🚀 启动方式 ### 方式 1: 直接测试 Workflow ```bash python workflow.py ``` ### 方式 2: 启动 LangGraph Server ```bash langgraph dev --port 2024 ``` 服务将在 `http://localhost:2024` 启动 ## 🌐 访问前端 UI 浏览器访问: ``` http://localhost:2024 ``` ## 📡 API 接口 ### 1. 创建会话线程 ```bash curl -X POST http://localhost:2024/threads ``` 返回: ```json { "thread_id": "xxx-xxx-xxx" } ``` ### 2. 发送消息(流式返回) ```bash curl -X POST http://localhost:2024/threads/{thread_id}/runs/stream \ -H "Content-Type: application/json" \ -d '{ "assistant_id": "multi_agent", "input": { "messages": [ { "role": "user", "content": "计算 25 * 4" } ] }, "stream_mode": "values" }' ``` ### 3. 获取会话历史 ```bash curl http://localhost:2024/threads/{thread_id}/state ``` ## 🎯 Agent 说明 ### Supervisor (调度中心) - 分析用户问题 - 选择最合适的 Agent 处理 - 支持多轮对话 ### Math Agent (数学专家) - 数学计算 - 解题推理 - 公式推导 ### Poet Agent (诗人) - 诗歌创作 - 文学表达 - 情感抒发 ## 📊 工作流程 ``` 用户输入 ↓ Supervisor (决策) ↓ Math Agent / Poet Agent (执行) ↓ Supervisor (检查是否完成) ↓ 返回结果 / 继续循环 ``` ## 🧪 测试示例 ### 数学问题 **输入**: "计算 123 * 456" **流程**: 1. Supervisor → Math Agent 2. Math Agent 计算并返回结果 3. Supervisor → FINISH ### 诗歌创作 **输入**: "写一首关于春天的诗" **流程**: 1. Supervisor → Poet Agent 2. Poet Agent 创作诗歌 3. Supervisor → FINISH ## 🔍 调试技巧 ### 查看日志 ```bash langgraph dev --port 2024 --verbose ``` ### 测试单个 Agent ```python from workflow import graph from langchain_core.messages import HumanMessage result = graph.invoke({ "messages": [HumanMessage(content="你的问题")], "next": "" }) print(result["messages"][-1].content) ``` ## 🛠️ 扩展方式 ### 添加新 Agent 1. 在 `workflow.py` 中定义新的 agent 函数: ```python def create_translator_agent(model: ChatOpenAI): system_prompt = "你是一个专业翻译..." def translator_node(state: State): messages = state["messages"] agent_messages = [SystemMessage(content=system_prompt)] + messages response = model.invoke(agent_messages) return { "messages": [response], "next": "supervisor" } return translator_node ``` 2. 在 `create_workflow()` 中添加节点: ```python workflow.add_node("translator_agent", create_translator_agent(model)) ``` 3. 更新 Supervisor 的可用专家列表 4. 更新路由函数 ## 🐛 常见问题 ### Q1: 端口被占用 ```bash # 使用其他端口 langgraph dev --port 8080 ``` ### Q2: API Key 无效 检查 `.env` 文件中的 `OPENAI_API_KEY` 是否正确 ### Q3: 前端无法连接 确保: 1. LangGraph server 已启动 2. 端口号正确(默认 2024) 3. 浏览器访问 `http://localhost:2024` ## 📚 相关文档 - [LangGraph 官方文档](https://langchain-ai.github.io/langgraph/) - [LangChain 文档](https://python.langchain.com/) - [OpenAI API 文档](https://platform.openai.com/docs/) ## 📄 许可证 MIT License