SkillAgentSearch skills...

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/AIRouter
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

AIRouter

<div align="center">

Python License Docker GitHub Stars

๐ŸŒ 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 ๆ–‡ไปถไบ†่งฃ่ฏฆ็ป†ไฟกๆฏใ€‚

๐Ÿ†˜ ๆ”ฏๆŒ

ๅฆ‚ๆžœๆ‚จๅœจไฝฟ็”จ่ฟ‡็จ‹ไธญ้‡ๅˆฐ้—ฎ้ข˜๏ผŒ่ฏท๏ผš

  1. ๆŸฅ็œ‹ GitHub Issues
  2. ๅˆ›ๅปบๆ–ฐ็š„ 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/       
View on GitHub
GitHub Stars173
CategoryDevelopment
Updated5d ago
Forks33

Languages

Python

Security Score

95/100

Audited on Mar 24, 2026

No findings