OpenClaw LLM Router 🧠
统一的大型语言模型(LLM)网关,由AIsa提供支持。
一个API密钥。70+模型。与OpenAI兼容。
用一个API密钥替换100+个API密钥。通过统一的、与OpenAI兼容的API访问GPT-4、Claude-3、Gemini、Qwen、Deepseek、Grok等。
🔥 你能做什么?
多模型聊天
"与GPT-4聊天进行推理,切换到Claude进行创意写作"
模型比较
"比较GPT-4、Claude和Gemini对同一个问题的回答"
视觉分析
"用GPT-4o分析这张图片 - 里面有什么物体?"
成本优化
"将简单查询路由到快速/便宜的模型,将复杂查询路由到GPT-4"
后备策略
"如果GPT-4失败,自动尝试Claude,然后是Gemini"
为什么选择LLM Router?
| 特性 | LLM Router | 直接API |
|---|---|---|
| API密钥 | 1 | 10+ |
| SDK兼容性 | OpenAI SDK | 多个SDK |
| 计费 | 统一 | 每个提供者 |
| 模型切换 | 更改字符串 | 代码重写 |
| 后备路由 | 内置 | DIY |
| 成本跟踪 | 统一 | 分散 |
支持的模型家族
| 家族 | 开发者 | 示例模型 |
|---|---|---|
| GPT | OpenAI | gpt-4.1, gpt-4o, gpt-4o-mini, o1, o1-mini, o3-mini |
| Claude | Anthropic | claude-3-5-sonnet, claude-3-opus, claude-3-sonnet |
| Gemini | gemini-2.0-flash, gemini-1.5-pro, gemini-1.5-flash | |
| Qwen | Alibaba | qwen-max, qwen-plus, qwen2.5-72b-instruct |
| Deepseek | Deepseek | deepseek-chat, deepseek-coder, deepseek-v3, deepseek-r1 |
| Grok | xAI | grok-2, grok-beta |
注意:模型可用性可能有所不同。请查看marketplace.aisa.one/pricing以获取当前可用模型和定价的完整列表。
快速开始
export AISA_API_KEY="your-key"
API端点
与OpenAI兼容的聊天完成
POST https://api.aisa.one/v1/chat/completions
请求
curl -X POST "https://api.aisa.one/v1/chat/completions" \
-H "Authorization: Bearer $AISA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4.1",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain quantum computing in simple terms."}
],
"temperature": 0.7,
"max_tokens": 1000
}'
参数
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
model |
string | 是 | 模型标识符(例如,gpt-4.1, claude-3-sonnet) |
messages |
array | 是 | 对话消息 |
temperature |
number | 否 | 随机性(0-2,默认:1) |
max_tokens |
integer | 否 | 最大响应令牌 |
stream |
boolean | 否 | 启用流(默认:false) |
top_p |
number | 否 | 核采样(0-1) |
frequency_penalty |
number | 否 | 频率惩罚(-2至2) |
presence_penalty |
number | 否 | 存在惩罚(-2至2) |
stop |
string/array | 否 | 停止序列 |
消息格式
{
"role": "user|assistant|system",
"content": "message text or array for multimodal"
}
响应
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "gpt-4.1",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Quantum computing uses..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 50,
"completion_tokens": 200,
"total_tokens": 250,
"cost": 0.0025
}
}
流响应
curl -X POST "https://api.aisa.one/v1/chat/completions" \
-H "Authorization: Bearer $AISA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-sonnet",
"messages": [{"role": "user", "content": "Write a poem about AI."}],
"stream": true
}'
流返回服务器发送的事件(SSE):
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"In"}}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":" circuits"}}]}
...
data: [DONE]
视觉/图像分析
通过传递图像URL或base64数据来分析图像:
curl -X POST "https://api.aisa.one/v1/chat/completions" \
-H "Authorization: Bearer $AISA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
]
}
]
}'
功能调用
启用工具/功能以获得结构化输出:
curl -X POST "https://api.aisa.one/v1/chat/completions" \
-H "Authorization: Bearer $AISA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4.1",
"messages": [{"role": "user", "content": "What is the weather in Tokyo?"}],
"functions": [
{
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
],
"function_call": "auto"
}'
Google Gemini格式
对于Gemini模型,您也可以使用本地格式:
POST https://api.aisa.one/v1/models/{model}:generateContent
curl -X POST "https://api.aisa.one/v1/models/gemini-2.0-flash:generateContent" \
-H "Authorization: Bearer $AISA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"role": "user",
"parts": [{"text": "Explain machine learning."}]
}
],
"generationConfig": {
"temperature": 0.7,
"maxOutputTokens": 1000
}
}'
Python客户端
安装
不需要安装 - 仅使用标准库。
CLI用法
# 基本完成
python3 {baseDir}/scripts/llm_router_client.py chat --model gpt-4.1 --message "Hello, world!"
# 带系统提示
python3 {baseDir}/scripts/llm_router_client.py chat --model claude-3-sonnet --system "You are a poet" --message "Write about the moon"
# 流式传输
python3 {baseDir}/scripts/llm_router_client.py chat --model gpt-4o --message "Tell me a story" --stream
# 多轮对话
python3 {baseDir}/scripts/llm_router_client.py chat --model qwen-max --messages '[{"role":"user","content":"Hi"},{"role":"assistant","content":"Hello!"},{"role":"user","content":"How are you?"}]'
# 视觉分析
python3 {baseDir}/scripts/llm_router_client.py vision --model gpt-4o --image "https://example.com/image.jpg" --prompt "Describe this image"
# 列出支持的模型
python3 {baseDir}/scripts/llm_router_client.py models
# 比较模型
python3 {baseDir}/scripts/llm_router_client.py compare --models "gpt-4.1,claude-3-sonnet,gemini-2.0-flash" --message "What is 2+2?"
Python SDK用法
from llm_router_client import LLMRouterClient
client = LLMRouterClient() # 使用AISA_API_KEY环境变量
# 简单聊天
response = client.chat(
model="gpt-4.1",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response["choices"][0]["message"]["content"])
# 带选项
response = client.chat(
model="claude-3-sonnet",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain relativity."}
],
temperature=0.7,
max_tokens=500
)
# 流式传输
for chunk in client.chat_stream(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a story."}]
):
print(chunk, end="", flush=True)
# 视觉
response = client.vision(
model="gpt-4o",
image_url="https://example.com/image.jpg",
prompt="What's in this image?"
)
# 比较模型
results = client.compare_models(
models=["gpt-4.1", "claude-3-opus"],
message="Analyze this quarterly report..."
)
for model, result in results.items():
print(f"{model}: {result['response'][:100]}...")
用例
1. 成本优化路由
使用更便宜的模型处理简单任务:
def smart_route(message: str) -> str:
# 简单查询 -> 快速/便宜模型
if len(message) < 50:
model = "gpt-3.5-turbo"
# 复杂推理 -> 强大模型
else:
model = "gpt-4.1"
return client.chat(model=model, messages=[{"role": "user", "content": message}])
2. 后备策略
自动后备失败:
def chat_with_fallback(message: str) -> str:
models = ["gpt-4.1", "claude-3-sonnet", "gemini-2.0-flash"]
for model in models:
try:
return client.chat(model=model, messages=[{"role": "user", "content": message}])
except Exception:
continue
raise Exception("All models failed")
3. 模型A/B测试
比较模型输出:
results = client.compare_models(
models=["gpt-4.1", "claude-3-opus"],
message="Analyze this quarterly report..."
)
# 记录以供分析
for model, result in results.items():
log_response(model=model, latency=result["latency"], cost=result["cost"])
4. 专业模型选择
为每项任务选择最佳模型:
MODEL_MAP = {
"code": "deepseek-coder",
"creative": "claude-3-opus",
"fast": "gpt-3.5-turbo",
"vision": "gpt-4o",
"chinese": "qwen-max",
"reasoning": "gpt-4.1"
}
def route_by_task(task_type: str, message: str) -> str:
model = MODEL_MAP.get(task_type, "gpt-4.1")
return client.chat(model=model, messages=[{"role": "user", "content": message}])
错误处理
错误返回带有error字段的JSON:
{
"error": {
"code": "model_not_found",
"message": "Model 'xyz' is not available"
}
}
常见错误代码:
401- 无效或缺少API密钥402- 积分不足404- 模型未找到429- 速率限制超出500- 服务器错误
最佳实践
- 使用流式传输 以改善长响应的用户体验
- 设置max_tokens 以控制成本
- 实现后备 以提高生产可靠性
- 缓存响应 以处理重复查询
- 通过响应元数据 监控使用情况
- 使用适当的模型 - 不要将GPT-4用于简单任务
OpenAI SDK兼容性
只需更改基础URL和密钥:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["AISA_API_KEY"],
base_url="https://api.aisa.one/v1"
)
response = client.chat.completions.create(
model="gpt-4.1",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
定价
基于令牌的定价因模型而异。请查看marketplace.aisa.one/pricing以获取当前费率。
| 模型家族 | 近似成本 |
|---|---|
| GPT-4.1 / GPT-4o | ~$0.01 / 1K令牌 |
| Claude-3-Sonnet | ~$0.01 / 1K令牌 |
| Gemini-2.0-Flash | ~$0.001 / 1K令牌 |
| Qwen-Max | ~$0.005 / 1K令牌 |
| DeepSeek-V3 | ~$0.002 / 1K令牌 |
每个响应都包括usage.cost和usage.credits_remaining。
开始使用
- 在aisa.one注册
- 从仪表板获取您的API密钥
- 添加积分(按需支付)
- 设置环境变量:
export AISA_API_KEY="your-key"
完整的API参考
查看API Reference以获取完整的端点文档。