Gemini Enterprise Agent Platform 上的 OpenAI 合作伙伴模型支持 OpenAI Responses API (publishers/openai/v1/responses),可用于根据文本和图片输入生成文本、调用工具、控制推理工作量以及压缩对话。
准备工作
如需将 OpenAI 合作伙伴模型与 Gemini Enterprise Agent Platform 搭配使用,您必须完成以下步骤。必须在 Google Cloud 项目中启用 Gemini Enterprise Agent Platform API (aiplatform.googleapis.com)。
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Gemini Enterprise Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Gemini Enterprise Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
完成 OpenAI GPT 模型中的初始配置步骤,包括通过 Google Cloud Marketplace 购买产品并在 Model Garden 中启用模型。
-
如果贵组织的政策限制了模型访问权限,请在该政策中允许使用该模型。
所需的角色
如需获得调用 OpenAI 合作伙伴模型所需的权限,请让您的管理员为您授予项目的 Agent Platform User (roles/aiplatform.user) IAM 角色。
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
端点
OpenAI 合作伙伴模型通过以下智能体平台基础网址提供:
- 全球端点 (
global):https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/publishers/openai/v1 - 美国多区域端点 (
us):https://aiplatform.us.rep.googleapis.com/v1/projects/PROJECT_ID/locations/us/publishers/openai/v1
请将 <var>PROJECT_ID</var> 替换为您的 Google Cloud 项目 ID。
使用 OpenAI Python SDK 调用 Responses API
您可以使用官方 openai Python 库,方法是将 base_url 设置为您的 Agent Platform 端点,并将刷新的 Google Cloud OAuth 2.0 访问令牌作为 api_key 传递。为无状态请求和 ZDR 工作负载设置 store=False。
import google.auth from google.auth.transport.requests import Request from openai import OpenAI credentials, _ = google.auth.default( scopes=["https://www.googleapis.com/auth/cloud-platform"] ) credentials.refresh(Request()) project_id = "PROJECT_ID" base_url = ( f"https://aiplatform.googleapis.com/v1/projects/{project_id}" "/locations/global/publishers/openai/v1" ) client = OpenAI( base_url=base_url, api_key=credentials.token, ) response = client.responses.create( model="gpt-6.1-sol", input="Explain how Zero Data Retention works on Agent Platform.", reasoning={"effort": "medium"}, store=False, ) print(response.output_text)
流式传输响应
如需通过服务器发送的事件 (SSE) 流式传输增量文本,请设置 stream=True:
import google.auth from google.auth.transport.requests import Request from openai import OpenAI credentials, _ = google.auth.default( scopes=["https://www.googleapis.com/auth/cloud-platform"] ) credentials.refresh(Request()) project_id = "PROJECT_ID" base_url = ( f"https://aiplatform.googleapis.com/v1/projects/{project_id}" "/locations/global/publishers/openai/v1" ) client = OpenAI( base_url=base_url, api_key=credentials.token, ) stream = client.responses.create( model="gpt-6-luna", input="Write a short Python function to validate a JSON schema.", stream=True, store=False, ) for event in stream: if event.type == "response.output_text.delta": print(event.delta, end="")
紧凑的对话记录
对于长跨度智能体对话,请调用独立的压缩端点 (POST .../responses/compact),将之前的消息压缩为加密的压缩项,以便传递到后续的无状态 /responses 调用中:
import google.auth from google.auth.transport.requests import Request from openai import OpenAI credentials, _ = google.auth.default( scopes=["https://www.googleapis.com/auth/cloud-platform"] ) credentials.refresh(Request()) project_id = "PROJECT_ID" base_url = ( f"https://aiplatform.googleapis.com/v1/projects/{project_id}" "/locations/global/publishers/openai/v1" ) client = OpenAI( base_url=base_url, api_key=credentials.token, ) compacted = client.responses.compact( model="gpt-6-astra", input=[ {"role": "user", "content": "Audit our multi-region VPC firewall rules."}, {"role": "assistant", "content": "I checked the ingress rules for us-east1 and us-central1."}, ], ) print(compacted.output)
使用 OpenAI Node.js SDK 调用 Responses API
您可以将官方 openai Node.js 库与 Google Cloud OAuth 凭据搭配使用:
import OpenAI from "openai"; const projectId = "PROJECT_ID"; const client = new OpenAI({ apiKey: process.env.GOOGLE_CLOUD_ACCESS_TOKEN, baseURL: `https://aiplatform.googleapis.com/v1/projects/${projectId}/locations/global/publishers/openai/v1`, }); const response = await client.responses.create({ model: "gpt-6-luna", input: "Write a short summary of how authentication works on Agent Platform.", store: false, }); console.log(response.output_text);
使用 OpenAI Go SDK 调用 Responses API
您可以将官方 openai-go 库与 Google Cloud OAuth 凭据搭配使用:
package main import ( "context" "fmt" "os" "github.com/openai/openai-go/v3" "github.com/openai/openai-go/v3/option" "github.com/openai/openai-go/v3/responses" ) func main() { ctx := context.Background() projectID := "PROJECT_ID" client := openai.NewClient( option.WithAPIKey(os.Getenv("GOOGLE_CLOUD_ACCESS_TOKEN")), option.WithBaseURL("https://aiplatform.googleapis.com/v1/projects/"+projectID+"/locations/global/publishers/openai/v1"), ) response, err := client.Responses.New(ctx, responses.ResponseNewParams{ Model: "gpt-6-luna", Input: responses.ResponseNewParamsInputUnion{ OfString: openai.String("Write a one-sentence bedtime story about a unicorn."), }, Store: openai.Bool(false), }) if err != nil { panic(err) } fmt.Println(response.OutputText()) }
使用 curl 调用 Responses API
您可以使用 curl 直接向 Responses API 发送 REST 请求。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID:您的 Google Cloud 项目 ID。
HTTP 方法和网址:
POST https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/publishers/openai/v1/responses
请求 JSON 正文:
{
"model": "gpt-6.1-sol",
"input": "Summarize the key differences between Google Cloud and OpenAI terms.",
"reasoning": {"effort": "medium"},
"store": false
}
如需发送您的请求,请展开以下选项之一:
您应该会收到一个成功的状态代码 (2xx) 和一个空响应。
错误处理
在生产工作负载中,针对 429 和 5xx 响应构建具有抖动的指数退避算法。
| 状态代码 | 可能的原因 | 建议采取的操作 |
|---|---|---|
401 |
访问令牌已过期或无效。 | 刷新 Google Cloud 访问令牌,然后重试请求。 |
403 |
调用方身份缺少所需权限,或者 API、项目或模型未启用。 | 验证 IAM 角色、API 启用情况、项目许可清单和 Model Garden 启用情况。 |
404 |
端点路径、位置或模型标识符不正确。 | 验证基础网址、位置细分和模型名称。 |
429 |
已达到速率、配额或可用容量上限。 | 请使用指数退避算法重试。如果持续出现此问题,请与您的客户支持团队联系。 |
5xx |
服务器出现暂时性问题。 | 请使用指数退避算法重试。 |
将 Codex 关联到 Google Cloud
您可以配置 OpenAI Codex(CLI 或桌面应用),以使用 Google Cloud 身份验证通过 Google Cloud 项目路由模型请求。如需查看完整的设置说明、交互式登录步骤和手动 config.toml 选项,请参阅将 OpenAI Codex 连接到Google Cloud。
后续步骤
- 在将 OpenAI Codex 连接到Google Cloud 中配置开发者工具。
- 在 OpenAI GPT 模型中查看模型规范、ZDR 流程和管辖条款。
- 了解滥用行为监控和零数据保留。