跳到主要内容

a6-plugin-ai-proxy

概览

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

适用场景

  • 代理聊天完成或嵌入请求到任何支持的 LLM 提供者
  • 在网关集中 API 密钥, 而不是向客户端分发
  • 为 LLM 调用增加可观测性,包括 Token 数和延迟
  • ai-prompt-templateai-prompt-decorator 或内容审核插件组合,构建完整的 AI 网关处理链

支持的服务提供方

服务提供方默认端点
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"
}
}

Gemini

{
"auth": {
"header": {
"Authorization": "Bearer your-gemini-key"
}
}
}

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"
}
}

service_account_json也可以通过GCP_SERVICE_ACCOUNT 环境变量

自定义OpenAI兼容API

{
"auth": {
"header": {
"Authorization": "Bearer your-token"
}
},
"override": {
"endpoint": "https://your-custom-llm.com/v1/chat/completions"
}
}

分步操作:路由到 OpenAI

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

a6 route create -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?"}
]
}'

网关添加身份认证信息并将请求转发到 OpenAI,客户端无需接触提供商 API Key。

常见模式

流式响应

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

客户端通过 Server-Sent Events(SSE)接收响应。在流式模式下获取 Token 用量时,请在请求正文中设置 stream_options.include_usage: true

Azure OpenAI

{
"plugins": {
"ai-proxy": {
"provider": "azure-openai",
"auth": {
"header": {
"api-key": "your-azure-key"
}
},
"options": {
"model": "gpt-4"
},
"override": {
"endpoint": "https://myresource.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-15-preview"
},
"timeout": 60000
}
}
}

嵌入终结点

a6 route create -f - <<'EOF'
{
"id": "embeddings",
"uri": "/v1/embeddings",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer sk-your-key"
}
},
"options": {
"model": "text-embedding-3-small"
},
"override": {
"endpoint": "https://api.openai.com/v1/embeddings"
}
}
}
}
EOF

启用日志

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

具有多条路由的模型路由

插件本身不会按模型路由。请创建多条路由,并使用 vars 匹配请求正文中的模型字段:

# Route requests for gpt-4 to OpenAI
a6 route create -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

# Route requests for deepseek-chat to DeepSeek
a6 route create -f - <<'EOF'
{
"id": "deepseek-chat",
"uri": "/v1/chat/completions",
"methods": ["POST"],
"vars": [["post_arg.model", "==", "deepseek-chat"]],
"plugins": {
"ai-proxy": {
"provider": "deepseek",
"auth": { "header": { "Authorization": "Bearer sk-deepseek-key" } },
"options": { "model": "deepseek-chat" }
}
}
}
EOF

使用ai-proxy-multi进行负载平衡

对于跨提供商的负载平衡、故障转移和基于优先级的路由, 改用ai-proxy-multi

{
"plugins": {
"ai-proxy-multi": {
"balancer": {
"algorithm": "roundrobin"
},
"fallback_strategy": ["rate_limiting", "http_429", "http_5xx"],
"instances": [
{
"name": "openai-primary",
"provider": "openai",
"priority": 1,
"weight": 8,
"auth": {
"header": { "Authorization": "Bearer sk-openai-key" }
},
"options": { "model": "gpt-4" }
},
{
"name": "deepseek-backup",
"provider": "deepseek",
"priority": 0,
"weight": 2,
"auth": {
"header": { "Authorization": "Bearer sk-deepseek-key" }
},
"options": { "model": "deepseek-chat" }
}
]
}
}
}

访问日志变量

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

配置同步示例

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
logging:
summaries: true

故障排查

症状原因解决方法
502 Bad Gateway端点或提供商配置错误确认 provider 与目标 API 一致;使用 Azure 或自定义端点时检查 override.endpoint
上游返回 401API Key 无效检查 auth.header,并确认密钥对该提供商有效
请求超时LLM 响应缓慢增大 timeout(默认 30,000 毫秒),或使用流式响应
流式响应中没有 Token 用量缺少流式用量选项客户端应发送 stream_options.include_usage: true
Azure 返回 404URL 缺少 API 版本override.endpoint 中加入 ?api-version=YYYY-MM-DD-preview
Vertex AI 认证失败服务账号 JSON 无效通过 auth.gcp.service_account_jsonGCP_SERVICE_ACCOUNT 环境变量配置

本文根据 api7/a6 仓库中的 a6-plugin-ai-proxy/SKILL.md 生成。可在 AI Agent Skills 页面浏览全部 Skill。