Responses API

Gemini Enterprise Agent Platform 上的 OpenAI 合作夥伴模型支援 OpenAI Responses API (publishers/openai/v1/responses),可根據文字和圖片輸入生成文字、呼叫工具、控制推論工作,以及壓縮對話。

事前準備

如要透過 Gemini Enterprise Agent Platform 使用 OpenAI 合作夥伴模型,請完成下列步驟。您的 Cloud de Confiance 專案必須啟用 Gemini Enterprise Agent Platform API (aiplatform.googleapis.com)。

  1. In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Cloud de Confiance project.

  3. 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.enable permission. 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.

    Enable the API

  4. 完成 OpenAI GPT 模型的新手上路步驟,包括透過 Google Cloud Marketplace 購買產品,以及在 Model Garden 啟用模型。

  5. 如果貴組織的政策限制模型存取權,請在政策中允許模型。

必要的角色

如要取得呼叫 OpenAI 合作夥伴模型所需的權限,請要求管理員授予您專案的代理平台使用者 (roles/aiplatform.user) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

端點

OpenAI 合作夥伴模型是透過下列 Agent Platform 基準網址提供:

  • 全域端點 (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> 替換為 Cloud de Confiance 專案 ID。

使用 OpenAI Python SDK 呼叫 Responses API

您可以使用官方 openai Python 程式庫,方法是將 base_url 設為代理平台端點,並將更新後的 Cloud de Confiance by S3NS 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

您可以搭配 Cloud de Confiance by S3NS OAuth 憑證使用官方 openai Node.js 程式庫:

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 程式庫搭配 Cloud de Confiance by S3NS 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,直接將 REST 要求傳送至 Responses API。

使用任何要求資料之前,請先修改下列項目的值:

  • PROJECT_ID: Cloud de Confiance 專案 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 Cloud de Confiance by S3NS and OpenAI terms.",
  "reasoning": {"effort": "medium"},
  "store": false
}

請展開以下其中一個選項,以傳送要求:

您應該會收到執行成功的狀態碼 (2xx) 和空白回應。

的專案層級核准。

錯誤處理

在實際工作環境工作負載中,建構 429 和 5xx 回應的指數輪詢 (含隨機延遲)。

狀態碼 可能原因 建議做法
401 存取權杖已過期或無效。 請重新整理 Cloud de Confiance by S3NS 存取權杖,然後再次提出要求。
403 呼叫端身分缺少必要權限,或 API、專案或模型未啟用。 確認 IAM 角色、API 啟用狀態、專案許可清單和 Model Garden 啟用狀態。
404 端點路徑、位置或模型 ID 不正確。 請確認基準網址、位置區隔和模型名稱。
429 已達頻率、配額或可用容量上限。 以指數輪詢方式重試。如果問題持續發生,請與帳戶團隊聯絡。
5xx 伺服器暫時性狀況。 以指數輪詢方式重試。

將 Codex 連結至 Cloud de Confiance by S3NS

您可以設定 OpenAI Codex (CLI 或電腦版應用程式),使用 Cloud de Confiance by S3NS 驗證,透過 Cloud de Confiance 專案轉送模型要求。如需完整的設定操作說明、互動式登入步驟和手動 config.toml 選項,請參閱「將 OpenAI Codex 連線至Cloud de Confiance by S3NS」。

後續步驟