跳到主要内容
版本:3.10.x

API7 网关 AI Agent Skill:ai-proxy 插件

概览

ai-proxy 插件将 API7 企业版变为 AI 网关。它以 OpenAI 兼容格式将请求代理到模型服务提供方,并处理身份验证、端点路由和响应流式传输。客户端发送标准聊天补全请求;插件会转换请求并转发给已配置的模型服务提供方。

适用场景

  • 将聊天补全或嵌入请求代理到任意受支持的 LLM 提供方
  • 将 API Key 集中保存在网关侧,而不是分发给客户端
  • 为 LLM 调用添加可观测性(Token 用量和延迟)
  • ai-prompt-templateai-prompt-decorator 或内容 审核插件组合,构建完整的 AI 网关流水线
  • 直接在服务或路由上应用一致的 ai-proxy 配置

支持的服务提供方

服务提供方默认端点
OpenAIopenaihttps://api.openai.com/v1/chat/completions
DeepSeekdeepseekhttps://api.deepseek.com/chat/completions
Azure OpenAIazure-openai通过 override.endpoint 自定义
Anthropicanthropichttps://api.anthropic.com/v1/chat/completions
AIMLAPIaimlapihttps://api.aimlapi.com/v1/chat/completions
OpenRouteropenrouterhttps://openrouter.ai/api/v1/chat/completions
Geminigeminihttps://generativelanguage.googleapis.com/v1beta/openai/chat/completions
Vertex AIvertex-aihttps://aiplatform.googleapis.com
OpenAI 兼容openai-compatible通过 override.endpoint 自定义

插件配置参考

字段类型是否必填默认值说明
providerstring9 个受支持提供方之一
authobject认证配置(见下文)
optionsobject模型和生成参数
options.modelstring模型名称(提供方相关)
options.temperaturenumber采样温度
options.top_pnumber核采样
options.max_tokensinteger生成的最大 Token 数
options.streambooleanfalse启用 SSE 流式传输
overrideobject覆盖默认端点
override.endpointstring提供方 API 的完整 URL
provider_confobject提供方特定配置(Vertex AI)
provider_conf.project_idstringGCP project ID(Vertex AI)
provider_conf.regionstringGCP 区域(Vertex AI)
loggingobject日志选项
logging.summariesbooleanfalse记录模型、耗时和 Token
logging.payloadsbooleanfalse记录请求/响应体
timeoutinteger30000请求超时时间(毫秒)
keepalivebooleantrue保持连接
keepalive_timeoutinteger60000Keepalive 超时时间(毫秒)
keepalive_poolinteger30Keepalive 连接池大小
ssl_verifybooleantrue校验 SSL 证书

按服务提供方配置身份认证

OpenAI / DeepSeek / Anthropic / AIMLAPI / OpenRouter

{
"auth": {
"header": {
"Authorization": "Bearer sk-your-api-key"
}
}
}

Azure OpenAI

{
"auth": {
"header": {
"api-key": "your-azure-key"
}
},
"override": {
"endpoint": "https://YOUR-RESOURCE.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-15-preview"
}
}

Vertex AI(GCP 服务账号)

{
"auth": {
"gcp": {
"service_account_json": "{ ... }",
"max_ttl": 3600,
"expire_early_secs": 60
}
},
"provider_conf": {
"project_id": "your-project-id",
"region": "us-central1"
}
}

分步操作:路由到 OpenAI

1. 创建启用 ai-proxy 的路由

路由等所有运行时资源都必须使用 --gateway-group-g 指定网关组作用域。

a7 route create -g default -f - <<'EOF'
{
"id": "openai-chat",
"uri": "/v1/chat/completions",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer sk-your-openai-key"
}
},
"options": {
"model": "gpt-4",
"temperature": 0.7,
"max_tokens": 1024
}
}
}
}
EOF

2. 发送请求

curl http://127.0.0.1:9080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is 1+1?"}
]
}'

使用服务

在 API7 企业版中,可直接在服务或路由上配置 ai-proxy。服务更适合承载可复用的上游和插件配置。

a7 service create -g default -f - <<'EOF'
{
"id": "standard-ai-proxy",
"name": "Standard AI Proxy",
"plugins": {
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer sk-global-key"
}
},
"options": {
"model": "gpt-4"
}
}
}
}
EOF

常见模式

流式响应

{
"plugins": {
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer sk-your-key"
}
},
"options": {
"model": "gpt-4",
"stream": true
}
}
}
}

使用多个路由进行模型路由

该插件不会原生按模型路由。请使用不同路由,并通过 vars 匹配请求体字段:

# 将 gpt-4 请求路由到 OpenAI
a7 route create -g default -f - <<'EOF'
{
"id": "openai-gpt4",
"uri": "/v1/chat/completions",
"methods": ["POST"],
"vars": [["post_arg.model", "==", "gpt-4"]],
"plugins": {
"ai-proxy": {
"provider": "openai",
"auth": { "header": { "Authorization": "Bearer sk-openai-key" } },
"options": { "model": "gpt-4" }
}
}
}
EOF

访问日志变量

变量说明
$request_typetraditional_httpai_chatai_stream
$llm_time_to_first_token首个 Token 返回耗时(毫秒)
$llm_model提供方实际使用的模型
$request_llm_model客户端请求的模型
$llm_prompt_tokens提示词 Token 数量
$llm_completion_tokens补全 Token 数量

配置同步示例

配置同步以网关组为作用域:

a7 config sync -f config.yaml --gateway-group default
version: "1"
routes:
- id: openai-chat
uri: /v1/chat/completions
methods:
- POST
plugins:
ai-proxy:
provider: openai
auth:
header:
Authorization: Bearer sk-your-openai-key
options:
model: gpt-4
max_tokens: 1024
temperature: 0.7

故障排查

现象原因修复方式
502 Bad Gateway端点或 provider 值错误确认 provider 匹配,并检查 override.endpoint
上游返回 401API Key 无效检查 auth.header
404 Not Found缺少 --gateway-group确保所有运行时命令都包含 -g <group>
Azure 404URL 中缺少 api-versionoverride.endpoint 中包含 ?api-version=YYYY-MM-DD-preview

本页面由 api7/a7 仓库中的 a7-plugin-ai-proxy/SKILL.md 生成。你可以在 AI Agent Skills 页面查看所有技能。