ai-proxy
ai-proxy 插件通过将插件配置转换为指定的请求格式,简化对 LLM 和向量嵌入模型的访问。该插件支持集成 OpenAI、DeepSeek、Anthropic、Gemini、Vertex AI 以及其他兼容 OpenAI 的 API。
此外,该插件还支持将 LLM 请求信息记录到访问日志(而非错误日志)中,例如 Token 用量、模型、首 字节响应时间等。日志插件也可以采集这些日志条目。
请求协议检测
插件会先检测客户端请求协议,再选择兼容的上游端点。检测规则按以下顺序执行:
| 客户端协议 | 检测方式 | URI 要求 |
|---|---|---|
| Bedrock Converse | 请求体包含 messages 数组,且请求 URI 以 /converse 结尾。 | URI 可以包含自定义前缀,但必须保留 /converse 后缀。 |
| Anthropic Messages | 请求体是 JSON 对象,且请求 URI 以 /v1/messages 结尾。 | URI 可以包含自定义前缀,但必须保留 /v1/messages 后缀。 |
| OpenAI Responses | 请求体包含 input,且请求 URI 以 /v1/responses 结尾。 | URI 可以包含自定义前缀,但必须保留 /v1/responses 后缀。 |
| OpenAI Chat Completions | 请求体包含 messages 数组。 | 路由匹配的任意 URI。 |
| OpenAI Embeddings | 请求体包含 input,且前面的规则均不匹配。 | 路由匹配的任意 URI。 |
URI 特定规则会先于仅基于请求体的规则执行,从而避免将包含 messages 的 Bedrock Converse 和 Anthropic Messages 请求识别为 Chat Completions。Responses 和 Embeddings 请求都使用 input。包含 input 但不包含 messages 的请求,只有在 URI 以 /v1/responses 结尾时才会被识别为 Responses,否则会被识别为 Embeddings。其他非空 JSON 对象按透传处理;空请求体 或无效请求体会被拒绝。
示例
以下示例演示了如何针对不同场景配置 ai-proxy。
代理到 OpenAI
以下示例演示了如何在 ai-proxy 插件中配置 API Key、模型和其他参数,并在路由上配置该插件以将用户提示词代理到 OpenAI。
获取 OpenAI API Key,并可选择将其保存到环境变量中:
export OPENAI_API_KEY=YOUR_OPENAI_API_KEY # 替换为你的 API Key
- Admin API
- ADC
- Ingress Controller
创建一个路由并按如下方式配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
--data-binary @- <<EOF
{
"id": "ai-proxy-route",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer $OPENAI_API_KEY"
}
},
"options":{
"model": "gpt-4"
}
}
}
}
EOF
❶ 指定提供商为 openai。
❷ 在 Authorization 请求头中附带 OpenAI API Key。
❸ 指定模型名称。
向该路由发送一个 POST 请求,请求体中包含系统提示词和示例用户问题:
services:
- name: openai-service
routes:
- name: openai-route
uris:
- /anything
methods:
- POST
plugins:
ai-proxy:
provider: openai
auth:
header:
Authorization: "Bearer ${OPENAI_API_KEY}"
options:
model: gpt-4
将配置同步到网关:
adc sync -f adc.yaml
❶ 指定提供商为 openai。
❷ 在 Authorization 请求头中附带 OpenAI API Key。
❸ 指定模型名称。
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: ai-proxy-plugin-config
spec:
plugins:
- name: ai-proxy
config:
provider: openai
auth:
header:
Authorization: "Bearer YOUR_OPENAI_API_KEY"
options:
model: gpt-4
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: openai-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
method: POST
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: ai-proxy-plugin-config
将配置应用到集群:
kubectl apply -f ai-proxy-ic.yaml
❶ 指定提供商为 openai。
❷ 在 Authorization 请求头中附带 OpenAI API Key。
❸ 指定模型名称。
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: openai-route
spec:
ingressClassName: apisix
http:
- name: openai-route
match:
paths:
- /anything
methods:
- POST
plugins:
- name: ai-proxy
enable: true
config:
provider: openai
auth:
header:
Authorization: "Bearer YOUR_OPENAI_API_KEY"
options:
model: gpt-4
将配置应用到集群:
kubectl apply -f ai-proxy-ic.yaml
❶ 指定提供商为 openai。
❷ 在 Authorization 请求头中附带 OpenAI API Key。
❸ 指定模型名称。
发送一个 POST 请求到该路由,请求体中包含系统提示词和一个示例用户问题:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{ "role": "system", "content": "You are a mathematician" },
{ "role": "user", "content": "What is 1+1?" }
]
}'
你应该会收到类似以下的响应:
{
...,
"model": "gpt-4-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "1+1 equals 2.",
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}
],
...
}
代理到 DeepSeek
以下示例演示了如何配置 ai-proxy 插件将请求代理到 DeepSeek。
获取 DeepSeek API Key,并可选择将其保存到环境变量中:
export DEEPSEEK_API_KEY=YOUR_DEEPSEEK_API_KEY # 替换为你的 API Key
- Admin API
- ADC
- Ingress Controller
创建一个路由并按如下方式配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
--data-binary @- <<EOF
{
"id": "ai-proxy-route",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
"provider": "deepseek",
"auth": {
"header": {
"Authorization": "Bearer $DEEPSEEK_API_KEY"
}
},
"options": {
"model": "deepseek-chat"
}
}
}
}
EOF
❶ 指定提供商为 deepseek,插件会将请求代理到 https://api.deepseek.com/chat/completions。
❷ 在 Authorization 请求头中附带 DeepSeek API Key。
❸ 指定模型名称。
向该路由发送一个 POST 请求,请求体中包含系统提示词和示例用户问题:
services:
- name: deepseek-service
routes:
- name: deepseek-route
uris:
- /anything
methods:
- POST
plugins:
ai-proxy:
provider: deepseek
auth:
header:
Authorization: "Bearer ${DEEPSEEK_API_KEY}"
options:
model: deepseek-chat
将配置同步到网关:
adc sync -f adc.yaml
❶ 指定提供商为 deepseek,插件会将请求代理到 https://api.deepseek.com/chat/completions。
❷ 在 Authorization 请求头中附带 DeepSeek API Key。
❸ 指定模型名称。
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: ai-proxy-plugin-config
spec:
plugins:
- name: ai-proxy
config:
provider: deepseek
auth:
header:
Authorization: "Bearer YOUR_DEEPSEEK_API_KEY"
options:
model: deepseek-chat
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: deepseek-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
method: POST
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: ai-proxy-plugin-config
将配置应用到集群:
kubectl apply -f deepseek-ic.yaml
❶ 指定提供商为 deepseek,插件会将请求代理到 https://api.deepseek.com/chat/completions。
❷ 在 Authorization 请求头中附带 DeepSeek API Key。
❸ 指定模型名称。
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: deepseek-route
spec:
ingressClassName: apisix
http:
- name: deepseek-route
match:
paths:
- /anything
methods:
- POST
plugins:
- name: ai-proxy
enable: true
config:
provider: deepseek
auth:
header:
Authorization: "Bearer YOUR_DEEPSEEK_API_KEY"
options:
model: deepseek-chat
将配置应用到集群:
kubectl apply -f deepseek-ic.yaml
❶ 指定提供商为 deepseek,插件会将请求代理到 https://api.deepseek.com/chat/completions。
❷ 在 Authorization 请求头中附带 DeepSeek API Key。
❸ 指定模型名称。
向该路 由发送一个 POST 请求,请求体中包含输入字符串:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "system",
"content": "You are an AI assistant that helps people find information."
},
{
"role": "user",
"content": "Write me a 50-word introduction for Apache APISIX."
}
]
}'
你应该会收到类似以下的响应:
{
...
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Apache APISIX is a dynamic, real-time, high-performance API gateway and cloud-native platform. It provides rich traffic management features like load balancing, dynamic upstream, canary release, circuit breaking, authentication, observability, and more. Designed for microservices and serverless architectures, APISIX ensures scalability, security, and seamless integration with modern DevOps workflows."
},
"logprobs": null,
"finish_reason": "stop"
}
],
...
}
代理到 Azure OpenAI
以下示例演示了如何配置 ai-proxy 插件,将请求代理到 Azure OpenAI 等其他 LLM 服务。
获取 Azure OpenAI API Key,并可选择将其保存到环境变量中:
export AZ_OPENAI_API_KEY=YOUR_AZURE_OPENAI_API_KEY # 替换为你的 API Key
- Admin API
- ADC
- Ingress Controller
创建一个路由并按如下方式配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
--data-binary @- <<EOF
{
"id": "ai-proxy-route",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
"provider": "azure-openai",
"auth": {
"header": {
"api-key": "$AZ_OPENAI_API_KEY"
}
},
"options":{
"model": "gpt-4"
},
"override": {
"endpoint": "https://api7-azure-openai.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-15-preview"
}
}
}
}
EOF
❶ 将提供商设置为 azure-openai。
❷ 在 api-key 请求头中附加 Azure OpenAI API Key。
❸ 指定模型名称。
❹ 指定 Azure OpenAI 端点。
向该路由发送一个 POST 请求,请求体中包含系统提示词和示例用户问题:
services:
- name: azure-openai-service
routes:
- name: azure-openai-route
uris:
- /anything
methods:
- POST
plugins:
ai-proxy:
provider: azure-openai
auth:
header:
api-key: "${AZ_OPENAI_API_KEY}"
options:
model: gpt-4
override:
endpoint: "https://api7-azure-openai.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-15-preview"
将配置同步到网关:
adc sync -f adc.yaml
❶ 将提供商设置为 azure-openai。
❷ 在 api-key 请求头中附加 Azure OpenAI API Key。
❸ 指定模型名称。
❹ 指定 Azure OpenAI 端点。
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: ai-proxy-plugin-config
spec:
plugins:
- name: ai-proxy
config:
provider: azure-openai
auth:
header:
api-key: "YOUR_AZURE_OPENAI_API_KEY"
options:
model: gpt-4
override:
endpoint: "https://api7-azure-openai.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-15-preview"
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: azure-openai-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
method: POST
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: ai-proxy-plugin-config
将配置应用到集群:
kubectl apply -f azure-openai-ic.yaml
❶ 将提供商设置为 azure-openai。
❷ 在 api-key 请求头中附加 Azure OpenAI API Key。
❸ 指定模型名称。
❹ 指定 Azure OpenAI 端点。
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: azure-openai-route
spec:
ingressClassName: apisix
http:
- name: azure-openai-route
match:
paths:
- /anything
methods:
- POST
plugins:
- name: ai-proxy
enable: true
config:
provider: azure-openai
auth:
header:
api-key: "YOUR_AZURE_OPENAI_API_KEY"
options:
model: gpt-4
override:
endpoint: "https://api7-azure-openai.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-15-preview"
将配置应用到集群:
kubectl apply -f azure-openai-ic.yaml
❶ 将提供商设置为 azure-openai。
❷ 在 api-key 请求头中附加 Azure OpenAI API Key。
❸ 指定模型名称。
❹ 指定 Azure OpenAI 端点。
向该路由发送一个 POST 请求,请求体中包含输入字符串:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "system",
"content": "You are an AI assistant that helps people find information."
},
{
"role": "user",
"content": "Write me a 50-word introduction for Apache APISIX."
}
],
"max_tokens": 800,
"temperature": 0.7,
"frequency_penalty": 0,
"presence_penalty": 0,
"top_p": 0.95,
"stop": null
}'
你应该会收到类似以下的响应:
{
"choices": [
{
...,
"message": {
"content": "Apache APISIX is a modern, cloud-native API gateway built to handle high-performance and low-latency use cases. It offers a wide range of features, including load balancing, rate limiting, authentication, and dynamic routing, making it an ideal choice for microservices and cloud-native architectures.",
"role": "assistant"
}
}
],
...
}
代理到 Gemini
以下示例演示了如何配置 ai-proxy 插件,将聊天补全请求代理到 Google 的 Gemini API。本示例仅适用于 API7 企业版 3.9.2 及更高版本,不适用于 Apache APISIX。
获取 Gemini API Key,并可选择将其保存到环境变量中:
export GEMINI_API_KEY=YOUR_GEMINI_API_KEY # 替换为你的 API Key
- Admin API
- ADC
- Ingress Controller
创建一个路由并按如下方式配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
--data-binary @- <<EOF
{
"id": "ai-proxy-gemini-route",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
"provider": "gemini",
"auth": {
"header": {
"Authorization": "Bearer $GEMINI_API_KEY"
}
},
"options": {
"model": "gemini-2.5-flash"
}
}
}
}
EOF
❶ 将提供商指定为 gemini。
❷ 在 Authorization 请求头中替换为你的 Gemini API Key。
❸ 指定 Gemini 模型名称。
向该路由发送一个 POST 请求,请求体中包含系统提示词和示例用户问题:
services:
- name: gemini-service
routes:
- name: gemini-route
uris:
- /anything
methods:
- POST
plugins:
ai-proxy:
provider: gemini
auth:
header:
Authorization: "Bearer ${GEMINI_API_KEY}"
options:
model: gemini-2.5-flash
将配置同步到网关:
adc sync -f adc.yaml
❶ 将提供商指定为 gemini。
❷ 在 Authorization 请求头中替换为你的 Gemini API Key。
❸ 指定 Gemini 模型名称。
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: ai-proxy-plugin-config
spec:
plugins:
- name: ai-proxy
config:
provider: gemini
auth:
header:
Authorization: "Bearer YOUR_GEMINI_API_KEY"
options:
model: gemini-2.5-flash
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: gemini-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
method: POST
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: ai-proxy-plugin-config
将配置应用到集群:
kubectl apply -f gemini-ic.yaml
❶ 将提供商指定为 gemini。
❷ 在 Authorization 请求头中替换为你的 Gemini API Key。
❸ 指定 Gemini 模型名称。
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: gemini-route
spec:
ingressClassName: apisix
http:
- name: gemini-route
match:
paths:
- /anything
methods:
- POST
plugins:
- name: ai-proxy
enable: true
config:
provider: gemini
auth:
header:
Authorization: "Bearer YOUR_GEMINI_API_KEY"
options:
model: gemini-2.5-flash
将配置应用到集群:
kubectl apply -f gemini-ic.yaml
❶ 将提供商指定为 gemini。
❷ 在 Authorization 请求头中替换为你的 Gemini API Key。
❸ 指定 Gemini 模型名称。
上述配置会将请求代理到位于 https://generativelanguage.googleapis.com/v1beta/openai/chat/completions 的聊天补全端点。若要将请求代理到向量嵌入模型,请在 override 字段中显式配置该模型的端点。
发送一个 POST 请求到该路由,请求体中包含系统提示词和一个示例用户问题:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{ "role": "system", "content": "You are a helpful AI assistant" },
{ "role": "user", "content": "What is the capital of France?" }
]
}'
你应该会收到类似以下的响应:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "The capital of France is **Paris**.",
"role": "assistant"
}
}
],
"model": "gemini-2.5-flash",
"object": "chat.completion",
"usage": {
"completion_tokens": 8,
"prompt_tokens": 15,
"total_tokens": 41
},
...
}
代理到 Vertex AI 聊天补全
以下示例演示了如何配置 ai-proxy 插件,使用 GCP 服务账号认证将请求代理到 Google Cloud Vertex AI 平台。本示例仅适用于 API7 企业版 3.9.2 及更高版本,不适用于 Apache APISIX。
在继续之前:
- 启用 Vertex AI 并为 GCP 项目启用结算。
- 按照服务账号凭证文档在 GCP 中创建服务账号,为该账号分配“Vertex AI User”角色,并获取 JSON 格式的账号凭证。
凭证文件应类似如下:
{
"type": "service_account",
"project_id": "api7-vertex",
"private_key_id": "...",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
"client_email": "api7-docs@api7-vertex.iam.gserviceaccount.com",
"client_id": "....",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/api7-docs%40api7-vertex.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
你可以选择将该 JSON 保存到 环境变量中:
export GCP_SA_JSON="$(cat credentials.json)"
- Admin API
- ADC
- Ingress Controller
创建一个路由并按如下方式配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
--data-binary @- <<EOF
{
"id": "ai-proxy-vertex-ai-route",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
"provider": "vertex-ai",
"auth": {
"gcp": {
"service_account_json": "$GCP_SA_JSON"
}
},
"provider_conf": {
"project_id": "api7-vertex",
"region": "us-central1"
},
"options": {
"model": "google/gemini-2.5-flash"
}
}
}
}
EOF
❶ 将模型服务提供方指定为 vertex-ai。
❷ 替换为你的 JSON 凭证。请确保该值为经过 JSON 转义的字符串。
❸ 替换为你的 Vertex AI 项目 ID 和区域。
❹ 以 <publisher>/<model> 格式指定通过 Vertex AI 使用的 Gemini 模型名称。
向该路由发送一个 POST 请求,请求体中包含系统提示词和示例用户问题:
services:
- name: vertex-ai-service
routes:
- name: vertex-ai-route
uris:
- /anything
methods:
- POST
plugins:
ai-proxy:
provider: vertex-ai
auth:
gcp:
service_account_json: "${GCP_SA_JSON}"
provider_conf:
project_id: api7-vertex
region: us-central1
options:
model: google/gemini-2.5-flash
将配置同步到网关:
adc sync -f adc.yaml
❶ 将模型服务提供方指定为 vertex-ai。
❷ 替换为你的 JSON 凭证。请确保该值为经过 JSON 转义的字符串。
❸ 替换为你的 Vertex AI 项目 ID 和区域。
❹ 以 <publisher>/<model> 格式指定通过 Vertex AI 使用的 Gemini 模型名称。
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: ai-proxy-plugin-config
spec:
plugins:
- name: ai-proxy
config:
provider: vertex-ai
auth:
gcp:
service_account_json: '{"type":"service_account","project_id":"api7-vertex","private_key_id":"...","private_key":"-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----","client_email":"api7-docs@api7-vertex.iam.gserviceaccount.com","client_id":"...","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/api7-docs%40api7-vertex.iam.gserviceaccount.com","universe_domain":"googleapis.com"}'
provider_conf:
project_id: api7-vertex
region: us-central1
options:
model: google/gemini-2.5-flash
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: vertex-ai-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
method: POST
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: ai-proxy-plugin-config
将配置应用到集群:
kubectl apply -f vertex-ai-ic.yaml
❶ 将模型服务提供方指定为 vertex-ai。
❷ 替换为你的 JSON 凭证。请确保该值为经过 JSON 转义的字符串。
❸ 替换为你的 Vertex AI 项目 ID 和区域。
❹ 以 <publisher>/<model> 格式指定通过 Vertex AI 使用的 Gemini 模型名称。
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: vertex-ai-route
spec:
ingressClassName: apisix
http:
- name: vertex-ai-route
match:
paths:
- /anything
methods:
- POST
plugins:
- name: ai-proxy
enable: true
config:
provider: vertex-ai
auth:
gcp:
service_account_json: '{"type":"service_account","project_id":"api7-vertex","private_key_id":"...","private_key":"-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----","client_email":"api7-docs@api7-vertex.iam.gserviceaccount.com","client_id":"...","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/api7-docs%40api7-vertex.iam.gserviceaccount.com","universe_domain":"googleapis.com"}'
provider_conf:
project_id: api7-vertex
region: us-central1
options:
model: google/gemini-2.5-flash
将配置 应用到集群:
kubectl apply -f vertex-ai-ic.yaml
❶ 将模型服务提供方指定为 vertex-ai。
❷ 替换为你的 JSON 凭证。请确保该值为经过 JSON 转义的字符串。
❸ 替换为你的 Vertex AI 项目 ID 和区域。
❹ 以 <publisher>/<model> 格式指定通过 Vertex AI 使用的 Gemini 模型名称。
发送一个 POST 请求到该路由,请求体中包含系统提示词和一个示例用户问题:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{ "role": "system", "content": "You are a mathematician" },
{ "role": "user", "content": "What is 1+1?" }
]
}'
你应该会收到类似以下的响应:
{
"choices": [
{
"message": {
"role": "assistant",
"content": "1 + 1 = 2\n"
},
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"completion_tokens": 8,
"extra_properties": {
"google": {
"traffic_type": "ON_DEMAND"
}
},
"total_tokens": 19,
"prompt_tokens": 11
},
"object": "chat.completion",
"model": "google/gemini-2.5-flash",
...
}