一个智能的AI路由器,为大型语言模型(LLM)提供高性能的统一API接口,支持多源负载均衡和智能故障转移。
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ 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 文件了解详细信息。
如果您在使用过程中遇到问题,请:
An intelligent AI router that provides high-performance unified API interfaces for Large Language Models (LLMs), supporting multi-source load balancing and intelligent failover.
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ AIRouter │ │ LoadBalancing │ │ Health Monitor │
│ (Core API) │◄──►│ (Smart Routing) │◄──►│ (Health Check) │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Multiple LLM │ │ API Key Manager │ │ Performance │
│ Providers │ │ (Service) │ │ Analytics │
└─────────────────┘ └──────────────────┘ └─────────────────┘
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
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
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
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)
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)
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)
# Configure load balancing mode
response = LLM_Wrapper.generate(
model_name="gpt4o_mini",
prompt="Hello",
mode="cost_first" # Options: fast_first, cost_first, balanced
)
# Select optimal results from multiple models
response = LLM_Wrapper.generate_fromTHEbest(
model_list=["gpt4o_mini", "claude35_sonnet", "gemini15_pro"],
prompt="Complex reasoning task"
)
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/ # Test suite
ew_config/source.pyew_config/api_keys.pyew_api/LoadBalancing.py# Run basic tests
python unit_test.py
# Run complete test suite
python unit_test/run_all_tests.py
# Run API key manager tests
python -m api_key_manager.unit_test
docker build -t airouter:latest .
Create .env file:
DB_HOST=host.docker.internal
DB_USER=root
DB_PASSWORD=your_password_here
DB_NAME=airouter
DB_PORT=3306
docker-compose up -d
http://localhost:8001/check_healthyhttp://localhost:8002/check_healthyhttp://localhost:8001/docker-health# View service logs
docker-compose logs -f airouter-health-check
docker-compose logs -f airouter-key-manager
# View real-time logs
tail -f health_check.log
We welcome all forms of contributions! Please see CONTRIBUTING.md for detailed information.
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues during use, please:
Thanks to all developers who contributed to this project!
⭐ If this project helps you, please give us a Star!
⭐ 如果这个项目对您有帮助,请给我们一个 Star!
$ claude mcp add AIRouter \
-- python -m otcore.mcp_server <graph>