ai-proxy
ai-proxy 插件通过将插件配置转换为所需的请求格式,简化对 LLM 和向量嵌入模型的访问。该插件支持 OpenAI、DeepSeek、Anthropic、Gemini、Vertex AI 以及其他兼容 OpenAI 的 API。
转发请求前,插件会根据请求 URI 和请求体识别客户端协议。当客户端使用 Anthropic Messages,而所选服务提供方支持 OpenAI Chat Completions 而非原生 Anthropic Messages 时,插件会执行协议转换:将客户端请求转换为 OpenAI 格式,并将后端响应转换为 Anthropic 格式。该转换只支持 Anthropic Messages 的一部分功能。有关检测规则、字段映射、版本边界和限制,请参阅协议参考。
该插件还可以在访问日志而非错误日志中记录 LLM 请求信息,包括 Token 用量、模型和首字节响应时间。日志插件可以采集这些条目。
流式响应失败
空的流式响应会返回 HTTP 502。协议转换未生成任何客户端输出时也会返回相同的状态码。API7 企业版 3.9.19、3.10.6 和 APISIX 3.19.0 支持此行为。使用 ai-proxy-multi 时,该失败可在输出开始前触发已配置的重试。
在 API7 企业版 3.9.19、3.10.7 和 APISIX 3.19.0 中,如果输出开始后发生读取失败,流会直接结束,不会在另一个实例上重试。输出开始后达到响应限制时,流也会直接结束且不会重试。客户端应检测不完整的流;在开始时收到 HTTP 200 并不能证明响应已经成功完成。设置客户端超时和重试策略时,请参阅流持续时间和响应大小限制。
示例
以下示例演示了如何针对不同场景配置 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",
...
}