AIRouter
๐ AIRouter - ๆบ่ฝAI่ทฏ็ฑๅจ๏ผไธบๅคไธชLLMๆไพๅๆไพ็ปไธAPIๆฅๅฃ๏ผๆฏๆ่ด่ฝฝๅ่กกใๆ ้่ฝฌ็งปๅๆบ่ฝ่ทฏ็ฑ | Intelligent AI Router with unified API interface, load balancing, and smart routing for multiple LLM providers
Install / Use
/learn @THESIS-AGENT/AIRouterREADME
AIRouter
<div align="center">๐ Language / ่ฏญ่จ
๐จ๐ณ ไธญๆ็ | ๐บ๐ธ English Version
</div>
๐จ๐ณ ไธญๆ็
ไธไธชๆบ่ฝ็AI่ทฏ็ฑๅจ๏ผไธบๅคงๅ่ฏญ่จๆจกๅ(LLM)ๆไพ้ซๆง่ฝ็็ปไธAPIๆฅๅฃ๏ผๆฏๆๅคๆบ่ด่ฝฝๅ่กกๅๆบ่ฝๆ ้่ฝฌ็งปใ
โจ ไธป่ฆ็นๆง
- ๐ ็ปไธAPIๆฅๅฃ: ้่ฟ็ปไธ็ๆฅๅฃ่ฎฟ้ฎๅค็งLLMๆไพๅ๏ผOpenRouterใDeepInfraใDeerAPIใTogetherAIใGoogleใOpenAIใAnthropic็ญ๏ผ
- โก ๆบ่ฝ่ด่ฝฝๅ่กก: ๅบไบๅๅบๆถ้ดใๆๆฌๅๆๅ็็ๆบ่ฝ่ด่ฝฝๅ่กกไธๆ ้่ฝฌ็งป
- ๐ ๅฎๆถๅฅๅบท็ๆง: ่ชๅจ็ๆงAPIๅฅๅบท็ถๆๅๆง่ฝๆๆ
- ๐ ้ซๆง่ฝAPIๅฏ้ฅ็ฎก็: 100ๅๆง่ฝๆๅ็APIๅฏ้ฅ็ฎก็็ณป็ป๏ผๆฏๆๆบ่ฝๅคฑ่ดฅ้ฟๅ
- ๐ฏ ๅคๆจกๆๆฏๆ: ๆฏๆๆๆฌ็ๆใๅคๆจกๆ่พๅ ฅ๏ผๅพๅ+ๆๆฌ๏ผๅๅฝๆฐ่ฐ็จ
- ๐ ๅธ็ดฏๆๆไผ้ๆฉ: ไปๅคไธชๆจกๅไธญๆบ่ฝ้ๆฉๆไผๆจกๅ
- ๐ฐ ๆๆฌไผๅ: ๅฅๅบทๆฃๆฅๅฑ่ฝๅ่ฝ๏ผ้ฟๅ ๆ่ดตๆจกๅ็ไธๅฟ ่ฆๆฃๆฅ
- ๐ณ ๅฎนๅจๅ้จ็ฝฒ: ๅฎๆด็Dockerๆฏๆ๏ผๅผ็ฎฑๅณ็จ
๐๏ธ ็ณป็ปๆถๆ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ AIRouter โ โ LoadBalancing โ โ Health Monitor โ
โ (Core API) โโโโโบโ (ๆบ่ฝ่ทฏ็ฑ) โโโโโบโ (ๅฅๅบทๆฃๆฅ) โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Multiple LLM โ โ API Key Manager โ โ Performance โ
โ Providers โ โ (Service) โ โ Analytics โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
๐ฆ ๅฟซ้ๅผๅง
ๅฎ่ฃ
ๆนๅผไธ๏ผไฝไธบPythonๅ ๅฎ่ฃ ๏ผๆจ่๏ผ
git clone https://github.com/your-username/AIRouter.git
cd AIRouter
pip install -e .
ๆนๅผไบ๏ผ็ดๆฅๅฎ่ฃ ไพ่ต
git clone https://github.com/your-username/AIRouter.git
cd AIRouter
pip install -r requirements.txt
้ ็ฝฎ
1. ้ ็ฝฎAPIๅฏ้ฅ
cp ew_config/api_keys.example.py ew_config/api_keys_local.py
# ็ผ่พ ew_config/api_keys_local.py๏ผๅกซๅ
ฅๆจ็็ๅฎAPIๅฏ้ฅ
2. ๆฐๆฎๅบ่ฎพ็ฝฎ
CREATE DATABASE airouter;
CREATE TABLE api_key_usage (
request_id VARCHAR(50) PRIMARY KEY,
api_key VARCHAR(100) NOT NULL,
model_name VARCHAR(50) NOT NULL,
source_name VARCHAR(50) NOT NULL,
prompt_tokens INT,
completion_tokens INT,
create_time DATETIME NOT NULL,
finish_time DATETIME NOT NULL,
execution_time FLOAT NOT NULL,
status BOOLEAN NOT NULL
);
3. ็ฏๅขๅ้
# ๅคๅถ็ฏๅขๅ้้
็ฝฎๆไปถ
cp env.example .env
# ็ผ่พ .env ๆไปถ๏ผๅกซๅ
ฅๆจ็็ๅฎๆฐๆฎๅบไฟกๆฏ
# ๆณจๆ๏ผ่ฏท็กฎไฟ่ฎพ็ฝฎ DB_PASSWORD๏ผ่ฟๆฏๅฟ
้็็ฏๅขๅ้
ๅฏๅจๆๅก
Docker้จ็ฝฒ๏ผๆจ่๏ผ
# ๅฏๅจๆๅก
docker-compose up -d
# ๆฅ็ๆๅก็ถๆ
docker-compose ps
ๆๅจๅฏๅจ
# ๅฏๅจๅฅๅบทๆฃๆฅๆๅก
python CheckHealthy.py
# ๅจๅฆไธไธช็ป็ซฏๅฏๅจAPIๅฏ้ฅ็ฎก็ๆๅก
python -m api_key_manager.main
๐ ไฝฟ็จ็คบไพ
ๅบๆฌๆๆฌ็ๆ
from LLMwrapper import LLM_Wrapper
# ็ฎๅ็ๆๆฌ็ๆ
response = LLM_Wrapper.generate(
model_name="gpt4o_mini",
prompt="่งฃ้้ๅญ่ฎก็ฎ็ๅบๆฌๅ็"
)
print(response)
ๅคๆจกๆ่พๅ ฅ
import base64
# ๅพๅ+ๆๆฌ่พๅ
ฅ
with open("image.jpg", "rb") as f:
img_base64 = base64.b64encode(f.read()).decode()
response = LLM_Wrapper.generate_mm(
model_name="gpt4o_mini",
prompt="ๆ่ฟฐ่ฟๅผ ๅพ็็ๅ
ๅฎน",
img_base64=img_base64
)
print(response)
ๅฝๆฐ่ฐ็จ
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "่ทๅๅคฉๆฐไฟกๆฏ",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "ๅๅธๅ็งฐ"}
},
"required": ["location"]
}
}
}
]
response = LLM_Wrapper.function_calling(
model_name="gpt4o_mini",
prompt="ๅไบฌไปๅคฉๅคฉๆฐๅฆไฝ๏ผ",
tools=tools
)
print(response)
๐ง ้ซ็บง้ ็ฝฎ
่ด่ฝฝๅ่กก็ญ็ฅ
# ้
็ฝฎ่ด่ฝฝๅ่กกๆจกๅผ
response = LLM_Wrapper.generate(
model_name="gpt4o_mini",
prompt="ไฝ ๅฅฝ",
mode="cost_first" # ๅฏ้: fast_first, cost_first, balanced
)
ๅธ็ดฏๆๆไผ้ๆฉ
# ไปๅคไธชๆจกๅไธญ้ๆฉๆไผ็ปๆ
response = LLM_Wrapper.generate_fromTHEbest(
model_list=["gpt4o_mini", "claude35_sonnet", "gemini15_pro"],
prompt="ๅคๆๆจ็ไปปๅก"
)
๐ค ่ดก็ฎๆๅ
ๆไปฌๆฌข่ฟๆๆๅฝขๅผ็่ดก็ฎ๏ผ่ฏทๆฅ็ CONTRIBUTING.md ไบ่งฃ่ฏฆ็ปไฟกๆฏใ
๐ ่ฎธๅฏ่ฏ
่ฟไธช้กน็ฎไฝฟ็จ MIT ่ฎธๅฏ่ฏ - ๆฅ็ LICENSE ๆไปถไบ่งฃ่ฏฆ็ปไฟกๆฏใ
๐ ๆฏๆ
ๅฆๆๆจๅจไฝฟ็จ่ฟ็จไธญ้ๅฐ้ฎ้ข๏ผ่ฏท๏ผ
- ๆฅ็ GitHub Issues
- ๅๅปบๆฐ็ Issue ๆ่ฟฐๆจ็้ฎ้ข
๐บ๐ธ English Version
An intelligent AI router that provides high-performance unified API interfaces for Large Language Models (LLMs), supporting multi-source load balancing and intelligent failover.
โจ Key Features
- ๐ Unified API Interface: Access multiple LLM providers through a single interface (OpenRouter, DeepInfra, DeerAPI, TogetherAI, Google, OpenAI, Anthropic, etc.)
- โก Smart Load Balancing: Intelligent load balancing and failover based on response time, cost, and success rate
- ๐ Real-time Health Monitoring: Automatic monitoring of API health status and performance metrics
- ๐ High-performance API Key Management: 100x performance improvement in API key management with intelligent failure avoidance
- ๐ฏ Multimodal Support: Support for text generation, multimodal input (image + text), and function calling
- ๐ Pareto Optimal Selection: Intelligently select the optimal model from multiple options
- ๐ฐ Cost Optimization: Health check blacklist feature to avoid unnecessary checks on expensive models
- ๐ณ Containerized Deployment: Complete Docker support, ready to use out of the box
๐๏ธ System Architecture
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ AIRouter โ โ LoadBalancing โ โ Health Monitor โ
โ (Core API) โโโโโบโ (Smart Routing) โโโโโบโ (Health Check) โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Multiple LLM โ โ API Key Manager โ โ Performance โ
โ Providers โ โ (Service) โ โ Analytics โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
๐ฆ Quick Start
Installation
Method 1: Install as Python Package (Recommended)
git clone https://github.com/your-username/AIRouter.git
cd AIRouter
pip install -e .
Method 2: Install Dependencies Directly
git clone https://github.com/your-username/AIRouter.git
cd AIRouter
pip install -r requirements.txt
Configuration
1. Configure API Keys
cp ew_config/api_keys.example.py ew_config/api_keys_local.py
# Edit ew_config/api_keys_local.py and fill in your real API keys
2. Database Setup
CREATE DATABASE airouter;
CREATE TABLE api_key_usage (
request_id VARCHAR(50) PRIMARY KEY,
api_key VARCHAR(100) NOT NULL,
model_name VARCHAR(50) NOT NULL,
source_name VARCHAR(50) NOT NULL,
prompt_tokens INT,
completion_tokens INT,
create_time DATETIME NOT NULL,
finish_time DATETIME NOT NULL,
execution_time FLOAT NOT NULL,
status BOOLEAN NOT NULL
);
3. Environment Variables
# Copy environment variable configuration file
cp env.example .env
# Edit .env file and fill in your real database information
# Note: Make sure to set DB_PASSWORD, this is a required environment variable
Start Services
Docker Deployment (Recommended)
# Start services
docker-compose up -d
# Check service status
docker-compose ps
Manual Start
# Start health check service
python CheckHealthy.py
# Start API key manager service in another terminal
python -m api_key_manager.main
๐ Usage Examples
Basic Text Generation
from LLMwrapper import LLM_Wrapper
# Simple text generation
response = LLM_Wrapper.generate(
model_name="gpt4o_mini",
prompt="Explain the basic principles of quantum computing"
)
print(response)
Multimodal Input
import base64
# Image + text input
with open("image.jpg", "rb") as f:
img_base64 = base64.b64encode(f.read()).decode()
response = LLM_Wrapper.generate_mm(
model_name="gpt4o_mini",
prompt="Describe the content of this image",
img_base64=img_base64
)
print(response)
Function Calling
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather information",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}
}
]
response = LLM_Wrapper.function_calling(
model_name="gpt4o_mini",
prompt="What's the weather like in Beijing today?",
tools=tools
)
print(response)
๐ง Advanced Configuration
Load Balancing Strategies
# Configure load balancing mode
response = LLM_Wrapper.generate(
model_name="gpt4o_mini",
prompt="Hello",
mode="cost_first" # Options: fast_first, cost_first, balanced
)
Pareto Optimal Selection
# Select optimal results from multiple models
response = LLM_Wrapper.generate_fromTHEbest(
model_list=["gpt4o_mini", "claude35_sonnet", "gemini15_pro"],
prompt="Complex reasoning task"
)
๐ ๏ธ Development Guide
Project Structure
AIRouter/
โโโ LLMwrapper.py # Core API interface
โโโ LoadBalancing.py # Load balancing logic
โโโ CheckHealthy.py # Health check service
โโโ api_key_manager/ # API key management service
โโโ ew_config/ # Configuration files
โโโ ew_api/ # API infrastructure
โโโ ew_decorator/ # Decorator utilities
โโโ unit_test/
