3.3 KiB
3.3 KiB
TradingAgents FastAPI Setup Guide
Overview
This guide explains how to run TradingAgents as a FastAPI server and integrate it with your Swift app.
Prerequisites
- Python 3.8+
- API keys for OpenAI (or other LLM providers)
- Swift/SwiftUI project with ReSwift
Python Server Setup
1. Install Dependencies
pip install -r requirements.txt
2. Set Environment Variables
Create a .env file in the project root:
# Required
OPENAI_API_KEY=your_openai_api_key_here
# Optional (defaults shown)
DEEP_THINK_MODEL=gpt-4o-mini
QUICK_THINK_MODEL=gpt-4o-mini
BACKEND_URL=https://api.openai.com/v1
3. Run the FastAPI Server
python run_api.py
The server will start at http://localhost:8000
4. Test the API
# Test health endpoint
curl http://localhost:8000/health
# Test analysis endpoint
curl -X POST http://localhost:8000/analyze \
-H "Content-Type: application/json" \
-d '{"ticker": "AAPL"}'
Swift Integration
1. Add ReSwift to Your Project
In Xcode, go to File → Add Package Dependencies and add:
https://github.com/ReSwift/ReSwift
2. Copy Swift Files
Copy these files to your Swift project:
TradingAgents_Swift_Integration.swift- Redux architecture and networkingTradingAnalysisView.swift- SwiftUI views
3. Update Your App
In your main app file, initialize the store and show the view:
import SwiftUI
import ReSwift
@main
struct YourApp: App {
var body: some Scene {
WindowGroup {
TradingAnalysisView()
}
}
}
4. Configure API URL
If running on a real device or different network, update the base URL in TradingAgentsAPIService:
private let baseURL = "http://your-server-ip:8000"
API Endpoints
POST /analyze
Analyzes a stock ticker.
Request:
{
"ticker": "AAPL"
}
Response:
{
"ticker": "AAPL",
"analysis_date": "2024-01-15",
"market_report": "Technical analysis...",
"sentiment_report": "Social sentiment analysis...",
"news_report": "Recent news analysis...",
"fundamentals_report": "Fundamental analysis...",
"investment_plan": "Research team recommendation...",
"trader_investment_plan": "Trading strategy...",
"final_trade_decision": "Final decision...",
"processed_signal": "BUY"
}
Production Deployment
Docker (Recommended)
Create a Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"]
Build and run:
docker build -t tradingagents-api .
docker run -p 8000:8000 --env-file .env tradingagents-api
Security Considerations
- Use HTTPS in production
- Add API authentication
- Implement rate limiting
- Validate and sanitize inputs
- Use environment-specific configurations
Troubleshooting
Common Issues
- Import errors: Ensure all dependencies are installed
- API key errors: Check your
.envfile - Connection refused: Verify the server is running and accessible
- CORS errors: Check CORS configuration matches your Swift app's needs
Performance Tips
- The analysis can take 30-60 seconds depending on the LLM models
- Consider implementing caching for repeated requests
- Use background processing for long-running analyses