Skip to content

berkayouzhan/amd_hackathon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Adaptive Model Dispatcher

Adaptive Model Dispatcher Logo

Adaptive Model Dispatcher Banner

AMD Developer Hackathon: ACT II β€” Track 1: General-Purpose AI Agent
A token-efficient, category-aware routing agent that intelligently processes batches of diverse AI tasks while minimizing token consumption.

Python 3.11 147 Tests Passed Docker Fireworks AI MIT License


⚑ How It Works

Adaptive Model Dispatcher routes each task through the most cost-effective path using a 5-layer pipeline:

graph LR
    A["πŸ“₯ Input<br/>tasks.json"] --> B["πŸ—œοΈ Compress<br/>0 tokens"]
    B --> C{"⚑ Tier 0<br/>Deterministic?"}
    C -->|Yes| H["πŸ“€ Output<br/>0 tokens"]
    C -->|No| D["🏷️ Triage<br/>Classify (8 cat)"]
    D --> E{"πŸ€– Model Call<br/>Route by category"}
    E -->|Default| F1["MiniMax M3<br/>$0.30/M"]
    E -->|Code| F2["Kimi K2P7<br/>$0.95/M"]
    E -->|Gemma bonus| F3["Gemma 4<br/>bonus prize"]
    F1 --> G["βœ… Validate"]
    F2 --> G
    F3 --> G
    G -->|Suspicious| I["πŸ”„ Retry<br/>1 attempt"]
    G -->|OK| J["🧹 Clean<br/>0 tokens"]
    I --> J
    J --> H
Loading

Pipeline Stages

Stage What it does Token cost
Compress Removes redundant whitespace, URLs, repeated punctuation (preserves code blocks) 0
Tier 0 Deterministic arithmetic, percentages, powers, unit conversions 0
Triage Two-layer classifier: regex heuristic (0 tokens) β†’ model fallback if uncertain 0 or ~100
Model Call Routes to optimal model based on category + role mapping varies
Validate Free deterministic checks catch empty/truncated/invalid responses 0
Clean Strips intro phrases, markdown fences, formats for LLM-Judge 0

🎯 Key Features

  • πŸ”‹ Zero-Token Solving β€” Arithmetic, percentages (15% of 200), unit conversions (5 km to m), temperature (100 Celsius to Fahrenheit), sqrt (sqrt(144)), simple discounts, square/cube solved without any API calls
  • 🏷️ Smart Triage β€” Two-layer classifier (regex heuristics + lightweight model fallback) categorizes tasks at near-zero cost
  • 🎯 Optimal Model Selection β€” Each of 8 categories is routed to the best-fit model
  • πŸ›‘οΈ Speculative Validation β€” Free deterministic checks catch empty/truncated/invalid responses, triggering corrective retries
  • ⚑ Gemma Circuit-Breaker β€” Time-based reset with max 2 failures before permanent shutdown
  • 🧠 Local Model Tier β€” Qwen 2.5 1.5B runs inside the container for sentiment/NER/summary at 0 Fireworks tokens
  • πŸ”’ Robust Batch Processing β€” Isolated error handling per task, atomic file writes, deadline-aware processing
  • πŸ“Š Monitoring Dashboard β€” Real-time dark-mode dashboard visualizing routing decisions, token usage, and pipeline flow

πŸ“‹ 8 Supported Task Categories

Category Model Role Routing Strategy max_tokens
Factual Knowledge default Local Qwen β†’ Gemma β†’ MiniMax M3 fallback 160
Mathematical Reasoning reasoning MiniMax M3 (concise step-by-step) 320
Sentiment Classification default Local Qwen β†’ Gemma β†’ MiniMax M3 48
Text Summarization default Local Qwen β†’ Gemma β†’ MiniMax M3 192
Named Entity Recognition default Local Qwen β†’ Gemma β†’ MiniMax M3 192
Code Debugging code Kimi K2P7 Code 768
Logical Reasoning reasoning MiniMax M3 384
Code Generation code Kimi K2P7 Code 768

πŸ“ Project Structure

