Call Responses API

מודלי Grok של xAI ב-Gemini Enterprise Agent Platform תומכים ב-Responses API ליצירת תשובות.

בדף הזה מוסבר איך לבצע קריאות למודלים של Grok באמצעות Responses API.

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

כדי להשתמש במודלים של Grok ב-Gemini Enterprise Agent Platform, צריך לבצע את הפעולות הבאות. ‫Gemini Enterprise Agent Platform API (aiplatform.googleapis.com) חייב להיות מופעל.

  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

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

ערך ברירת המחדל של store הוא false, כך שהבקשות הן בלי שמירת מצב, אלא אם מגדירים את store כ-true. בדוגמאות הבאות, הערך של store מוגדר באופן מפורש ל-false (או ל-False ב-Python).

שליחת קריאה ל-Responses API בלי סטרימינג

הדוגמאות הבאות מראות איך לבצע קריאה ל-Responses API בלי סטרימינג:

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Python שמפורטות במאמר התחלה מהירה של Agent Platform באמצעות ספריות לקוח.

כדי לבצע אימות ב-Agent Platform, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

לפני שמריצים את הדוגמה הזו, צריך להגדיר את משתנה הסביבה OPENAI_BASE_URL או להגדיר את פרטי הכניסה ל-OAuth. מידע נוסף זמין במאמר אימות ופרטי כניסה.

import google.auth
import google.auth.transport.requests
from openai import OpenAI
credentials, _ = google.auth.default(
    scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
credentials.refresh(google.auth.transport.requests.Request())
client = OpenAI(
    base_url="https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi",
    api_key=credentials.token,
)
response = client.responses.create(
    model="MODEL",
    input="INPUT",
    max_output_tokens=MAX_OUTPUT_TOKENS,
    stream=False,
    store=False,
)
print(response)
  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫MODEL: שם המודל שבו רוצים להשתמש, לדוגמה xai/grok-4.7.
  • ‫INPUT: ההנחיה או הקלט למודל.
  • ‫MAX_OUTPUT_TOKENS: המספר המקסימלי של טוקנים שאפשר ליצור בתשובה. טוקן הוא בערך ארבעה תווים. ‫100 טוקנים שווים בערך ל-60 עד 80 מילים.

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

REST

אחרי שמגדירים את הסביבה, אפשר להשתמש ב-REST כדי לבדוק פרומפט טקסטואלי. הדוגמה הבאה שולחת בקשה לנקודת הקצה של המודל של בעל התוכן הדיגיטלי.

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

  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫MODEL: שם המודל שבו רוצים להשתמש, לדוגמה xai/grok-4.7.
  • ‫INPUT: ההנחיה או הקלט למודל.
  • ‫MAX_OUTPUT_TOKENS: המספר המקסימלי של טוקנים שאפשר ליצור בתשובה. טוקן הוא בערך ארבעה תווים. ‫100 טוקנים שווים בערך ל-60 עד 80 מילים.

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

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

POST https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses

גוף בקשת JSON:

{
  "model": "MODEL",
  "input": "INPUT",
  "max_output_tokens": MAX_OUTPUT_TOKENS,
  "stream": false,
  "store": false
}

כדי לשלוח את הבקשה עליכם לבחור אחת מהאפשרויות הבאות:

curl

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses"

PowerShell

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses" | Select-Object -Expand Content

בדוגמה הבאה מוצגת בקשת curl מלאה:

curl -s -X POST \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "Content-Type: application/json" \
  "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses" \
  -d '{
    "model": "xai/grok-4.7",
    "input": "Explain black holes in one short sentence.",
    "max_output_tokens": 100,
    "stream": false,
    "store": false
  }'

לפי ההגדרה של Responses API, תשובה ללא סטרימינג תכיל מזהה ייחודי, מטא-נתונים של המודל, נתוני שימוש ומערך פלט שמכיל את הטקסט שנוצר.

