MCTS (Monte Carlo Tree Search) + GPT-4o + Self Evolution for API discovering novel API Composition scenarios
An intelligent API composition system that uses MCTS (Monte Carlo Tree Search) + GPT-4o + Self Evolution to automatically discover and build novel API composition scenarios for business intelligence and data analysis tasks.
Clean 3-phase architecture showing MCTS exploration, Evolution optimization, and Self-Evolution for continuous improvement
π View Interactive Animation Demo
Watch the complete MCTS + Evolution process in action with a real financial dashboard example!
- GPT-4o Neural Guidance: Uses GPT-4o as both policy and value network for intelligent API selection
- MCTS Tree Search: Systematically explores API composition space with UCB1 selection
- Evolution Engine: Optimizes API chains through mutation and selection
- Real-time Visualization: Professional React frontend with live MCTS tree and evolution progress
- 25+ API Bank: Comprehensive set of APIs for data analysis, visualization, and reporting
- Type Safety: Automatic type compatibility checking and data flow validation
- WebSocket Communication: Real-time updates of composition progress
The system combines three powerful techniques:
- GPT-4o Neural Network: Provides semantic understanding and intelligent guidance
- MCTS Tree Search: Explores API composition space systematically
- Evolution Engine: Optimizes the best candidates through genetic algorithms
- Backend: FastAPI with WebSocket support
- Frontend: React with TypeScript, Tailwind CSS, and Framer Motion
- AI Integration: GPT-4o for policy and value networks
- Visualization: Real-time D3.js tree visualization and evolution progress
- Python 3.8+
- Node.js 16+
- OpenAI API key
- Git
git clone <repository-url>
cd mcts-evolution-coding-agent# Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements_api.txt
# Set up environment variables
cp .env.template .env
# Edit .env and add your OPENAI_API_KEYcd frontend
npm install
cd ..source venv/bin/activate
python api_composition_backend.pyThe backend will start on http://localhost:8000
cd frontend
npm run devThe frontend will start on http://localhost:3000
Navigate to http://localhost:3000 in your browser.
- Configure Parameters: Set MCTS iterations, evolution generations, and business objectives
- Start Composition: Click "Start Composition" to begin the intelligent API chain building
- Watch Progress: Observe real-time MCTS tree construction and evolution progress
- View Results: See the optimized API chain with type compatibility and data flow
- Business Intelligence Dashboard
- Financial Risk Analysis
- Market Research Report
- Competitive Analysis
- Investment Insights
- Iterations: Number of MCTS search iterations (5-30)
- Exploration Constant: UCB1 exploration parameter (0.5-2.0)
- Target Task: Business objective for API composition
- Generations: Number of evolution generations (5-20)
- Population Size: Size of candidate population (10-50)
- Mutation Rate: Probability of mutations (0.1-1.0)
- Model: gpt-4o or gpt-4o-mini
- Temperature: Creativity level (0.1-1.0)
- Max Tokens: Response length limit (100-2000)
The system includes 25+ APIs across 5 categories:
- Data Sources: News search, stock data, weather, company info
- Analysis: Sentiment analysis, trend analysis, entity extraction
- Processing: Text summarization, data filtering, normalization
- Visualization: Charts, heatmaps, timelines, plots
- Output: Reports, dashboards, alerts, presentations
- Selection: Choose nodes using UCB1 + GPT-4o guidance
- Expansion: Add new APIs based on GPT-4o suggestions
- Evaluation: Score API chains using GPT-4o value network
- Backpropagation: Update node statistics
- Population: Initialize with best MCTS candidates
- Mutation: Modify API chains (add/remove/replace APIs)
- Selection: Choose fittest candidates for next generation
- Optimization: Converge to optimal API compositions
- Live MCTS tree construction
- Evolution progress tracking
- API chain flow visualization
- Performance metrics dashboard
Think of building API compositions like making the perfect pizza:
- Tries different ingredient combinations systematically
- "Let me try pepperoni... now add mushrooms... now cheese..."
- GPT-4o guides: "Based on customer preferences, try adding basil next"
- Finds 5 good pizza recipes (API chains)
- Takes those 5 good pizzas and creates variations
- Makes small changes: "What if I add more cheese? Less sauce? Different crust?"
- Creates a population of 10 pizza variations
- Keeps the tastiest ones, discards the bad ones
- Repeats to make them even better over generations
MCTS Exploration:
Iteration 1: START β get_stock_data("AAPL")
Iteration 5: START β get_stock_data β calculate_risk
Iteration 10: START β get_stock_data β search_news β analyze_sentiment
Iteration 15: START β get_stock_data β calculate_risk β create_chart β generate_report
Evolution Optimization:
Generation 1: 5 different financial API chains
Best: Stock β Risk β Chart β Report (Score: 0.75)
Generation 3: Evolved variations
Better: Stock β News β Sentiment β Risk β Prediction β Dashboard (Score: 0.85)
Generation 5: Further optimized
Best: Stock β News β Social β Sentiment β Risk β Timeline β Alert β Dashboard (Score: 0.92)
The system keeps evolving forever by:
- Month 1: 25 APIs (basic financial)
- Month 6: 40 APIs (added social media)
- Month 12: 60 APIs (added crypto, ESG, supply chain)
- January: "Create fastest dashboard"
- June: "Optimize for accuracy"
- December: "Minimize costs"
- Start: Financial analysis
- Later: Healthcare, retail, manufacturing
- Future: Space exploration, quantum computing
- Evolve current chains (make them better) β
- Add new APIs when available (more ingredients) β
- Change goals when needs shift (new preferences) β
- Expand to new domains (new customers) β
- Repeat forever π
Generation 1: 5 API chains from MCTS
Chain A: News β Sentiment β Report
Chain B: Stock β Risk β Chart
Chain C: Weather β Analysis β Dashboard
Chain D: Company β Patents β Summary
Chain E: Social β Trends β Visualization
Evolution creates variations:
Chain A1: News β Sentiment β Summary β Report (added step)
Chain A2: News β Sentiment (removed report)
Chain B1: Stock β Risk β Chart β Dashboard (added dashboard)
Chain B2: Stock β News β Risk β Chart (added news)
Generation 2: Keep best 5, create 5 more variations
Generation 3: Keep best 5, create 5 more variations
...and the cycle continues infinitely!
Week 1: System creates financial dashboards
- Population: 10 different financial API chains
- Best:
Stock β Risk β Chart β Report
Week 10: Market crashes, need faster alerts
- Goal changes to "speed over accuracy"
- Population evolves:
Stock β Quick-Risk β Alert
Week 20: New crypto APIs become available
- System adds Bitcoin, Ethereum APIs
- Population evolves:
Stock β Crypto β Combined-Risk β Alert
Week 30: Users want social sentiment
- System adds Twitter, Reddit APIs
- Population evolves:
Stock β Crypto β Social β Sentiment β Risk β Alert
This creates a system that never stops discovering novel API composition scenarios!
The system ensures type compatibility through:
- Type Compatibility Graph: Maps valid type conversions
- Automatic Validation: Checks input/output type matching
- Data Flow Analysis: Ensures valid API chaining
Real-time communication endpoints:
ws://localhost:8000/ws/{session_id}- WebSocket connection- Message types:
mcts_update,evolution_generation,system_status
βββ api_composition_backend.py # Main FastAPI backend
βββ frontend/ # React frontend
β βββ src/
β β βββ components/ # React components
β β βββ hooks/ # Custom hooks
β β βββ store/ # Zustand state management
β β βββ types/ # TypeScript definitions
βββ architecture_diagram.svg # System architecture
βββ requirements_api.txt # Python dependencies
βββ README.md # This file
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- OpenAI for GPT-4o API
- FastAPI for the excellent web framework
- React and the amazing frontend ecosystem
- D3.js for powerful visualizations
For questions and support, please open an issue on GitHub.
Built with β€οΈ using GPT-4o, MCTS, and Evolution