multi-agent README

The project's README from github.com/phoenix-zhou/multi-agent. As one markdown file: README.md.

🤖 基于 A2A 协议的旅行智能助手

一个采用 Agent2Agent (A2A) 协议 + MCP (Model Context Protocol) 构建的多智能体旅行助手系统。系统通过总控中心进行意图识别与路由,由多个专业 Agent 协作完成天气查询、票务查询、票务预定、景点推荐等服务,支持 CLI 与 Streamlit Web 两种交互方式。

✨ 功能特性

🏗️ 系统架构

┌─────────────────────────────────────────────────────────────┐
│                    总控中心 (Orchestrator)                    │
│        app.py (Streamlit Web)  /  main.py (CLI)             │
│    - 意图识别 (Intent Agent, LLM)                            │
│    - 查询改写 (Query Rewriting)                              │
│    - 结果总结 (Summarization, LLM)                          │
└──────────────┬──────────────────────┬───────────────────────┘
               │ A2A 协议             │ A2A 协议
               ▼                      ▼
   ┌───────────────────────┐  ┌───────────────────────────┐
   │   A2A Agent 层         │  │                           │
   │  WeatherQueryAssistant│  │  TicketOrderAssistant      │
   │  (端口 5005)           │  │  (端口 5007)               │
   │  TicketQueryAssistant │  │   └─ 调用 TicketQuery...   │
   │  (端口 5006)           │  │      (Agent 间协作)        │
   └───────┬───────────┬───┘  └───────────┬───────────────┘
           │ MCP       │ MCP             │ MCP
           ▼           ▼                 ▼
   ┌─────────────┐ ┌────────────┐ ┌───────────────┐
   │ MCP Server 层│ │            │ │               │
   │WeatherTools │ │TicketTools │ │ OrderTools    │
   │ (端口 8002) │ │(端口 8001) │ │ (端口 8003)   │
   │query_weather│ │query_tickets│ │order_train/    │
   │             │ │            │ │order_flight/   │
   │             │ │            │ │order_concert   │
   └──────┬──────┘ └──────┬─────┘ └──────┬────────┘
          │               │              │
          ▼               ▼              ▼
        ┌────────────────────────────────────┐
        │           MySQL 数据库 travel_rag    │
        │  weather_data / train_tickets /    │
        │  flight_tickets / concert_tickets  │
        └────────────────────────────────────┘

处理流程

用户输入 → 意图识别(LLM) → 路由到对应 Agent
                              ├─ out_of_scope / 追问 → 直接回复
                              ├─ attraction → LLM 直接生成景点推荐
                              └─ weather/flight/train/concert/order
                                    → A2A Agent 生成 SQL (LLM)
                                    → 调用 MCP 工具查询数据库
                                    → 返回结果 → 总控中心 LLM 总结 → 用户

端口规划

服务 组件 端口
A2A Agent WeatherQueryAssistant(天气查询) 5005
A2A Agent TicketQueryAssistant(票务查询) 5006
A2A Agent TicketOrderAssistant(票务预定) 5007
MCP Server WeatherTools(天气工具) 8002
MCP Server TicketTools(票务工具) 8001
MCP Server OrderTools(预定工具) 8003
Web UI Streamlit 应用 8501(默认)

📁 目录结构

multi-agent/
├── app.py                     # Streamlit Web 入口(总控中心)
├── main.py                    # CLI 命令行入口(总控中心)
├── main_prompts.py            # 提示词模板(意图识别/结果总结/景点推荐)
├── config.py                  # 全局配置(LLM / 数据库 / 意图映射)
├── create_logger.py           # 日志配置
│
├── a2a_server/                # A2A 协议 Agent 服务层
│   ├── weather_server.py      # 天气查询 Agent(端口 5005)
│   ├── ticket_server.py       # 票务查询 Agent(端口 5006)
│   └── order_server.py        # 票务预定 Agent(端口 5007)
│
├── mcp_server/                # MCP Server 数据服务层
│   ├── mcp_weather_server.py  # 天气 MCP(端口 8002)
│   ├── mcp_ticket_server.py   # 票务 MCP(端口 8001)
│   └── mcp_order_server.py    # 预定 MCP(端口 8003)
│
├── query_data/
│   └── query_ticket.py        # 票务数据库查询服务
│
├── utils/
│   ├── format.py              # JSON 日期/Decimal 序列化工具
│   └── spider_weather.py      # 和风天气数据爬虫(定时更新天气库)
│
├── sql/
│   ├── sql_data.sql           # 建库建表 DDL
│   └── insert2.sql            # 票务示例数据
│
└── test/                      # 各服务测试脚本
    ├── test_weather_mcp_server.py
    ├── test_ticket_mcp_server.py
    ├── test_order_mcp_server.py
    ├── test_weather_agent_server.py
    ├── test_ticket_agent_server.py
    └── test_order_agent_server.py

🛠️ 技术栈

🚀 快速开始

1. 环境准备

# Python 3.9+
pip install streamlit langchain langchain-openai python-a2a mcp
pip install langchain-mcp-adapters mysql-connector-python requests schedule pytz

2. 初始化数据库

# 执行建库建表脚本
mysql -u root -p < sql/sql_data.sql

# 导入票务示例数据
mysql -u root -p travel_rag < sql/insert2.sql

如需导入真实天气数据,运行 python utils/spider_weather.py(需配置和风天气 API Key)。

3. 修改配置

编辑 config.py,配置你的 LLM 与数据库信息:

