Responses API

המודלים של OpenAI שזמינים ב-Gemini Enterprise Agent Platform תומכים ב-OpenAI Responses API (publishers/openai/v1/responses) ליצירת טקסט מקלט של טקסט ותמונות, קריאות לפונקציות, שליטה במאמץ החשיבה ודחיסת שיחות.

לפני שמתחילים

כדי להשתמש במודלים של OpenAI ב-Gemini Enterprise Agent Platform, צריך לבצע את הפעולות הבאות. ‫Gemini Enterprise Agent Platform API‏ (aiplatform.googleapis.com) צריך להיות מופעל בפרויקט ב- Google Cloud .

  1. נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloudליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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

  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. 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

  8. משלימים את שלבי ההגדרה ב-OpenAI GPT Models, כולל רכישת המוצר דרך Google Cloud Marketplace והפעלת המודל ב-Model Garden.

  9. אם המדיניות של הארגון מגבילה את הגישה למודל, צריך לאשר את המודל במדיניות הזו.

התפקידים הנדרשים

כדי לקבל את ההרשאות שנדרשות לקריאה למודלים של שותפי OpenAI, בקשו מהאדמין להקצות לכם את התפקיד משתמש בפלטפורמת Agent (roles/aiplatform.user) ב-IAM בפרויקט. כדי לקרוא הסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.

נקודות קצה

המודלים של השותפים של OpenAI מוגשים מכתובות ה-URL הבסיסיות הבאות של פלטפורמת הסוכנים:

  • נקודת קצה גלובלית (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 .

שליחת קריאה ל-Responses API באמצעות OpenAI Python SDK

אפשר להשתמש בספריית openai הרשמית של Python על ידי הגדרת base_url לנקודת הקצה של Agent Platform והעברת אסימון גישה מרוענן של Google Cloud OAuth 2.0 בתור api_key. מגדירים את store=False לבקשות ללא שמירת מצב (stateless) ולעומסי עבודה של ZDR.

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)

שליחת קריאה ל-Responses API באמצעות OpenAI Node.js SDK

אתם יכולים להשתמש בספריית 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);

שליחת קריאה ל-Responses API באמצעות OpenAI Go SDK

אפשר להשתמש בספרייה הרשמית של 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())
}

שליחת קריאה ל-Responses API באמצעות curl

אתם יכולים לשלוח בקשת REST ישירות ל-Responses API באמצעות curl.

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .

ה-method של ה-HTTP וכתובת ה-URL:

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 הנתיב של נקודת הקצה, המיקום או המזהה של המודל שגויים. בודקים את כתובת ה-URL הבסיסית, את פלח המיקום ואת שם המודל.
429 הגעתם למגבלה של קצב, מכסה או קיבולת זמינה. השהיה מעריכית לפני ניסיון חוזר (exponential backoff). אם הבעיה נמשכת, פנו לצוות ניהול החשבון.
5xx מצב זמני בשרת. נסו שוב עם השהיה מעריכית לפני ניסיון חוזר (exponential backoff).

חיבור Codex אל Google Cloud

אתם יכולים להגדיר את OpenAI Codex (אפליקציה למחשב או CLI) כדי לנתב בקשות למודלים דרך פרויקט Google Cloud באמצעות אימות Google Cloud . במאמר קישור OpenAI Codex ל-Google Cloud מפורטות הוראות מלאות להגדרה, שלבים אינטראקטיביים לכניסה ואפשרויות ידניות ל-config.toml.

המאמרים הבאים