Skip to content

wishfay/agentService

Repository files navigation

Agent Service Manager

基于 LangChain 和 FastAPI 的多智能体管理系统,支持 MCP (Model Context Protocol) 和多种 LLM 提供商。

🚀 特性

核心功能

  • 多智能体管理: 支持创建和管理多个独立的智能体实例
  • 独立配置: 每个智能体可配置独立的 LLM、系统提示词和参数
  • 智能体路由: 通过 agent_id 路由到不同的智能体
  • 生命周期管理: 启动、停止、创建、删除智能体

技术特性

  • 智能对话: 集成 LangChain Agent,支持工具调用和复杂推理
  • MCP 支持: 通过 Model Context Protocol 集成外部工具和服务
  • MCP Inspector: 基于 Vue3 的可视化 MCP 服务器测试工具
  • 多模型支持: 兼容 OpenAI API 格式的 LLM 提供商
  • 流式响应: 支持实时流式输出
  • RESTful API: 简洁易用的 HTTP 接口
  • 异步架构: 基于 FastAPI 的高性能异步处理

Web UI

  • 智能体管理界面: 创建、编辑、启动、停止智能体
  • 对话测试界面: 与不同智能体进行实时对话
  • MCP Inspector: 可视化 MCP 工具测试

📋 环境要求

  • Python 3.12+
  • Conda (推荐) 或 venv

🛠️ 安装

1. 克隆仓库

git clone https://github.com/wishfay/agentService.git
cd agentService

2. 创建 Conda 环境

conda create -n agent python=3.12 -y
conda activate agent

3. 安装依赖

使用 pip:

pip install -r requirements.txt

或使用 conda:

conda env update -f environment.yml

⚙️ 配置

1. 创建环境变量文件

cp .env.example .env

2. 配置 LLM

编辑 .env 文件,配置你的 LLM 提供商:

使用智谱 AI

OPENAI_API_BASE=https://open.bigmodel.cn/api/paas/v4/
OPENAI_API_KEY=your-zhipuai-api-key
MODEL_NAME=glm-4-flash

使用 Ollama (本地)

OPENAI_API_BASE=http://localhost:11434/v1
OPENAI_API_KEY=sk-dummy
MODEL_NAME=llama3.2

使用 OpenAI

OPENAI_API_BASE=https://api.openai.com/v1
OPENAI_API_KEY=your-openai-api-key
MODEL_NAME=gpt-4

3. 配置 MCP 服务器 (可选)

.env 文件中添加 MCP 服务器配置:

MCP_SERVERS__weather__transport=http
MCP_SERVERS__weather__url=http://localhost:8000/mcp

MCP_SERVERS__database__transport=stdio
MCP_SERVERS__database__command=python
MCP_SERVERS__database__args=["/path/to/mcp_server.py"]

🎯 运行

方式 1: 使用 Python 模块

PYTHONPATH=/path/to/agentService python -m app.main

方式 2: 使用启动脚本

chmod +x scripts/start.sh
./scripts/start.sh

服务将在 http://0.0.0.0:8000 启动。

后台运行

nohup python -m app.main > /tmp/agent_service.log 2>&1 &
echo $! > /tmp/agent_service.pid

停止服务

kill $(cat /tmp/agent_service.pid)

📡 API 使用

智能体管理

列出所有智能体

curl http://localhost:8080/agents/

响应:

{
  "agents": [
    {
      "agent_id": "default",
      "name": "通用助手",
      "description": "默认的通用对话助手",
      "status": "running",
      "message_count": 0,
      "created_at": "2026-01-28T23:18:17.165040",
      "last_used": null
    }
  ]
}

启动智能体

curl -X POST http://localhost:8080/agents/coder/start

停止智能体

curl -X POST http://localhost:8080/agents/coder/stop

创建新智能体

curl -X POST http://localhost:8080/agents/ \
  -H "Content-Type: application/json" \
  -d '{
    "agent_id": "translator",
    "name": "翻译助手",
    "description": "专业翻译助手",
    "llm": {
      "model": "glm-4-flash",
      "temperature": 0.3
    },
    "system_prompt": "你是一个专业的翻译助手。",
    "runtime": {
      "auto_start": false
    }
  }'

删除智能体

curl -X DELETE http://localhost:8080/agents/translator

聊天接口

与指定智能体对话

curl -X POST http://localhost:8080/chat/ \
  -H "Content-Type: application/json" \
  -d '{
    "message": "你好,请介绍一下你自己",
    "agent_id": "coder"
  }'

响应:

{
  "response": "我是一个专业的编程助手...",
  "session_id": "default",
  "agent_id": "coder",
  "tool_calls": []
}

使用默认智能体

curl -X POST http://localhost:8080/chat/ \
  -H "Content-Type: application/json" \
  -d '{"message": "你好"}'

健康检查

curl http://localhost:8080/health

查看可用工具

curl http://localhost:8000/tools/

Python 客户端示例

import requests

# 发送消息
response = requests.post(
    "http://localhost:8000/chat/",
    json={"message": "法国的首都是什么?"}
)
data = response.json()
print(data["response"])

