🚀 現代化的 TradingAgents RESTful API 伺服器 - 將多代理 LLM 金融交易框架轉換為可擴展的 API 服務
TradingAgents API Server 是一個現代化的 RESTful API 伺服器,將原始的 TradingAgents 多代理 LLM 金融交易框架封裝為可擴展的 API 服務。
- 🔄 無侵入式整合: 保持原始 TradingAgents 代碼完整性
- 🚀 現代化架構: 使用 FastAPI + Celery + Redis 構建高性能 API
- 🌐 多平台支援: 支援本地、Docker、Colab 多種部署方式
- 📱 移動端準備: 為 Flutter 移動應用提供後端支援
- 🔍 實時監控: 提供完整的任務進度追蹤和代理監控
- 異步 API 伺服器: 基於 FastAPI 的高性能異步 Web 服務
- 分散式任務隊列: 使用 Celery 處理長時間運行的分析任務
- 實時通信: WebSocket 支援實時進度更新和狀態監控
- Redis 整合: 高效的緩存和訊息代理服務
- 容器化部署: 完整的 Docker 和 Docker Compose 配置
- 無修改封裝: 通過封裝器模式整合原始代碼
- 多代理管理: 支援分析師、研究員、交易員、風險管理員等多種代理
- 配置管理: 靈活的配置系統支援多種 LLM 提供商
- 進度追蹤: 實時追蹤代理執行狀態和分析進度
- 自動 API 文檔: 集成 OpenAPI/Swagger 文檔
- 類型安全: 完整的 Python 類型提示
- 測試套件: 全面的單元測試和整合測試
- 日誌系統: 結構化日誌和監控指標
- Python 3.10+
- Redis 6.0+
- Git
git clone [your-repo-url] trading-agents-api
cd trading-agents-api# 創建虛擬環境
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# 安裝依賴
pip install -r requirements.txt# 克隆 TradingAgents 專案
git clone https://github.com/TauricResearch/TradingAgents.git ../TradingAgents
# 整合核心代碼
python scripts/copy_tradingagents.py ../TradingAgents . --symlink --verify# 複製環境配置
cp .env.example .env
# 編輯配置檔案
nano .env必要的環境變數:
# API 金鑰
OPENAI_API_KEY=your-openai-api-key
FINNHUB_API_KEY=your-finnhub-api-key
# Redis 配置
REDIS_HOST=localhost
REDIS_PORT=6379
# 應用配置
APP_ENV=development
DEBUG=True# 啟動 Redis
redis-server
# 啟動 Celery Worker
celery -A tasks.celery_app worker --loglevel=info
# 啟動 API 伺服器
uvicorn api.main:app --reload --host 0.0.0.0 --port 8000# 運行整合測試
python scripts/test_integration.py
# 測試 API 端點
curl http://localhost:8000/health# 使用 Docker Compose
docker-compose up -d
# 檢查服務狀態
docker-compose ps
# 查看日誌
docker-compose logs -f- 在 Google Colab 中打開
colab/TradingAgents_API_Server.ipynb - 依次執行筆記本中的儲存格
- 通過 ngrok 獲取的 URL 訪問 API
# 使用生產環境配置
cp .env.production .env
# 啟動多個 Worker
celery -A tasks.celery_app worker --concurrency=4
# 使用 Gunicorn 啟動 API 伺服器
gunicorn api.main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000POST /api/v1/analysis- 提交分析任務GET /api/v1/analysis/{task_id}- 查詢任務狀態GET /api/v1/analysis/{task_id}/result- 獲取分析結果DELETE /api/v1/analysis/{task_id}- 取消任務
GET /api/v1/agents- 獲取所有代理GET /api/v1/agents/{agent_id}- 獲取特定代理GET /api/v1/agents/overview- 系統概覽GET /api/v1/system/health- 系統健康檢查
WS /ws/analysis/{task_id}- 實時進度更新WS /ws/agents/{agent_id}- 代理狀態更新
啟動伺服器後,可以通過以下 URL 查看完整的 API 文檔:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc - OpenAPI Schema:
http://localhost:8000/openapi.json
API 請求 → TradingService → Celery 任務 → TradingAgentsWrapper → 原始 TradingAgents
↓ ↓ ↓ ↓ ↓
WebSocket 推送 ← 進度更新 ← 任務狀態 ← 代理狀態 ← 分析進度
trading-agents-api/
├── api/ # API 層
│ ├── main.py # FastAPI 應用
│ ├── routers/ # API 路由
│ ├── models/ # 資料模型
│ └── services/ # 業務邏輯
├── core/ # 核心層
│ ├── trading_agents_wrapper.py # TradingAgents 封裝
│ ├── agents_manager.py # 代理管理
│ └── config.py # 配置管理
├── tasks/ # 任務層
│ ├── celery_app.py # Celery 應用
│ └── trading_tasks.py # 任務定義
├── scripts/ # 腳本工具
│ ├── test_integration.py # 整合測試
│ └── copy_tradingagents.py # 核心整合
├── colab/ # Colab 部署
│ ├── TradingAgents_API_Server.ipynb # 部署筆記本
│ └── setup_colab.py # Colab 設置
├── docs/ # 文檔
│ ├── API_INTEGRATION_GUIDE.md # 整合指南
│ ├── PROJECT_PROGRESS.md # 專案進度
│ └── ARCHITECTURE.md # 架構文檔
├── tests/ # 測試
├── docker-compose.yml # Docker 編排
├── Dockerfile # Docker 鏡像
└── requirements.txt # Python 依賴
# 運行所有測試
python -m pytest tests/ -v
# 運行整合測試
python scripts/test_integration.py
# 測試覆蓋率
python -m pytest --cov=api --cov=core --cov=tasks tests/- Fork 專案
- 創建功能分支 (
git checkout -b feature/amazing-feature) - 提交更改 (
git commit -m 'Add amazing feature') - 推送到分支 (
git push origin feature/amazing-feature) - 創建 Pull Request
本專案基於 MIT 授權協議。詳細信息請查看 LICENSE 文件。
- TradingAgents - 原始多代理交易框架
- FastAPI - 現代 Python Web 框架
- Celery - 分散式任務隊列
- Redis - 高性能緩存和訊息代理
讓 TradingAgents 在現代 API 架構中發揮更大潛力! 🚀
TradingAgents API Server v1.0.0 | 由 SuperClaude AI 助理開發