{
  "background": false,
  "completed_at": 1778892918,
  "created_at": 1778892916,
  "error": null,
  "frequency_penalty": 0,
  "id": "c8AHavnIMP6UifEPgIfcgAg",
  "incomplete_details": null,
  "instructions": null,
  "max_output_tokens": null,
  "max_tool_calls": null,
  "metadata": {
    "system_fingerprint": "fp_39c5j0a3e9"
  },
  "model": "MODEL",
  "object": "response",
  "output": [
    {
      "content": [
        {
          "annotations": [],
          "logprobs": [],
          "text": "OUTPUT_TEXT",
          "type": "output_text"
        }
      ],
      "id": "msg_c8AHavnIMP6UifEPgIfcgAg",
      "role": "assistant",
      "status": "completed",
      "type": "message"
    }
  ],
  "parallel_tool_calls": true,
  "presence_penalty": 0,
  "previous_response_id": null,
  "prompt_cache_key": null,
  "reasoning": {
    "effort": "medium",
    "summary": "detailed"
  },
  "safety_identifier": null,
  "service_tier": "default",
  "status": "completed",
  "store": false,
  "temperature": 0.7,
  "text": {
    "format": {
      "type": "text"
    }
  },
  "tool_choice": "auto",
  "tools": [],
  "top_logprobs": 0,
  "top_p": 0.95,
  "truncation": "disabled",
  "usage": {
    "extra_properties": {
      "google": {
        "traffic_type": "ON_DEMAND"
      }
    },
    "input_tokens": 335,
    "input_tokens_details": {
      "cached_tokens": 320
    },
    "num_server_side_tools_used": 0,
    "num_sources_used": 0,
    "output_tokens": 305,
    "output_tokens_details": {
      "reasoning_tokens": 284
    },
    "total_tokens": 640
  },
  "user": null
}

שליחת קריאה בסטרימינג ל-Responses API

הדוגמאות הבאות מראות איך לבצע קריאה בסטרימינג ל-Responses API:

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Python שמפורטות במאמר התחלה מהירה של Agent Platform באמצעות ספריות לקוח.

כדי לבצע אימות ב-Agent Platform, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

לפני שמריצים את הדוגמה הזו, צריך להגדיר את משתנה הסביבה OPENAI_BASE_URL או להגדיר את פרטי הכניסה ל-OAuth. מידע נוסף זמין במאמר אימות ופרטי כניסה.

import google.auth
import google.auth.transport.requests
from openai import OpenAI
credentials, _ = google.auth.default(
    scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
credentials.refresh(google.auth.transport.requests.Request())
client = OpenAI(
    base_url="https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi",
    api_key=credentials.token,
)
stream = client.responses.create(
    model="MODEL",
    input="INPUT",
    max_output_tokens=MAX_OUTPUT_TOKENS,
    stream=True,
    store=False,
)
for event in stream:
    if event.type == "response.output_text.delta":
        print(event.delta, end="")
  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫MODEL: שם המודל שבו רוצים להשתמש, לדוגמה xai/grok-4.7.
  • ‫INPUT: ההנחיה או הקלט למודל.
  • ‫MAX_OUTPUT_TOKENS: המספר המקסימלי של טוקנים שאפשר ליצור בתשובה. טוקן הוא בערך ארבעה תווים. ‫100 טוקנים שווים בערך ל-60 עד 80 מילים.

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

REST

אחרי שמגדירים את הסביבה, אפשר להשתמש ב-REST כדי לבדוק פרומפט טקסטואלי. הדוגמה הבאה שולחת בקשה לנקודת הקצה של המודל של בעל התוכן הדיגיטלי.

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

  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫MODEL: שם המודל שבו רוצים להשתמש, לדוגמה xai/grok-4.7.
  • ‫INPUT: ההנחיה או הקלט למודל.
  • ‫MAX_OUTPUT_TOKENS: המספר המקסימלי של טוקנים שאפשר ליצור בתשובה. טוקן הוא בערך ארבעה תווים. ‫100 טוקנים שווים בערך ל-60 עד 80 מילים.

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

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

POST https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses

גוף בקשת JSON:

{
  "model": "MODEL",
  "input": "INPUT",
  "max_output_tokens": MAX_OUTPUT_TOKENS,
  "stream": true,
  "store": false
}

כדי לשלוח את הבקשה עליכם לבחור אחת מהאפשרויות הבאות:

curl

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses"

PowerShell

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses" | Select-Object -Expand Content
 

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

כדי לשלוח בקשות עם שמירת מצב, צריך להגדיר את שירות מדיניות הארגון כך שיאפשר אותן. באופן ספציפי, צריך לעדכן את האילוץ constraints/vertexai.allowedPartnerModelFeatures על ידי הוספת publishers/xai/models/MODEL_NAME:stateful_responses_api לערכים המותרים (לדוגמה, publishers/xai/models/grok-4.7:stateful_responses_api). מידע נוסף זמין במאמר שליטה בגישה למודלים.

‫Responses API תומך בשיחות בכמה שלבים באמצעות בקשות עם שמירת מצב. ערך ברירת המחדל של store הוא false, לכן כדי לשלוח בקשה עם שמירת מצב, צריך להגדיר במפורש את store ל-true. בשיחה עם שמירת מצב, אתם יכולים להתייחס לתשובה קודמת על ידי ציון ה-id שלה בשדה previous_response_id.

יצירת תשובה

הדוגמאות הבאות מראות שיחה עם שני סבבים באמצעות Responses API:

שלב 1: בקשה ראשונית

בפנייה הראשונה, שולחים בקשה שבה store מוגדר ל-true. אם לא תציינו את הפרמטר store, ברירת המחדל תהיה false והתשובה לא תישמר.

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Python שמפורטות במאמר התחלה מהירה של Agent Platform באמצעות ספריות לקוח.

כדי לבצע אימות ב-Agent Platform, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

לפני שמריצים את הדוגמה הזו, צריך להגדיר את משתנה הסביבה OPENAI_BASE_URL או להגדיר את פרטי הכניסה ל-OAuth. מידע נוסף זמין במאמר אימות ופרטי כניסה.

import google.auth
import google.auth.transport.requests
from openai import OpenAI
credentials, _ = google.auth.default(
    scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
credentials.refresh(google.auth.transport.requests.Request())
client = OpenAI(
    base_url="https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi",
    api_key=credentials.token,
)
response = client.responses.create(
    model="MODEL",
    input="randomly pick 3 colors, just return the words of the colors",
    store=True,
)
print(response)
  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫MODEL: שם המודל שבו רוצים להשתמש, לדוגמה xai/grok-4.7.

דוגמה לתשובה לשלב 1:

{
  "background": false,
  "completed_at": 1793834418,
  "created_at": 1793834418,
  "error": null,
  "frequency_penalty": 0,
  "id": "5f76a045-71d9-45af-8b07-1f2feae0675a",
  "incomplete_details": null,
  "instructions": null,
  "max_output_tokens": null,
  "max_tool_calls": null,
  "metadata": {
    "system_fingerprint": "fp_grok47_0"
  },
  "model": "xai/grok-4.7",
  "object": "response",
  "output": [
    {
      "content": [
        {
          "annotations": [],
          "logprobs": [],
          "text": "cerulean amber violet",
          "type": "output_text"
        }
      ],
      "id": "msg_5f76a045-71d9-45af-8b07-1f2feae0675a",
      "role": "assistant",
      "status": "completed",
      "type": "message"
    }
  ],
  "parallel_tool_calls": true,
  "presence_penalty": 0,
  "previous_response_id": null,
  "prompt_cache_key": null,
  "reasoning": {
    "effort": "high",
    "summary": "detailed"
  },
  "safety_identifier": null,
  "service_tier": "default",
  "status": "completed",
  "store": true,
  "temperature": 0.7,
  "text": {
    "format": {
      "type": "text"
    }
  },
  "tool_choice": "auto",
  "tools": [],
  "top_logprobs": 0,
  "top_p": 0.95,
  "truncation": "disabled",
  "usage": {
    "context_details": {
      "input_tokens": 220,
      "output_tokens": 144
    },
    "extra_properties": {
      "google": {
        "traffic_type": "ON_DEMAND"
      }
    },
    "input_tokens": 220,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "num_server_side_tools_used": 0,
    "num_sources_used": 0,
    "output_tokens": 144,
    "output_tokens_details": {
      "reasoning_tokens": 139
    },
    "total_tokens": 364
  },
  "user": null
}

REST

אחרי שמגדירים את הסביבה, אפשר להשתמש ב-REST כדי לבדוק פרומפט טקסטואלי. הדוגמה הבאה שולחת בקשה לנקודת הקצה של המודל של בעל התוכן הדיגיטלי.

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

  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫MODEL: שם המודל שבו רוצים להשתמש, לדוגמה xai/grok-4.7.

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

POST https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses

גוף בקשת JSON:

{
  "model": "MODEL",
  "input": "randomly pick 3 colors, just return the words of the colors",
  "store": true
}

כדי לשלוח את הבקשה עליכם לבחור אחת מהאפשרויות הבאות:

curl

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses"

PowerShell

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses" | Select-Object -Expand Content

בדוגמה הבאה מוצגת בקשת curl מלאה:

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "Content-Type: application/json" \
  "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses" \
  -d '{
    "model": "xai/grok-4.7",
    "input": "randomly pick 3 colors, just return the words of the colors",
    "store": true
  }'

דוגמה לתשובה לשלב 1:

{
  "background": false,
  "completed_at": 1793834418,
  "created_at": 1793834418,
  "error": null,
  "frequency_penalty": 0,
  "id": "5f76a045-71d9-45af-8b07-1f2feae0675a",
  "incomplete_details": null,
  "instructions": null,
  "max_output_tokens": null,
  "max_tool_calls": null,
  "metadata": {
    "system_fingerprint": "fp_grok47_0"
  },
  "model": "xai/grok-4.7",
  "object": "response",
  "output": [
    {
      "content": [
        {
          "annotations": [],
          "logprobs": [],
          "text": "cerulean amber violet",
          "type": "output_text"
        }
      ],
      "id": "msg_5f76a045-71d9-45af-8b07-1f2feae0675a",
      "role": "assistant",
      "status": "completed",
      "type": "message"
    }
  ],
  "parallel_tool_calls": true,
  "presence_penalty": 0,
  "previous_response_id": null,
  "prompt_cache_key": null,
  "reasoning": {
    "effort": "high",
    "summary": "detailed"
  },
  "safety_identifier": null,
  "service_tier": "default",
  "status": "completed",
  "store": true,
  "temperature": 0.7,
  "text": {
    "format": {
      "type": "text"
    }
  },
  "tool_choice": "auto",
  "tools": [],
  "top_logprobs": 0,
  "top_p": 0.95,
  "truncation": "disabled",
  "usage": {
    "context_details": {
      "input_tokens": 220,
      "output_tokens": 144
    },
    "extra_properties": {
      "google": {
        "traffic_type": "ON_DEMAND"
      }
    },
    "input_tokens": 220,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "num_server_side_tools_used": 0,
    "num_sources_used": 0,
    "output_tokens": 144,
    "output_tokens_details": {
      "reasoning_tokens": 139
    },
    "total_tokens": 364
  },
  "user": null
}

שלב 2: בקשת המשך

בפנייה השנייה, מפנים למזהה מהתגובה הקודמת בפרמטר previous_response_id. מגדירים את store ל-true אם רוצים לשמור גם את התשובה הזו, למשל כדי להמשיך את השיחה או לאחזר אותה מאוחר יותר.

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Python שמפורטות במאמר התחלה מהירה של Agent Platform באמצעות ספריות לקוח.

כדי לבצע אימות ב-Agent Platform, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

לפני שמריצים את הדוגמה הזו, צריך להגדיר את משתנה הסביבה OPENAI_BASE_URL או להגדיר את פרטי הכניסה ל-OAuth. מידע נוסף זמין במאמר אימות ופרטי כניסה.

import google.auth
import google.auth.transport.requests
from openai import OpenAI
credentials, _ = google.auth.default(
    scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
credentials.refresh(google.auth.transport.requests.Request())
client = OpenAI(
    base_url="https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi",
    api_key=credentials.token,
)
response = client.responses.create(
    model="MODEL",
    input="What is the second color?",
    previous_response_id="PREVIOUS_RESPONSE_ID",
    store=True,
)
print(response)
  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫MODEL: שם המודל שבו רוצים להשתמש, לדוגמה xai/grok-4.7.
  • ‫PREVIOUS_RESPONSE_ID: המזהה של התשובה הקודמת כדי להמשיך את השיחה.

דוגמה לתשובה לשלב 2:

{
  "background": false,
  "completed_at": 1793835029,
  "created_at": 1793835029,
  "error": null,
  "frequency_penalty": 0,
  "id": "ee8b6aa6-777e-4aa2-8db4-46bbfe84491b",
  "incomplete_details": null,
  "instructions": null,
  "max_output_tokens": null,
  "max_tool_calls": null,
  "metadata": {
    "system_fingerprint": "fp_grok47_0"
  },
  "model": "xai/grok-4.7",
  "object": "response",
  "output": [
    {
      "content": [
        {
          "annotations": [],
          "logprobs": [],
          "text": "amber",
          "type": "output_text"
        }
      ],
      "id": "msg_ee8b6aa6-777e-4aa2-8db4-46bbfe84491b",
      "role": "assistant",
      "status": "completed",
      "type": "message"
    }
  ],
  "parallel_tool_calls": true,
  "presence_penalty": 0,
  "previous_response_id": "5f76a045-71d9-45af-8b07-1f2feae0675a",
  "prompt_cache_key": null,
  "reasoning": {
    "effort": "high",
    "summary": "detailed"
  },
  "safety_identifier": null,
  "service_tier": "default",
  "status": "completed",
  "store": true,
  "temperature": 0.7,
  "text": {
    "format": {
      "type": "text"
    }
  },
  "tool_choice": "auto",
  "tools": [],
  "top_logprobs": 0,
  "top_p": 0.95,
  "truncation": "disabled",
  "usage": {
    "context_details": {
      "input_tokens": 239,
      "output_tokens": 41
    },
    "extra_properties": {
      "google": {
        "traffic_type": "ON_DEMAND"
      }
    },
    "input_tokens": 239,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "num_server_side_tools_used": 0,
    "num_sources_used": 0,
    "output_tokens": 41,
    "output_tokens_details": {
      "reasoning_tokens": 40
    },
    "total_tokens": 280
  },
  "user": null
}

REST

אחרי שמגדירים את הסביבה, אפשר להשתמש ב-REST כדי לבדוק פרומפט טקסטואלי. הדוגמה הבאה שולחת בקשה לנקודת הקצה של המודל של בעל התוכן הדיגיטלי.

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

  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫MODEL: שם המודל שבו רוצים להשתמש, לדוגמה xai/grok-4.7.
  • ‫PREVIOUS_RESPONSE_ID: המזהה של התשובה הקודמת.

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

POST https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses

גוף בקשת JSON:

{
  "model": "MODEL",
  "input": "What is the second color?",
  "previous_response_id": "PREVIOUS_RESPONSE_ID",
  "store": true
}

כדי לשלוח את הבקשה עליכם לבחור אחת מהאפשרויות הבאות:

curl

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses"

PowerShell

שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:

$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses" | Select-Object -Expand Content

בדוגמה הבאה מוצגת בקשת curl מלאה:

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "Content-Type: application/json" \
  "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses" \
  -d '{
    "model": "xai/grok-4.7",
    "input": "What is the second color?",
    "previous_response_id": "5f76a045-71d9-45af-8b07-1f2feae0675a",
    "store": true
  }'

דוגמה לתשובה לשלב 2:

{
  "background": false,
  "completed_at": 1793835029,
  "created_at": 1793835029,
  "error": null,
  "frequency_penalty": 0,
  "id": "ee8b6aa6-777e-4aa2-8db4-46bbfe84491b",
  "incomplete_details": null,
  "instructions": null,
  "max_output_tokens": null,
  "max_tool_calls": null,
  "metadata": {
    "system_fingerprint": "fp_grok47_0"
  },
  "model": "xai/grok-4.7",
  "object": "response",
  "output": [
    {
      "content": [
        {
          "annotations": [],
          "logprobs": [],
          "text": "amber",
          "type": "output_text"
        }
      ],
      "id": "msg_ee8b6aa6-777e-4aa2-8db4-46bbfe84491b",
      "role": "assistant",
      "status": "completed",
      "type": "message"
    }
  ],
  "parallel_tool_calls": true,
  "presence_penalty": 0,
  "previous_response_id": "5f76a045-71d9-45af-8b07-1f2feae0675a",
  "prompt_cache_key": null,
  "reasoning": {
    "effort": "high",
    "summary": "detailed"
  },
  "safety_identifier": null,
  "service_tier": "default",
  "status": "completed",
  "store": true,
  "temperature": 0.7,
  "text": {
    "format": {
      "type": "text"
    }
  },
  "tool_choice": "auto",
  "tools": [],
  "top_logprobs": 0,
  "top_p": 0.95,
  "truncation": "disabled",
  "usage": {
    "context_details": {
      "input_tokens": 239,
      "output_tokens": 41
    },
    "extra_properties": {
      "google": {
        "traffic_type": "ON_DEMAND"
      }
    },
    "input_tokens": 239,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "num_server_side_tools_used": 0,
    "num_sources_used": 0,
    "output_tokens": 41,
    "output_tokens_details": {
      "reasoning_tokens": 40
    },
    "total_tokens": 280
  },
  "user": null
}

קבלת תשובה

אתם יכולים לאחזר תשובה שנוצרה בעבר לפי המזהה שלה.

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Python שמפורטות במאמר התחלה מהירה של Agent Platform באמצעות ספריות לקוח.

כדי לבצע אימות ב-Agent Platform, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

לפני שמריצים את הדוגמה הזו, צריך להגדיר את משתנה הסביבה OPENAI_BASE_URL או להגדיר את פרטי הכניסה ל-OAuth. מידע נוסף זמין במאמר אימות ופרטי כניסה.

import json
import google.auth
import google.auth.transport.requests
from openai import OpenAI
credentials, _ = google.auth.default(
    scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
credentials.refresh(google.auth.transport.requests.Request())
client = OpenAI(
    base_url="https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi",
    api_key=credentials.token,
)
response_id = "RESPONSE_ID"
retrieved_response = client.responses.retrieve(response_id)
print(json.dumps(retrieved_response.model_dump(), indent=2))
  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫RESPONSE_ID: המזהה של התשובה שרוצים לאחזר.

דוגמה לתגובה:

{
  "background": false,
  "completed_at": 1793834418,
  "created_at": 1793834418,
  "error": null,
  "frequency_penalty": 0,
  "id": "5f76a045-71d9-45af-8b07-1f2feae0675a",
  "incomplete_details": null,
  "instructions": null,
  "max_output_tokens": null,
  "max_tool_calls": null,
  "metadata": {
    "system_fingerprint": "fp_grok47_0"
  },
  "model": "xai/grok-4.7",
  "object": "response",
  "output": [
    {
      "content": [
        {
          "annotations": [],
          "logprobs": [],
          "text": "cerulean amber violet",
          "type": "output_text"
        }
      ],
      "id": "msg_5f76a045-71d9-45af-8b07-1f2feae0675a",
      "role": "assistant",
      "status": "completed",
      "type": "message"
    }
  ],
  "parallel_tool_calls": true,
  "presence_penalty": 0,
  "previous_response_id": null,
  "prompt_cache_key": null,
  "reasoning": {
    "effort": "high",
    "summary": "detailed"
  },
  "safety_identifier": null,
  "service_tier": "default",
  "status": "completed",
  "store": true,
  "temperature": 0.7,
  "text": {
    "format": {
      "type": "text"
    }
  },
  "tool_choice": "auto",
  "tools": [],
  "top_logprobs": 0,
  "top_p": 0.95,
  "truncation": "disabled",
  "usage": {
    "context_details": {
      "input_tokens": 220,
      "output_tokens": 144
    },
    "extra_properties": {
      "google": {
        "traffic_type": "ON_DEMAND"
      }
    },
    "input_tokens": 220,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "num_server_side_tools_used": 0,
    "num_sources_used": 0,
    "output_tokens": 144,
    "output_tokens_details": {
      "reasoning_tokens": 139
    },
    "total_tokens": 364
  },
  "user": null
}

REST

אחרי שמגדירים את הסביבה, אפשר להשתמש ב-REST כדי לבדוק פרומפט טקסטואלי. הדוגמה הבאה שולחת בקשה לנקודת הקצה של המודל של בעל התוכן הדיגיטלי.

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

  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫RESPONSE_ID: המזהה של התשובה שרוצים לאחזר.

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

GET https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/RESPONSE_ID

כדי לשלוח את הבקשה אתם צריכים לבחור אחת מהאפשרויות הבאות:

curl

מריצים את הפקודה הבאה:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
"https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/RESPONSE_ID"

PowerShell

מריצים את הפקודה הבאה:

$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/RESPONSE_ID" | Select-Object -Expand Content

בדוגמה הבאה מוצגת בקשת curl מלאה:

curl -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "Content-Type: application/json" \
  "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/5f76a045-71d9-45af-8b07-1f2feae0675a"

דוגמה לתגובה:

{
  "background": false,
  "completed_at": 1793834418,
  "created_at": 1793834418,
  "error": null,
  "frequency_penalty": 0,
  "id": "5f76a045-71d9-45af-8b07-1f2feae0675a",
  "incomplete_details": null,
  "instructions": null,
  "max_output_tokens": null,
  "max_tool_calls": null,
  "metadata": {
    "system_fingerprint": "fp_grok47_0"
  },
  "model": "xai/grok-4.7",
  "object": "response",
  "output": [
    {
      "content": [
        {
          "annotations": [],
          "logprobs": [],
          "text": "cerulean amber violet",
          "type": "output_text"
        }
      ],
      "id": "msg_5f76a045-71d9-45af-8b07-1f2feae0675a",
      "role": "assistant",
      "status": "completed",
      "type": "message"
    }
  ],
  "parallel_tool_calls": true,
  "presence_penalty": 0,
  "previous_response_id": null,
  "prompt_cache_key": null,
  "reasoning": {
    "effort": "high",
    "summary": "detailed"
  },
  "safety_identifier": null,
  "service_tier": "default",
  "status": "completed",
  "store": true,
  "temperature": 0.7,
  "text": {
    "format": {
      "type": "text"
    }
  },
  "tool_choice": "auto",
  "tools": [],
  "top_logprobs": 0,
  "top_p": 0.95,
  "truncation": "disabled",
  "usage": {
    "context_details": {
      "input_tokens": 220,
      "output_tokens": 144
    },
    "extra_properties": {
      "google": {
        "traffic_type": "ON_DEMAND"
      }
    },
    "input_tokens": 220,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "num_server_side_tools_used": 0,
    "num_sources_used": 0,
    "output_tokens": 144,
    "output_tokens_details": {
      "reasoning_tokens": 139
    },
    "total_tokens": 364
  },
  "user": null
}

מחיקת תשובה

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

אחרי שמוחקים תשובה, אי אפשר לאחזר אותה או להשתמש במזהה שלה בתור previous_response_id בבקשה חדשה. מחיקת תשובה לא מסירה את התוכן שלה מתשובות מאוחרות יותר באותה שיחה: כל תשובה שמפנה אליה דרך previous_response_id שומרת את התוכן הזה כהקשר עד שמוחקים גם את התשובה הזו. כדי להסיר שיחה שלמה, צריך למחוק את כל התשובות ששמורות בה.

Python

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Python שמפורטות במאמר התחלה מהירה של Agent Platform באמצעות ספריות לקוח.

כדי לבצע אימות ב-Agent Platform, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

לפני שמריצים את הדוגמה הזו, צריך להגדיר את משתנה הסביבה OPENAI_BASE_URL או להגדיר את פרטי הכניסה ל-OAuth. מידע נוסף זמין במאמר אימות ופרטי כניסה.

import google.auth
import google.auth.transport.requests
from openai import OpenAI
credentials, _ = google.auth.default(
    scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
credentials.refresh(google.auth.transport.requests.Request())
client = OpenAI(
    base_url="https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi",
    api_key=credentials.token,
)
response_id = "RESPONSE_ID"
delete_response = client.responses.delete(response_id)
print(delete_response)
  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫RESPONSE_ID: המזהה של התשובה שרוצים למחוק.

דוגמה לתגובה:

{
  "deleted": true,
  "id": "5f76a045-71d9-45af-8b07-1f2feae0675a",
  "object": "response"
}

REST

אחרי שמגדירים את הסביבה, אפשר להשתמש ב-REST כדי לבדוק פרומפט טקסטואלי. הדוגמה הבאה שולחת בקשה לנקודת הקצה של המודל של בעל התוכן הדיגיטלי.

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

  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫RESPONSE_ID: המזהה של התגובה שרוצים למחוק.

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

DELETE https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/RESPONSE_ID

כדי לשלוח את הבקשה אתם צריכים לבחור אחת מהאפשרויות הבאות:

curl

מריצים את הפקודה הבאה:

curl -X DELETE \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
"https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/RESPONSE_ID"

PowerShell

מריצים את הפקודה הבאה:

$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method DELETE `
-Headers $headers `
-Uri "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/RESPONSE_ID" | Select-Object -Expand Content

בדוגמה הבאה מוצגת בקשת curl מלאה:

curl -X DELETE \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "Content-Type: application/json" \
  "https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/5f76a045-71d9-45af-8b07-1f2feae0675a"

דוגמה לתגובה:

{
  "deleted": true,
  "id": "5f76a045-71d9-45af-8b07-1f2feae0675a",
  "object": "response"
}

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