Skip to content

Chuanyin1202/trading-agents-api

Repository files navigation

TradingAgents API Server

🚀 現代化的 TradingAgents RESTful API 伺服器 - 將多代理 LLM 金融交易框架轉換為可擴展的 API 服務

Python FastAPI Redis Celery Docker Colab

📋 專案概述

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 配置

🤖 TradingAgents 整合

  • 無修改封裝: 通過封裝器模式整合原始代碼
  • 多代理管理: 支援分析師、研究員、交易員、風險管理員等多種代理
  • 配置管理: 靈活的配置系統支援多種 LLM 提供商
  • 進度追蹤: 實時追蹤代理執行狀態和分析進度

🛠️ 開發者友好

  • 自動 API 文檔: 集成 OpenAPI/Swagger 文檔
  • 類型安全: 完整的 Python 類型提示
  • 測試套件: 全面的單元測試和整合測試
  • 日誌系統: 結構化日誌和監控指標

快速開始

前置要求

  • Python 3.10+
  • Redis 6.0+
  • Git

1. 克隆專案

git clone [your-repo-url] trading-agents-api
cd trading-agents-api

2. 安裝依賴

# 創建虛擬環境
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

# 安裝依賴
pip install -r requirements.txt

3. 整合 TradingAgents 核心

# 克隆 TradingAgents 專案
git clone https://github.com/TauricResearch/TradingAgents.git ../TradingAgents

# 整合核心代碼
python scripts/copy_tradingagents.py ../TradingAgents . --symlink --verify

4. 配置環境

# 複製環境配置
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

5. 啟動服務

# 啟動 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

6. 驗證安裝

# 運行整合測試
python scripts/test_integration.py

# 測試 API 端點
curl http://localhost:8000/health

部署選項

🐳 Docker 部署

# 使用 Docker Compose
docker-compose up -d

# 檢查服務狀態
docker-compose ps

# 查看日誌
docker-compose logs -f

🔬 Colab 部署

  1. 在 Google Colab 中打開 colab/TradingAgents_API_Server.ipynb
  2. 依次執行筆記本中的儲存格
  3. 通過 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:8000

API 端點

核心 API

  • POST /api/v1/analysis - 提交分析任務
  • GET /api/v1/analysis/{task_id} - 查詢任務狀態
  • GET /api/v1/analysis/{task_id}/result - 獲取分析結果
  • DELETE /api/v1/analysis/{task_id} - 取消任務

監控 API

  • GET /api/v1/agents - 獲取所有代理
  • GET /api/v1/agents/{agent_id} - 獲取特定代理
  • GET /api/v1/agents/overview - 系統概覽
  • GET /api/v1/system/health - 系統健康檢查

WebSocket 端點

  • WS /ws/analysis/{task_id} - 實時進度更新
  • WS /ws/agents/{agent_id} - 代理狀態更新

完整 API 文檔

啟動伺服器後,可以通過以下 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/

貢獻

  1. Fork 專案
  2. 創建功能分支 (git checkout -b feature/amazing-feature)
  3. 提交更改 (git commit -m 'Add amazing feature')
  4. 推送到分支 (git push origin feature/amazing-feature)
  5. 創建 Pull Request

📚 文檔

📄 授權協議

本專案基於 MIT 授權協議。詳細信息請查看 LICENSE 文件。

🤝 致謝


讓 TradingAgents 在現代 API 架構中發揮更大潛力! 🚀

TradingAgents API Server v1.0.0 | 由 SuperClaude AI 助理開發

About

RESTful API for TradingAgents LLM framework | FastAPI + Celery

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors