The system has been completely migrated from OpenAI to OpenRouter API. Here's what was changed:
-
LLM Integration Module (
src/llm_integration.py):- ✅ Replaced OpenAI client with direct HTTP requests to OpenRouter
- ✅ Updated all API calls to use OpenRouter endpoints
- ✅ Added proper OpenRouter headers (Authorization, HTTP-Referer, X-Title)
- ✅ Changed default model to
anthropic/claude-3-haiku(cost-effective)
-
Configuration Files:
- ✅ Updated
config/llm_config.yamlwith OpenRouter settings - ✅ Added multiple model options (Anthropic, OpenAI, Google, etc.)
- ✅ Included cost management and routing preferences
- ✅ Updated
-
Dependencies (
requirements.txt):- ✅ Removed
openai>=1.0.0andtiktoken>=0.5.0 - ✅ Added
requests>=2.31.0for HTTP API calls
- ✅ Removed
-
Demo and Main Scripts:
- ✅ Updated all references from OpenAI to OpenRouter
- ✅ Changed API key validation checks
- ✅ Updated installation instructions
Edit config/llm_config.yaml:
# Replace this line:
api_key: "your-openrouter-api-key-here"
# With your actual API key:
api_key: "sk-or-v1-your-actual-api-key-here"Set the environment variable:
# Linux/Mac
export OPENROUTER_API_KEY="sk-or-v1-your-actual-api-key-here"
# Windows Command Prompt
set OPENROUTER_API_KEY=sk-or-v1-your-actual-api-key-here
# Windows PowerShell
$env:OPENROUTER_API_KEY="sk-or-v1-your-actual-api-key-here"- Go to https://openrouter.ai/
- Click "Sign Up" or "Login"
- Create account with email/Google/GitHub
- Go to https://openrouter.ai/keys
- Click "Create Key"
- Give it a name (e.g., "Customer Behavior Simulation")
- Copy the key (starts with
sk-or-v1-)
- Go to https://openrouter.ai/credits
- Add $5-10 to start (very cost-effective)
- OpenRouter is typically 50-80% cheaper than direct OpenAI
model: "anthropic/claude-3-haiku"
# Cost: ~$0.25/1M input tokens, $1.25/1M output tokens
# Speed: Very fast
# Quality: Excellent for most tasksmodel: "openai/gpt-3.5-turbo"
# Cost: ~$0.50/1M input tokens, $1.50/1M output tokens
# Speed: Fast
# Quality: Good for complex tasksmodel: "anthropic/claude-3-sonnet"
# Cost: ~$3/1M input tokens, $15/1M output tokens
# Speed: Medium
# Quality: Excellent for complex analysismodel: "openai/gpt-4"
# Cost: ~$30/1M input tokens, $60/1M output tokens
# Speed: Slower
# Quality: Best for complex reasoning# config/llm_config.yaml
api_key: "sk-or-v1-your-key-here"
base_url: "https://openrouter.ai/api/v1"
model: "anthropic/claude-3-haiku"
temperature: 0.7
max_tokens: 2000# config/llm_config.yaml
api_key: "sk-or-v1-your-key-here"
base_url: "https://openrouter.ai/api/v1"
model: "anthropic/claude-3-haiku"
temperature: 0.7
max_tokens: 2000
site_url: "https://github.com/your-username/customer-behavior-sim"
site_name: "Customer Behavior Simulation"
# Task-specific models
task_models:
persona_generation:
model: "anthropic/claude-3-sonnet" # Better creativity
temperature: 0.8
max_tokens: 3000
insights_generation:
model: "anthropic/claude-3-haiku" # Cost-effective analysis
temperature: 0.3
max_tokens: 1500
# Cost management
cost_management:
max_monthly_spend: 25.0 # USD
alert_threshold: 20.0 # USD
track_usage: true# Run the demo to test LLM features
python demo_enhanced_features.pyfrom src.llm_integration import load_llm_config, LLMPersonaGenerator
# Load config
config = load_llm_config()
print(f"API Key configured: {config.api_key != 'your-openrouter-api-key-here'}")
print(f"Model: {config.model}")
# Test API call
generator = LLMPersonaGenerator(config)
# This will make a real API call if key is configured# Check your config file
cat config/llm_config.yaml | grep api_key
# Or check environment variable
echo $OPENROUTER_API_KEY- Verify your API key is correct
- Check if you have credits in your OpenRouter account
- Ensure key starts with
sk-or-v1-
- Reduce
requests_per_minutein config - Add delays between requests
- Upgrade your OpenRouter plan
- Check available models at https://openrouter.ai/models
- Verify model name spelling
- Some models require special access
# Install required dependencies
pip install requests>=2.31.0- Go to https://openrouter.ai/activity
- Monitor your usage and costs
- Set up alerts for spending limits
# The system logs all API calls
# Check logs for usage patterns
tail -f logs/enhanced_simulation_*.log | grep "API request"- Start with cheaper models (
claude-3-haiku) - Use task-specific models (different models for different tasks)
- Set spending limits in configuration
- Cache responses when possible
- Use lower temperatures for factual tasks (0.1-0.3)
- Batch requests when possible
- Use appropriate max_tokens (don't over-allocate)
- Implement retry logic with exponential backoff
- Monitor response times and switch models if needed
- Never commit API keys to version control
- Use environment variables in production
- Rotate keys regularly
- Monitor for unusual usage
Your system is now fully configured for OpenRouter! Here's what you can do:
# Edit the config file
nano config/llm_config.yaml
# Replace: api_key: "your-openrouter-api-key-here"
# With: api_key: "sk-or-v1-your-actual-key"# Run the demo
python demo_enhanced_features.py
# Or run the full enhanced simulation
python main_enhanced.py --enable-ai-insights- 🤖 AI Persona Generation: Create personas from market data
- 🧠 Intelligent Insights: Generate business insights from simulation results
- 📝 Natural Language Reports: Create comprehensive business reports
- 🔮 Trend Prediction: Predict future shopping behaviors
- 💰 Cost Savings: 50-80% cheaper than direct OpenAI
- 🔄 Model Variety: Access to Anthropic, Google, Meta, and more
- 🛡️ Reliability: Automatic failover between providers
- 📊 Unified API: One API for multiple LLM providers
- 🎯 Model Routing: Automatic selection of best model for task
- Persona Generation: Claude-3 excels at creative tasks
- Data Analysis: Multiple models for different analytical needs
- Cost Control: Essential for simulation workloads
- Scalability: Handle varying loads efficiently
The Customer Behavior Simulation System now uses OpenRouter for:
- ✅ AI-powered persona generation
- ✅ Intelligent business insights
- ✅ Natural language reporting
- ✅ Predictive analytics
Next Steps:
- Get your OpenRouter API key
- Update
config/llm_config.yaml - Run
python demo_enhanced_features.py - Explore the enhanced simulation features!
Need Help? Check the logs in logs/ directory or run the demo script for detailed error messages.