Google Cloud API Gateway支持在OpenAPI 3.x规范中配置模型路由规则,自动将请求转码并分发至Gemini、Claude或OpenAI OSS-GPT等不同模型,开发者无需硬编码端点或自建代理层。
在构建 AI 应用时,开发者需要能够灵活地将流量路由到最适合当前任务的模型,而不是硬编码端点或自行管理开源代理。Google Cloud API Gateway 现已推出模型路由功能,处于公开预览阶段。该功能提供了一层轻量、无服务器的入口层,接收 OpenAI 兼容的请求,并动态将其路由到 Gemini、Claude 或 OpenAI OSS-GPT。
API Gateway 可以独立使用,用于简单的限速和令牌追踪;也可以与 Gemini Enterprise Agent Platform 无缝配合。例如,你可以将 Agent 的出口流量通过 Agent Gateway 进行严格的安全治理,然后再将请求转发给 API Gateway,由其处理到 Google 托管 LLM 的动态路由。以下是配置路由逻辑的分步指南。
配置模型路由逻辑只需几个步骤:
你可以在 OpenAPI 3.x 规范中直接使用新的 x-google-api-management 扩展块,将虚拟模型名称映射到特定的后端目标。
openapi: 3.0.4
info:
title: OpenAPI 3.x spec using Model Routing
description: Using Model Routing in an OAS 3.x spec
version: 1.0.0
x-google-api-management:
backends:
gemini-35-flashlite:
address: >-
https://aiplatform.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/publishers/google/models/gemini-3.5-flash-lite:generateContent
deadline: 60.0
pathTranslation: CONSTANT_ADDRESS
anthropic-claude-opus-47:
address: >-
https://aiplatform.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/publishers/anthropic/models/claude-opus-4-7:rawPredict
deadline: 60.0
pathTranslation: CONSTANT_ADDRESS
openai-gpt-oss-120b:
address: >-
https://aiplatform.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/endpoints/openapi/chat/completions
deadline: 60.0
pathTranslation: CONSTANT_ADDRESS
ai:
models:
routing:
routers:
# Router 1: route between Gemini (default) and Claude.
gemini-claude-router:
defaultModel:
backend: gemini-35-flashlite
targetModel: google/gemini-3.5-flash-lite
rules:
- model: "claude-opus-4-7"
backend: anthropic-claude-opus-47
targetModel: anthropic/claude-opus-4-7
# Router 2: route between OpenAI GPT (default) and Gemini.
openai-gemini-router:
defaultModel:
backend: openai-gpt-oss-120b
targetModel: openai/gpt-oss-120b-maas
rules:
- model: "gemini-3.5-flash-lite"
backend: gemini-35-flashlite
targetModel: google/gemini-3.5-flash-lite
servers:
- url: "https://my-gateway-url.com"
paths:
/v1/chat/gemini-claude:
post:
summary: "Endpoint:defaults to Gemini & Claude as an option."
operationId: "chatGeminiClaude"
x-google-model-router: gemini-claude-router
responses:
'200':
description: "OK"
/v1/chat/openai-gemini:
post:
summary: "Endpoint:defaults to OpenAI & Gemini as an option."
operationId: "chatOpenAIGemini"
x-google-model-router: openai-gemini-router
responses:
'200':
description: "OK"
注意:单个路由器引用的所有后端必须共享同一个主机(例如 aiplatform.googleapis.com)。路由会在该共享的 Vertex 主机上选择不同的模型和路径,而不会跨不同主机进行路由。
部署更新后的 API 配置,使网关处于活跃状态并准备好处理流量。
你的应用只需发送标准的 OpenAI POST /v1/chat/gemini-claude 或 POST /v1/chat/openai-gemini 请求。网关会拦截请求,将载荷转码为后端的原生模式,并动态进行路由。例如(请为 $API_KEY 和 my-gateway-url.com 使用适当的值):
curl -X POST "https://my-gateway-url.com/v1/chat/gemini-claude" \
-H "content-type: application/json" \
-H "x-api-key: $API_KEY" \
-d '{
"model": "claude-opus-4-7",
"messages": [
{"role": "user", "content": "Introduce yourself in 5 words"}
]
}'
模型路由功能现已以公开预览形式在 API Gateway 中可用。想要摆脱代理管理的繁琐、开始统一你的 AI 流量,请查阅我们的文档,立即部署你的第一个模型路由器。