optiroute-ai/
β”œβ”€β”€ config.py                  # Environment variables, model catalog, role assignment
β”œβ”€β”€ fireworks_client.py        # Fireworks API wrapper β€” ALLOWED_MODELS guard, token tracking
β”œβ”€β”€ deterministic_solver.py    # Tier 0 β€” arithmetic, percentages, unit conversions (0 tokens)
β”œβ”€β”€ triage.py                  # 8-category classification (heuristic + model fallback)
β”œβ”€β”€ prompt_compressor.py       # Whitespace/URL/punctuation compression (preserves code)
β”œβ”€β”€ validator.py               # Free, deterministic response validation
β”œβ”€β”€ answer_cleaner.py          # Post-processing: strips intro phrases, markdown fences
β”œβ”€β”€ local_model.py             # In-container Qwen 2.5 1.5B inference (0 Fireworks tokens)
β”œβ”€β”€ router.py                  # AdaptiveDispatcher β€” the main orchestrator
β”œβ”€β”€ main.py                    # Harness entry point β€” batch I/O + run_report generation
β”œβ”€β”€ fake_fireworks_client.py   # Offline fake client for local testing
β”œβ”€β”€ dashboard/                 # Monitoring dashboard (HTML/CSS/JS)
β”‚   β”œβ”€β”€ index.html             # Dark-mode glassmorphism dashboard
β”‚   β”œβ”€β”€ style.css              # Premium styling with animations
β”‚   └── app.js                 # Chart.js visualizations + data loading
β”œβ”€β”€ local_test/                # Sample task sets for development
β”‚   β”œβ”€β”€ tasks.json             # 8 tasks (one per category)
β”‚   └── tasks_challenge.json   # 15 challenging edge-case tasks
β”œβ”€β”€ tests/                     # pytest suite (147 tests, no real API calls)
β”œβ”€β”€ Dockerfile                 # Multi-stage build (Python 3.11 + Qwen model)
└── requirements.txt           # openai, python-dotenv, tenacity, llama-cpp-python

πŸš€ Setup

python -m venv venv
source venv/bin/activate   # Windows: venv\Scripts\activate
pip install -r requirements-dev.txt

cp .env.example .env
# Fill .env with your Fireworks API key:
#   FIREWORKS_API_KEY=fw_...

πŸ§ͺ Running Tests

pytest tests/ -v

All 147 tests run with mocked FireworksClient β€” none connect to the real Fireworks API, so they work without an API key and complete in under 2 seconds.

πŸ–₯️ Offline End-to-End Test (no API key needed)

export TASKS_INPUT_PATH=./local_test/tasks.json
export RESULTS_OUTPUT_PATH=./local_test/results.json
export USE_FAKE_FIREWORKS=1
python main.py
cat local_test/results.json

This mode uses fake_fireworks_client.py to run the entire pipeline without any real API calls β€” useful for verifying the pipeline doesn't crash and routes correctly.

πŸ“Š Dashboard

After running main.py, a run_report.json is generated alongside results.json. To view the monitoring dashboard:

# Start a local server from the project root
python -m http.server 8000

# Open in browser
# http://localhost:8000/dashboard/index.html

You can also drag-and-drop any run_report.json onto the dashboard page.

🐳 Docker Build & Submission

# Standard build (Intel/AMD machines):
docker build --tag <your-image>:latest .

# Apple Silicon β€” MUST specify platform:
docker buildx build --platform linux/amd64 --tag <your-image>:latest --push .

Pre-submission Checklist

  • Image uses linux/amd64 manifest
  • Image size ≀ 10GB
  • .env file is NOT copied into the image (.dockerignore prevents this)
  • 147 automated tests passing
  • Gemma circuit-breaker with time-based reset
  • Local model (Qwen 2.5) for 0-token inference
  • Monitoring dashboard included
  • Run report auto-generated

πŸ—οΈ Architecture Decisions

Decision Rationale
Gemma "try first, fallback silently" Gemma bonus prize opportunity, but not serverless on Fireworks
Circuit-breaker with time-based reset Prevents permanent Gemma shutdown on transient errors
Local Qwen model for light categories 0 Fireworks tokens for sentiment/NER/summarization
Corrective retry on same model No clear model ladder in Track 1; different model adds complexity
temperature=0.0 default Consistency > creativity for a deterministic router
Tier 0 limited to safe patterns Wrong guess on word problems risks accuracy gate elimination
Atomic writes (tmp + os.replace) Half-written results.json = zero score
Category-tuned max_tokens Prevents token waste (e.g., sentiment needs only 48 tokens)

πŸ› οΈ Technologies

  • Language: Python 3.11
  • AI Platform: Fireworks AI
  • Models: MiniMax M3, Kimi K2P7 Code, Google Gemma 4, Qwen 2.5 (local)
  • Containerization: Docker (multi-stage build)
  • Testing: pytest (147 tests)
  • Dashboard: HTML/CSS/JS + Chart.js

πŸ“„ License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors