多智能体财务决策支持系统
使用谷歌的代理开发工具包(ADK)构建的全面财务决策支持系统。该系统演示了多种高级代理概念,以帮助用户做出明智的财务决策。
🎯 已实施的概念
该项目实施 8个关键概念 从ADK框架中:
1.多代理系统
- LLM授权代理:不同金融领域的专业代理
- 并行代理:从多个源同时获取数据
- 顺序代理:分析管道的有序工作流程
- 循环代理:逐步完善建议
2.工具
- 自定义工具:财务计算、风险评估、投资组合分析
- MCP集成:可扩展工具生态系统的模型上下文协议
- 内置工具:谷歌搜索市场新闻和研究
3.会话和记忆
- 内存会话服务:对话的会话状态管理
- 长期记忆:用户偏好和投资组合历史存储
- 状态管理:跟踪分析进度和用户上下文
4.上下文工程
- 上下文压缩:高效处理大型金融数据集
- 状态密钥模板:动态提示注射,实现个性化
5.可观察性
- 日志记录:整个系统的全面日志记录
- 追踪:基于OpenTetry的分布式跟踪
- 指标:性能和使用指标收集
6.代理评估
- 评判集:代理行为的预定义测试用例
- 刀具轨迹评分:验证工具的正确使用
- 响应质量指标:建议质量评估
7.A2A协议
- 代理卡:远程代理的能力发现
- 代理间通信:标准化的代理间消息传递
8.代理部署
- FastAPI集成:生产就绪的API服务器
- 云运行就绪:容器化部署支持
🏗️ 建筑
┌─────────────────────────────────────────────────────────────────┐
│ Financial Advisor (Root Agent) │
│ Orchestrates all sub-agents │
└─────────────────────────────────────────────────────────────────┘
│
┌───────────────────────┼───────────────────────┐
│ │ │
▼ ▼ ▼
┌───────────────┐ ┌───────────────────┐ ┌───────────────────┐
│ Research │ │ Analysis │ │ Recommendation │
│ Agent │ │ Pipeline │ │ Agent │
│ (Parallel) │ │ (Sequential) │ │ (Loop) │
└───────────────┘ └───────────────────┘ └───────────────────┘
│ │ │
┌────┼────┐ ┌─────┼─────┐ ┌─────┼─────┐
│ │ │ │ │ │ │ │ │
▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼
┌────┐┌────┐┌────┐ ┌─────┐┌─────┐┌─────┐ ┌─────┐┌─────┐┌─────┐
│Mkt ││News││Econ│ │Risk ││Port ││Perf │ │Gen ││Crit ││Ref │
│Data││Res ││Ind │ │Anal ││Anal ││Eval │ │Rec ││Eval ││ine │
└────┘└────┘└────┘ └─────┘└─────┘└─────┘ └─────┘└─────┘└─────┘📁 项目结构
financial_agent_system/
├── README.md
├── requirements.txt
├── .env.example
├── __init__.py
├── agent.py # Main agent definition (root_agent)
├── config.py # Configuration settings
│
├── agents/ # Sub-agent definitions
│ ├── __init__.py
│ ├── research_agent.py # Parallel research agents
│ ├── analysis_agent.py # Sequential analysis pipeline
│ └── recommendation_agent.py # Loop-based recommendation refiner
│
├── tools/ # Custom tools
│ ├── __init__.py
│ ├── market_tools.py # Market data tools
│ ├── portfolio_tools.py # Portfolio analysis tools
│ ├── risk_tools.py # Risk assessment tools
│ └── calculation_tools.py # Financial calculations
│
├── mcp/ # MCP Server integration
│ ├── __init__.py
│ └── financial_mcp_server.py # MCP server for financial tools
│
├── memory/ # Memory and session management
│ ├── __init__.py
│ └── memory_service.py # Custom memory implementation
│
├── observability/ # Logging, tracing, metrics
│ ├── __init__.py
│ ├── logging_config.py # Logging setup
│ ├── tracing.py # OpenTelemetry tracing
│ └── metrics.py # Metrics collection
│
├── evaluation/ # Agent evaluation
│ ├── __init__.py
│ ├── financial_advisor_eval.evalset.json
│ └── test_agents.py # pytest evaluation tests
│
├── a2a/ # A2A Protocol support
│ ├── __init__.py
│ ├── agent_card.json # Agent capability card
│ └── a2a_server.py # A2A server wrapper
│
└── deployment/ # Deployment configurations
├── Dockerfile
├── docker-compose.yml
└── deploy_api.py # FastAPI deployment🚀 入门指南
先决条件
- Python 3.10+
- 谷歌云项目(适用于Gemini API)
- 金融数据提供商的API密钥(可选)
安装
- 克隆存储库:
cd financial_agent_system- 创建虚拟环境:
python -m venv .venv
source .venv/bin/activate # Linux/Mac
# or .venv\Scripts\activate # Windows- 安装依赖项:
pip install -r requirements.txt- 配置环境:
cp .env.example .env
# Edit .env with your API keys运行代理
开发UI:
adk web .API服务器:
adk api_server .使用A2A协议:
adk api_server . --a2a💡 使用示例
基本财务查询
User: "Should I invest in tech stocks right now?"
Agent: [Researches market conditions, analyzes risk, provides recommendation]投资组合分析
User: "Analyze my portfolio: 40% AAPL, 30% GOOGL, 20% MSFT, 10% cash"
Agent: [Calculates metrics, assesses diversification, suggests rebalancing]风险评估
User: "What's my risk exposure if the market drops 20%?"
Agent: [Runs stress tests, calculates VaR, provides risk mitigation strategies]🧪 运行评估
通过CLI:
adk eval . evaluation/financial_advisor_eval.evalset.json通过pytest:
pytest evaluation/test_agents.py -v通过Web UI:
- 跑
adk web . - 导航到Eval选项卡
- 负载评估集
- 运行评估
📊 可观测性
查看痕迹
- 本地:使用ADK Web UI的跟踪查看器
- 云:使用启用云跟踪
--trace_to_cloud
指标
- 代理响应延迟
- 工具调用成功率
- 内存使用统计
日志记录
日志被写入 logs/financial_agent.log 具有可配置的级别。
🔒 安全考虑
- 存储在环境变量中的API密钥
- 所有财务计算的输入验证
- 对外部API调用的速率限制
- 不存储敏感财务数据