# 大模型配置
self.base_url = 'https://api.siliconflow.cn/v1'
self.api_key = 'sk-xxx'               # 替换为你的 API Key
self.model_name = 'Qwen/Qwen2.5-72B-Instruct'

# 数据库配置
self.host = 'localhost'
self.user = 'root'
self.password = '123456'
self.database = 'travel_rag'

4. 启动服务(按顺序)

# ① 启动 MCP Server(数据层)
python mcp_server/mcp_weather_server.py   # 端口 8002
python mcp_server/mcp_ticket_server.py    # 端口 8001
python mcp_server/mcp_order_server.py     # 端口 8003

# ② 启动 A2A Agent(智能体层)
python a2a_server/weather_server.py       # 端口 5005
python a2a_server/ticket_server.py        # 端口 5006
python a2a_server/order_server.py         # 端口 5007

# ③ 启动总控中心(二选一)
python main.py                            # CLI 交互版
streamlit run app.py                      # Web 聊天版

💬 使用示例

> 成都明天天气怎么样
> 帮我查一下明天成都到西安的高铁票
> 7月15号从成都到西安的机票还有吗
> 帮我订一张明天成都到西安的二等座火车票
> 去西安有什么好玩的景点推荐
> 明天天气怎么样 + 顺便看看去广州的机票    # 多意图组合

CLI 版输入 cards 可查看所有 Agent 的卡片信息(技能/描述/地址),输入 quit 退出。

🔑 核心设计说明

  1. 意图识别与查询改写(main_prompts.py):总控中心将用户输入收敛为 weather / flight / train / concert / order / attraction / out_of_scope 等固定意图;结合对话历史将残缺查询(如"那明天呢?")改写为完整明确的句子,使下游 Agent 无需关心上下文。

  2. SQL 生成 Agent(a2a_server/):下游 Agent 通过 LLM 将自然语言转换为 SQL,并将数据库 Schema 作为提示词上下文。信息不足时返回 input_required 追问用户,无关提问则拒绝回答。

  3. Agent 间协作(order_server.py):票务预定 Agent 先调用票务查询 Agent 确认余票,再将余票信息拼接后交由 MCP 预定工具执行,体现了多 Agent 协作流程。

  4. MCP 解耦:数据库访问统一封装为 MCP 工具(query_weather / query_tickets / order_train 等),Agent 通过 HTTP 流式调用,数据层与智能体层彻底解耦,便于扩展。

🧪 测试

test/ 目录下提供了各 MCP Server 与 A2A Agent 的独立测试脚本,启动对应服务后即可运行:

python test/test_weather_mcp_server.py
python test/test_weather_agent_server.py
python test/test_ticket_mcp_server.py
python test/test_ticket_agent_server.py
python test/test_order_mcp_server.py
python test/test_order_agent_server.py

📄 License

MIT

对应的博客:[]

multi-agent (phoenix-zhou/multi-agent)

Searching Database…

这是什么?这是一个多智能体旅行助手,使用 Agent2Agent(A2A)协议和 Model Context Protocol(MCP),让专门化的智能体协同完成天气查询、火车/航班/演出票查询、订票和景点推荐。
这是我导入的库,还是一个运行中的系统?它是一个自托管的多智能体系统:中央调度器把你的请求路由到专门化的 A2A 智能体(天气、票务查询、票务下单),这些智能体再调用 MCP 工具服务器获取数据。你要把它作为一组服务来运行,而不是作为库导入。
怎么让它跑起来?准备好环境,初始化 MySQL 数据库,在 config.py 中填入你的 LLM 和数据库设置,然后按顺序启动每个 A2A 智能体和 MCP 服务器,最后启动 CLI 或 Streamlit 网页应用。
除了 Python,运行它之前我还需要什么?你需要一个 MySQL 数据库和一个 OpenAI 兼容的 LLM API 密钥——项目默认通过 LangChain 使用 SiliconFlow 上的 Qwen2.5-72B-Instruct——另外可选地需要一个和风天气 (QWeather) API 密钥来获取真实的 30 天天气预报。
在本地运行这个有什么麻烦或限制?需要在不同的端口上运行六个独立的服务(三个 A2A 智能体、三个 MCP 服务器,外加网页 UI),并且必须按正确的顺序启动;意图识别仅限于固定的类别集合(weather/flight/train/concert/order/attraction/outofscope)。
我能通过浏览器使用它,还是只能在终端里?两者都可以:它自带 CLI 客户端和 Streamlit 网页聊天界面,后者默认运行在 8501 端口。
A2A AgentCard 是什么?每个智能体都公开一份标准的 A2A AgentCard,列出它的名称、描述和技能,这样其他智能体和中央编排器就能通过 A2A 协议发现并调用它。
系统如何处理“那明天呢?”这类追问?它会保存对话历史,并使用 LLM 把不完整的追问改写成完整的独立句子,再路由给正确的智能体,因此下游智能体不需要自己跟踪上下文。
订票在多个智能体之间实际是怎么运作的?订票智能体先调用票务查询智能体确认是否有余票,然后把结果传给 MCP 订票工具下单——这是智能体间协作的一个具体例子。
我可以更换 LLM 或天气数据源吗?可以,LLM 默认通过 SiliconFlow 使用 Qwen2.5-72B-Instruct,但任何 OpenAI 兼容的端点都可以;天气数据可以通过用你自己的 QWeather API 密钥运行自带的 spiderweather.py 爬虫来刷新。
Size
0.1 MB
Stars
490
Updated
CommitAug 11
Age
2mo
Commits · 90 Days