流式响应示例

import requests
import json

response = requests.post(
    "http://localhost:8000/chat/stream/",
    json={"message": "介绍一下人工智能"},
    stream=True
)

for line in response.iter_lines():
    if line:
        data = json.loads(line)
        if data["type"] == "content":
            print(data["content"], end="", flush=True)

🎨 Web UI

启动 Web UI

1. 启动后端服务

python -m uvicorn app.main:app --host 0.0.0.0 --port 8080

2. 启动前端

cd web-ui
npm install
npm run dev

3. 访问界面

打开浏览器访问:http://localhost:3000

功能说明

  • 智能体管理: 创建、编辑、启动、停止和删除智能体

    • 可视化配置 LLM 参数
    • 自定义系统提示词
    • 查看智能体运行状态和统计信息
  • 对话测试: 与不同智能体实时对话

    • 选择运行中的智能体
    • 查看对话历史
    • 实时响应
  • MCP Inspector: 可视化 MCP 工具测试

    • 连接管理: 支持 HTTP、Stdio、SSE 传输方式
    • Resources: 浏览和读取服务器资源
    • Prompts: 测试提示模板生成
    • Tools: 调用工具并查看执行结果
    • Notifications: 实时查看服务器日志

智能体配置

智能体配置文件存放在 agents/ 目录:

agents/
├── default.yaml      # 默认通用助手
├── coder.yaml        # 编程助手
└── custom.yaml       # 自定义智能体

配置示例:

agent_id: my-agent
name: "我的智能体"
description: "智能体描述"

llm:
  model: "glm-4-flash"
  temperature: 0.7
  # 可选:覆盖全局配置
  # base_url: "https://api.example.com/v1"
  # api_key: "your-api-key"

system_prompt: |
  你是一个有帮助的助手。

runtime:
  auto_start: true  # 服务启动时自动启动

🏗️ 项目结构

agentService/
├── agents/                    # 智能体配置文件目录
│   ├── default.yaml          # 默认智能体配置
│   └── coder.yaml            # 编程助手配置
├── api/                      # API 路由
│   ├── routes/
│   │   ├── chat.py          # 聊天接口(支持多智能体)
│   │   ├── agents.py        # 智能体管理接口
│   │   ├── health.py        # 健康检查
│   │   └── tools.py         # 工具管理
├── app/                      # FastAPI 应用
│   ├── config.py            # 配置管理
│   ├── main.py              # 应用入口
│   └── schemas.py           # Pydantic 模型
├── core/                     # 核心逻辑
│   ├── agent.py             # 单智能体服务(向后兼容)
│   ├── agent_instance.py    # 智能体实例类
│   ├── agent_manager.py     # 多智能体管理器
│   ├── llm.py               # LLM 管理
│   └── mcp.py               # MCP 客户端
├── inspector/               # MCP Inspector 后端
│   ├── manager.py           # Inspector 管理器
│   └── routes.py            # Inspector API 路由
├── web-ui/            # Web UI (Vue3)
│   ├── src/
│   │   ├── api/            # API 客户端
│   │   │   ├── agents.ts   # 智能体 API
│   │   │   ├── chat.ts     # 聊天 API
│   │   │   └── inspector.ts
│   │   ├── components/     # Vue 组件
│   │   │   ├── AgentCard.vue
│   │   │   ├── AgentConfigDialog.vue
│   │   │   └── ...
│   │   ├── stores/         # Pinia 状态管理
│   │   │   ├── agents.ts
│   │   │   ├── chat.ts
│   │   │   └── inspector.ts
│   │   ├── types/          # TypeScript 类型
│   │   │   ├── agent.ts
│   │   │   └── inspector.ts
│   │   └── views/          # 页面组件
│   │       ├── AgentsView.vue
│   │       ├── ChatView.vue
│   │       └── ...
│   └── package.json
├── deployment/              # 部署配置
│   ├── agent-service.service
│   ├── supervisor.conf
│   └── README.md
├── tests/                  # 测试文件
├── scripts/                # 脚本
├── .env.example            # 环境变量示例
├── requirements.txt        # Python 依赖
├── environment.yml         # Conda 环境配置
└── README.md

🔧 开发

运行测试

pytest tests/

代码风格

项目使用 Python 类型注解和异步编程最佳实践。

📝 技术栈

后端:

  • Web 框架: FastAPI 0.128+
  • Agent 框架: LangChain 1.2+
  • LLM 集成: langchain-openai
  • MCP 支持: langchain-mcp-adapters
  • 数据验证: Pydantic 2.10+
  • ASGI 服务器: Uvicorn

前端 (Inspector UI):

  • 框架: Vue 3 + TypeScript
  • 构建工具: Vite 5+
  • UI 组件: Element Plus
  • 状态管理: Pinia
  • HTTP 客户端: Axios

🤝 贡献

欢迎提交 Issue 和 Pull Request!

📄 许可证

MIT License

🔗 相关资源

📧 联系方式

About

基于langchain集成的agent后端。

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors