Appearance
Gateway模式是什么?
OpenClaw的两种使用方式
| 模式 | 说明 | 适用场景 |
|---|---|---|
| Agent模式 | 对话式交互 | 聊天机器人、问答助手 |
| Gateway模式 | API代理转发 | 应用后端、统一AI入口 |
Gateway模式价值
- 统一API入口:一个接口访问所有模型
- 统一Key管理:集中管理API密钥
- 统一成本统计:Token使用追踪
- 统一模型切换:灵活切换模型
启用Gateway模式
配置Gateway
yaml
# ~/.openclaw/config.yaml
gateway:
enabled: true
port: 8080
host: "0.0.0.0"
# API Key验证
auth:
enabled: true
keys:
- name: app-a
key: "sk-app-a-xxxx"
- name: app-b
key: "sk-app-b-xxxx"启动Gateway服务
bash
# 启动网关
openclaw gateway start
# 或指定端口
openclaw gateway start --port 8080Gateway API接口
兼容OpenAI格式
bash
curl http://localhost:8080/v1/chat/completions \
-H "Authorization: Bearer sk-app-a-xxxx" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-chat",
"messages": [{"role": "user", "content": "你好"}]
}'响应格式
json
{
"id": "chatcmpl-xxx",
"model": "deepseek-chat",
"choices": [{
"message": {"role": "assistant", "content": "你好!有什么可以帮助你的?"}
}],
"usage": {"prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30}
}配置模型路由
多模型配置
yaml
providers:
deepseek:
enabled: true
apiKey: "${DEEPSEEK_API_KEY}"
openai:
enabled: true
apiKey: "${OPENAI_API_KEY}"
gateway:
routes:
- name: default
model: deepseek-chat
- name: advanced
model: gpt-4o负载均衡配置
多模型负载均衡
yaml
gateway:
loadBalance:
strategy: round-robin # 轮询
models:
- deepseek-chat
- gpt-3.5-turbo按权重分配
yaml
gateway:
loadBalance:
strategy: weighted
models:
- name: deepseek-chat
weight: 80 # 80%流量
- name: gpt-3.5-turbo
weight: 20 # 20%流量故障转移
yaml
gateway:
failover:
enabled: true
primary: deepseek-chat
fallback:
- gpt-3.5-turbo
- claude-3-5-sonnet限流与配额
请求限流
yaml
gateway:
rateLimit:
enabled: true
requestsPerMinute: 100
tokensPerMinute: 100000按应用配额
yaml
gateway:
quotas:
- name: app-a
key: "sk-app-a-xxxx"
limits:
requestsPerDay: 1000
tokensPerDay: 500000成本统计
Token使用统计
yaml
gateway:
analytics:
enabled: true
logRequests: true
dimensions:
- app
- model
- date查看统计
bash
# 查看今日统计
openclaw gateway stats --today
# 按应用查看
openclaw gateway stats --app app-a
# 导出报告
openclaw gateway stats --export report.csv安全配置
API Key管理
yaml
gateway:
auth:
enabled: true
keys:
- name: app-a
key: "sk-app-a-xxxx"
permissions:
models:
- deepseek-chat
- gpt-3.5-turbo
rateLimit:
requestsPerMinute: 50IP白名单
yaml
gateway:
security:
ipWhitelist:
enabled: true
allowed:
- "127.0.0.1"
- "192.168.1.0/24"与应用集成
Python集成示例
python
from openai import OpenAI
client = OpenAI(
api_key="sk-app-a-xxxx",
base_url="http://localhost:8080/v1"
)
response = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "你好"}]
)
print(response.choices[0].message.content)Node.js集成示例
javascript
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'sk-app-a-xxxx',
baseURL: 'http://localhost:8080/v1'
});
const response = await client.chat.completions.create({
model: 'deepseek-chat',
messages: [{ role: 'user', content: '你好' }]
});高可用部署
使用Docker部署
yaml
# docker-compose.yml
version: '3'
services:
openclaw-gateway:
image: openclaw/gateway:latest
ports:
- "8080:8080"
volumes:
- ./config:/root/.openclaw
environment:
- DEEPSEEK_API_KEY=${DEEPSEEK_API_KEY}
restart: alwaysGateway监控
健康检查
bash
curl http://localhost:8080/health
# 响应
{
"status": "healthy",
"uptime": 86400,
"requests": 10000,
"errors": 5
}Prometheus指标
yaml
gateway:
metrics:
enabled: true
port: 9090
path: /metrics总结
OpenClaw Gateway模式:
| 功能 | 说明 |
|---|---|
| 统一API入口 | 一个接口访问所有模型 |
| 统一Key管理 | 集中管理API密钥 |
| 负载均衡 | 多模型流量分配 |
| 故障转移 | 主模型失败自动切换 |
| 成本统计 | Token使用追踪 |
| 安全控制 | Key权限、IP白名单 |
把OpenClaw当AI API网关用。
统一管理所有AI模型调用。
🦞 Claw is the law. Gateway模式,统一入口。
