代理 Gemini Enterprise Agent Platform 请求
Gemini Enterprise Agent Platform 是此前文档中称为 Vertex AI 的 Google Cloud 模型平台的现用名称。它提供 Gemini 及合作伙伴模型,包括 OpenAI 兼容的 Chat Completions API。
本指南介绍如何为 Agent Platform 配置 ai-proxy 插件。已发布的 APISIX 服务提供方标识符仍为 vertex-ai。本指南通过 override.endpoint 设置文档指定的全局 Chat Completions URL,因为已发布的驱动程序仅会根据 provider_conf.region 推导区域主机名。
前置条件
- 安装 Docker。
- 安装 cURL 以向服务发送请求进行验证。
- 按照 快速入门教程 在 Docker 或 Kubernetes 中启动一个新的 APISIX 实例。
- 拥有一个已启用结算功能及 Agent Platform API 的 Google Cloud 项目。
- 如果使用 Admin API 示例且启用了身份验证,请将管理员角色的密钥导出为
ADMIN_API_KEY。请在deployment.admin.admin_key下配置该密钥 。Docker 快速入门为了便于本地评估而禁用了身份验证;生产环境中请保持启用。
获取 Agent Platform 服务账户密钥
创建服务账户,为其授予 Agent Platform User(roles/aiplatform.user)角色,并创建 JSON 服务账户密钥。该插件需要使用下载的密钥,因为它不会自动发现应用默认凭证(Application Default Credentials)或 Workload Identity Federation 凭证。
你可以选择将服务账户 JSON 保存到环境变量:
export GCP_SERVICE_ACCOUNT_JSON="$(cat /path/to/service-account.json)"
创建到 Agent Platform 的路由
创建一个带有 ai-proxy 插件的路由,如下所示:
- Admin API
- ADC
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
--data-binary @- <<EOF
{
"id": "vertex-ai-chat",
"uri": "/anything",
"plugins": {
"ai-proxy": {
"provider": "vertex-ai",
"override": {
"endpoint": "https://aiplatform.googleapis.com/v1beta1/projects/<GCP_PROJECT_ID>/locations/global/endpoints/openapi/chat/completions"
},
"auth": {
"gcp": {
"service_account_json": "$GCP_SERVICE_ACCOUNT_JSON"
}
},
"options": {
"model": "google/gemini-3.5-flash"
}
}
}
}
EOF
❶ 将服务提供方设置为 vertex-ai,并使用文档指定的全局 Chat Completions URL 覆盖其端点。将 <GCP_PROJECT_ID> 替换为你的项目 ID。
❷ 替换为你的服务账户 JSON。
❸ 设置一个在全局端点上支持 Chat Completions 的 Agent Platform 模型。示例使用 google/gemini-3.5-flash,该模型在 global 区域支持默认的 Standard PayGo 消费模式。
services:
- name: Agent Platform Service
routes:
- uris:
- /anything
name: vertex-ai-chat
plugins:
ai-proxy:
provider: vertex-ai
override:
endpoint: https://aiplatform.googleapis.com/v1beta1/projects/<GCP_PROJECT_ID>/locations/global/endpoints/openapi/chat/completions
auth:
gcp:
service_account_json: ${GCP_SERVICE_ACCOUNT_JSON}
options:
model: google/gemini-3.5-flash
❶ 将服务提供方设置为 vertex-ai,并使用文档指定的全局 Chat Completions URL 覆盖其端点。将 <GCP_PROJECT_ID> 替换为你的项目 ID。
❷ 替换为你的服务账户 JSON。
❸ 设置一个在全局端点上支持 Chat Completions 的 Agent Platform 模型。示例使用 google/gemini-3.5-flash,该模型在 global 区域支持默认的 Standard PayGo 消费模式。
将配置同步到 APISIX:
adc sync -f adc.yaml
验证
向路由发送带有以下提示的请求:
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-3.5-flash",
...
}
下一步
你已将 APISIX 连接到 Gemini Enterprise Agent Platform。有关支持的模型和请求字段,请参阅 Agent Platform 模型目录和 OpenAI 兼容的 Chat Completions 文档。