关于 ai-gateway
ai-gateway 是用于 ML 运维中 AI 服务流量路由、安全控制和监控的 skill。它支持按规则路由、端点控制、指标查看,并可通过命令行、API 或 YAML 配置。
主要功能
- 01按路径或标头规则路由请求
- 02应用身份验证和速率限制
- 03跟踪延迟与错误指标
- 04使用 YAML 配置路由
- 05在后端间均衡流量并自动故障转移
适用场景
- 01将预测请求路由至 ML 服务
- 02保护 AI 端点并限制请求频率
- 03监控 AI 流量的延迟和错误
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ alphaonedev/ openclaw-graph --skill ai-gateway安装后在 Claude Code 中输入/ai-gateway确认已加载。
详细介绍
ai-gateway
用途
此技能用于管理 AI 网关,以便在 ML 运维中路由、保护和监控 AI 服务请求,确保 aimlops 集群内的流量得到高效处理、API 安全可靠且性能可跟踪。
适用场景
在构建需要集中路由 AI 请求的 ML 流水线时使用此技能,例如在生产环境中使用多个 AI 模型时,可通过它执行安全策略、监控流量或扩展 API 端点。当场景涉及用于 AI 推理的微服务,或将工具(如 Kubernetes)集成到 aimlops 工作流中时,也可使用此技能。
主要功能
- 路由:根据规则动态将请求路由到 AI 服务,支持基于路径或标头的匹配。
- 安全:通过 JWT 或 API 密钥执行身份验证、速率限制和加密。
- 监控:通过集成日志记录和 Prometheus 导出器跟踪请求延迟和错误率等指标。
- 配置:支持基于 YAML 的配置来定义路由,例如指定源端点和目标端点。
- 可扩展性:支持在多个 AI 后端之间进行负载均衡,并在故障时自动切换。
使用模式
要使用此技能,请先通过命令行或 API 设置 AI 网关,然后定义路由和安全规则。始终使用环境变量 $AI_GATEWAY_API_KEY 对请求进行身份验证。使用命令行时,通过 ai-gateway-cli init --config path/to/config.yaml 初始化,然后通过 ai-gateway-cli apply 应用更改。在代码中,导入 SDK 并调用 createRoute() 等方法以编程方式进行设置。定期查询指标端点以监控使用情况。
常用命令/API
-
命令行命令:
- 初始化网关:
ai-gateway-cli init --cluster aimlops --key $AI_GATEWAY_API_KEY - 添加路由:
ai-gateway-cli add-route --path /predict --target http://ai-service:8080 --method POST - 保护端点:
ai-gateway-cli secure --endpoint /predict --auth jwt --rate-limit 100/min - 查看指标:
ai-gateway-cli metrics --format json
- 初始化网关:
-
API 端点:
- 创建路由:使用请求体
{ "path": "/predict", "target": "http://ai-service:8080", "method": "POST" }发送 POST 请求到 /api/v1/routes - 更新安全设置:使用请求体
{ "authType": "jwt", "rateLimit": 100 }发送 PUT 请求到 /api/v1/security/{endpoint} - 获取指标:GET /api/v1/metrics?type=latency
- 创建路由:使用请求体
-
代码片段:
import requests headers = {'Authorization': f'Bearer {os.environ.get("AI_GATEWAY_API_KEY")}'} response = requests.post('http://gateway:8080/api/v1/routes', json={"path": "/predict", "target": "http://ai-service:8080"}, headers=headers)export AI_GATEWAY_API_KEY=your_api_key_here ai-gateway-cli add-route --path /chat --target http://llm-service:5000 -
配置格式: 使用 YAML 进行配置,例如:
routes: - path: /predict target: http://ai-service:8080 methods: [POST] security: - endpoint: /predict auth: jwt rateLimit: 100
集成说明
将网关部署为集群中的 sidecar 或独立服务,以便与 aimlops 集成。对于 Kubernetes,可向 Pod 添加注解,例如 kubectl annotate pod ai-pod aimlops/gateway=true。使用 SDK 与其他 AI 工具连接:导入并通过 AI_Gateway(api_key=os.environ['AI_GATEWAY_API_KEY']).connect(cluster='aimlops') 进行初始化。通过匹配“ai”和“mlops”等标签确保兼容性。对于外部服务,可在配置中设置网关的回调 URL 来配置 Webhook,例如在 YAML 中添加 callback: http://external-service/webhook。
错误处理
通过检查 API 响应中的 HTTP 状态码来处理错误;例如,401 表示身份验证失败,此时使用 headers['Authorization'] = f'Bearer {new_key}' 重新尝试。对于命令行,解析输出中的错误(如“Error: Invalid route path”)并更正输入。常见问题包括缺少 API 密钥——始终验证 if not os.environ.get('AI_GATEWAY_API_KEY'): raise ValueError('API key required')。使用网关内置的日志记录器记录错误:通过 ai-gateway-cli config --log-level debug 启用,然后监控速率限制超出等情况,并在代码中使用指数退避重试。
具体用法示例
-
路由 AI 请求:要将预测请求路由到 ML 模型,首先导出 API 密钥,然后使用 CLI:
export AI_GATEWAY_API_KEY=abc123; ai-gateway-cli add-route --path /ml-predict --target http://model-service:8000。使用 curl 请求进行验证:curl -H "Authorization: Bearer abc123" http://gateway:8080/ml-predict -d '{"input": "data"}'。 -
保护并监控 API:运行以下命令保护端点并监控流量:
ai-gateway-cli secure --endpoint /chat --auth api-key --rate-limit 50/min。然后查询指标:ai-gateway-cli metrics --endpoint /chat。在代码中,按如下方式处理: ```python import requests; headers = {'Authorization': f'Bearer {os.environ["AI_GATEWAY_API_KEY"]}'}; requests.get('http://gateway:8080/api/v1/metrics', headers=headers)
Graph Relationships
- Related to: aimlops (cluster), ai (tag), mlops (tag)
- Depends on: authentication services for security
- Used by: AI services for routing and monitoring
- Integrates with: Kubernetes for deployment, Prometheus for metrics collection