内容
# 🤖 基于 A2A 协议的旅行智能助手
一个采用 **Agent2Agent (A2A)** 协议 + **MCP (Model Context Protocol)** 构建的多智能体旅行助手系统。系统通过总控中心进行意图识别与路由,由多个专业 Agent 协作完成**天气查询、票务查询、票务预定、景点推荐**等服务,支持 CLI 与 Streamlit Web 两种交互方式。
## ✨ 功能特性
- 🧭 **智能意图识别**:基于大语言模型(LLM)对用户输入进行意图识别,支持多意图组合(如"明天天气怎么样?顺便查一下去西安的机票")
- 🌤️ **天气查询**:基于真实天气数据,支持自然语言查询(城市 + 日期/相对时间词)
- 🚄 **票务查询**:火车票 / 机票 / 演唱会门票查询,支持票价、余票、车次等信息
- 🎫 **票务预定**:Agent 之间协作完成预定流程(先查余票 → 再执行预定)
- 🏞️ **景点推荐**:直接由 LLM 生成个性化的景点推荐(AIGC)
- 🔄 **多轮对话**:支持上下文记忆,自动对残缺查询进行改写补全
- 🧩 **A2A 协议**:Agent 之间通过标准 A2A 协议通信,自带 AgentCard 名片与技能声明
- 🛠️ **MCP 标准化**:数据访问能力以 MCP 工具形式暴露,与 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
```
## 🛠️ 技术栈
- **Agent 协议**:[A2A](https://github.com/a2aproject/A2A)(`python_a2a`)
- **工具协议**:[MCP](https://modelcontextprotocol.io)(`mcp` / FastMCP / `langchain-mcp-adapters`)
- **大语言模型**:LangChain(`langchain-openai`),通过 SiliconFlow 接入 Qwen2.5-72B-Instruct(可替换任意 OpenAI 兼容接口)
- **Web 界面**:Streamlit
- **数据库**:MySQL(`mysql-connector-python`)
- **数据源**:和风天气 API(30 天预报)
## 🚀 快速开始
### 1. 环境准备
```bash
# Python 3.9+
pip install streamlit langchain langchain-openai python-a2a mcp
pip install langchain-mcp-adapters mysql-connector-python requests schedule pytz
```
### 2. 初始化数据库
```bash
# 执行建库建表脚本
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 与数据库信息:
```python
# 大模型配置
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. 启动服务(按顺序)
```bash
# ① 启动 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 的独立测试脚本,启动对应服务后即可运行:
```bash
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
## 对应的博客:[]
连接信息
猜你喜欢
暂无推荐服务器