Gemini Enterprise Agent Platform 的 XAI Grok 模型支援 Responses API,可生成回覆。
本頁說明如何使用 Responses API 呼叫 Grok 模型。
事前準備
如要在 Gemini Enterprise Agent Platform 中使用 Grok 模型,請按照下列步驟操作。必須啟用 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.
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.
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.
對 Responses API 發出無狀態呼叫
如要使用無狀態功能,請在要求中明確將 store 設為 false (或 Python 中的 False)。store 的預設值為 true。
對 Responses API 發出非串流呼叫
下列範例說明如何對 Responses API 進行非串流呼叫:
Python
在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Python 設定說明操作。
如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
執行這個範例前,請務必設定 OPENAI_BASE_URL 環境變數或設定 OAuth 認證。
詳情請參閱「驗證和憑證」。
from openai import OpenAI client = OpenAI() response = client.responses.create( model="MODEL", input="INPUT", max_output_tokens=MAX_OUTPUT_TOKENS, stream=False, store=False, ) print(response)
- MODEL:要使用的模型名稱,例如
xai/grok-4.20-reasoning。 - INPUT:模型的提示或輸入內容。
- MAX_OUTPUT_TOKENS:回覆內可以生成的權杖數量上限。一個符記約為四個字元。100 個符記約等於 60 到 80 個字。
如要取得較短的回覆,請指定較低的值;如要取得可能較長的回覆,請調高此值。
REST
設定環境後,您可以使用 REST 測試文字提示詞。下列範例會將要求傳送至發布商模型端點。
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- MODEL:要使用的模型名稱,例如
xai/grok-4.20-reasoning。 - INPUT:模型的提示或輸入內容。
- MAX_OUTPUT_TOKENS:回覆內可以生成的權杖數量上限。一個符記約為四個字元。100 個符記約等於 60 到 80 個字。
如要取得較短的回覆,請指定較低的值;如要取得可能較長的回覆,請調高此值。
HTTP 方法和網址:
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 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 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.20-reasoning", "input": "Explain black holes in one short sentence.", "max_output_tokens": 100, "stream": false, "store": false }'
根據 Responses API 定義,非串流回應會包含專屬 ID、模型中繼資料、用量統計資料,以及包含生成文字的輸出陣列。
{
"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
在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Python 設定說明操作。
如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
執行這個範例前,請務必設定 OPENAI_BASE_URL 環境變數或設定 OAuth 認證。
詳情請參閱「驗證和憑證」。
from openai import OpenAI client = OpenAI() 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="")
- MODEL:要使用的模型名稱,例如
xai/grok-4.20-reasoning。 - INPUT:模型的提示或輸入內容。
- MAX_OUTPUT_TOKENS:回覆內可以生成的權杖數量上限。一個符記約為四個字元。100 個符記約等於 60 到 80 個字。
如要取得較短的回覆,請指定較低的值;如要取得可能較長的回覆,請調高此值。
REST
設定環境後,您可以使用 REST 測試文字提示詞。下列範例會將要求傳送至發布商模型端點。
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- MODEL:要使用的模型名稱,例如
xai/grok-4.20-reasoning。 - INPUT:模型的提示或輸入內容。
- MAX_OUTPUT_TOKENS:回覆內可以生成的權杖數量上限。一個符記約為四個字元。100 個符記約等於 60 到 80 個字。
如要取得較短的回覆,請指定較低的值;如要取得可能較長的回覆,請調高此值。
HTTP 方法和網址:
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 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 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.20-reasoning:stateful_responses_api)。詳情請參閱「控管模型存取權」。
Responses API 支援透過具狀態的要求進行多輪對話。預設值為 true,可啟用有狀態的回應。store在有狀態的對話中,您可以在 previous_response_id 欄位中提供先前回覆的 id,藉此參考先前回覆。
下列範例顯示使用 Responses API 的兩輪對話:
第 1 輪:初始要求
在第一回合中,請發出要求,並將 store 設為 true。您也可以省略 store 參數,因為 true 是預設值。
Python
在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Python 設定說明操作。
如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
執行這個範例前,請務必設定 OPENAI_BASE_URL 環境變數或設定 OAuth 認證。
詳情請參閱「驗證和憑證」。
from openai import OpenAI client = OpenAI() response = client.responses.create( model="MODEL", input="randomly pick 3 colors, just return the words of the colors", ) print(response)
- MODEL:要使用的模型名稱,例如
xai/grok-4.20-reasoning。
REST
設定環境後,您可以使用 REST 測試文字提示詞。下列範例會將要求傳送至發布商模型端點。
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- MODEL:要使用的模型名稱,例如
xai/grok-4.20-reasoning。
HTTP 方法和網址:
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"
}
如要傳送要求,請選擇以下其中一個選項:
curl
將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth 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 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.20-reasoning", "input": "randomly pick 3 colors, just return the words of the colors" }'
第 1 輪的回應範例:
{
"background": false,
"completed_at": 1780435592,
"created_at": 1780435591,
"error": null,
"frequency_penalty": 0,
"id": "hkofavjWJuaRifEPntCBiA8",
"incomplete_details": null,
"instructions": null,
"max_output_tokens": null,
"max_tool_calls": null,
"metadata": {
"system_fingerprint": "fp_39c5j0a3e9"
},
"model": "xai/grok-4.20-reasoning",
"object": "response",
"output": [
{
"content": [
{
"annotations": [],
"logprobs": [],
"text": "violet teal crimson",
"type": "output_text"
}
],
"id": "msg_hkofavjWJuaRifEPntCBiA8",
"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": true,
"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": 343,
"input_tokens_details": {
"cached_tokens": 320
},
"num_server_side_tools_used": 0,
"num_sources_used": 0,
"output_tokens": 389,
"output_tokens_details": {
"reasoning_tokens": 386
},
"total_tokens": 732
},
"user": null
}
第 2 輪:後續要求
在第二輪中,請在 previous_response_id 參數中參照先前回應中的 ID。
Python
在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Python 設定說明操作。
如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
執行這個範例前,請務必設定 OPENAI_BASE_URL 環境變數或設定 OAuth 認證。
詳情請參閱「驗證和憑證」。
from openai import OpenAI client = OpenAI() response = client.responses.create( model="MODEL", input="What is the second color?", previous_response_id="PREVIOUS_RESPONSE_ID", ) print(response)
- MODEL:要使用的模型名稱,例如
xai/grok-4.20-reasoning。 - PREVIOUS_RESPONSE_ID:要繼續對話的先前回覆 ID。
REST
設定環境後,您可以使用 REST 測試文字提示詞。下列範例會將要求傳送至發布商模型端點。
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- MODEL:要使用的模型名稱,例如
xai/grok-4.20-reasoning。 - PREVIOUS_RESPONSE_ID:先前回覆的 ID。
HTTP 方法和網址:
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"
}
如要傳送要求,請選擇以下其中一個選項:
curl
將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth 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 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.20-reasoning", "input": "What is the second color?", "previous_response_id": "hkofavjWJuaRifEPntCBiA8" }'
第 2 輪的回應範例:
{
"background": false,
"completed_at": 1780436062,
"created_at": 1780436060,
"error": null,
"frequency_penalty": 0,
"id": "VkwfatGAFY2CifEPvPu-iQM",
"incomplete_details": null,
"instructions": null,
"max_output_tokens": null,
"max_tool_calls": null,
"metadata": {
"system_fingerprint": "fp_39c5j0a3e9"
},
"model": "xai/grok-4.20-reasoning",
"object": "response",
"output": [
{
"content": [
{
"annotations": [],
"logprobs": [],
"text": "teal",
"type": "output_text"
}
],
"id": "msg_VkwfatGAFY2CifEPvPu-iQM",
"role": "assistant",
"status": "completed",
"type": "message"
}
],
"parallel_tool_calls": true,
"presence_penalty": 0,
"previous_response_id": "hkofavjWJuaRifEPntCBiA8",
"prompt_cache_key": null,
"reasoning": {
"effort": "medium",
"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": {
"extra_properties": {
"google": {
"traffic_type": "ON_DEMAND"
}
},
"input_tokens": 360,
"input_tokens_details": {
"cached_tokens": 320
},
"num_server_side_tools_used": 0,
"num_sources_used": 0,
"output_tokens": 314,
"output_tokens_details": {
"reasoning_tokens": 312
},
"total_tokens": 674
},
"user": null
}
取得回覆
您可以透過 ID 擷取先前生成的內容。
Python
在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Python 設定說明操作。
如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
執行這個範例前,請務必設定 OPENAI_BASE_URL 環境變數或設定 OAuth 認證。
詳情請參閱「驗證和憑證」。
import json import openai v1beta1_client = openai.OpenAI( base_url=f"https://aiplatform.googleapis.com/v1beta1/projects/{project_id}/locations/global/endpoints/openapi", api_key=credentials.token, )response_id = "RESPONSE_ID" retrieved_response = v1beta1_client.responses.retrieve(response_id)
print(json.dumps(retrieved_response.model_dump(), indent=2))
- RESPONSE_ID:要擷取的回覆 ID。
回應範例:
{ "id": "pfgxarvYI4C0hMIP-qns4AU", "created_at": 1781659813.0, "error": null, "incomplete_details": null, "instructions": null, "metadata": { "system_fingerprint": "fp_39c5j0a3e9" }, "model": "xai/grok-4.20-reasoning", "object": "response", "output": [ { "id": "msg_pfgxarvYI4C0hMIP-qns4AU", "content": [ { "annotations": [], "text": "Once upon a twilight meadow, a gentle unicorn named Luna followed a trail of glowing fireflies to her mossy bed beneath the silver moon, where she dreamed of rainbow bridges and endless starlit gallops.", "type": "output_text", "logprobs": [] } ], "role": "assistant", "status": "completed", "type": "message", "phase": null } ], "parallel_tool_calls": true, "temperature": 0.7, "tool_choice": "auto", "tools": [], "top_p": 0.95, "background": false, "completed_at": 1781659815.0, "conversation": null, "max_output_tokens": null, "max_tool_calls": null, "moderation": null, "previous_response_id": null, "prompt": null, "prompt_cache_key": null, "prompt_cache_retention": null, "reasoning": { "effort": "medium", "generate_summary": null, "summary": "detailed" }, "safety_identifier": null, "service_tier": "default", "status": "completed", "text": { "format": { "type": "text" }, "verbosity": null }, "top_logprobs": 0, "truncation": "disabled", "usage": { "input_tokens": 340, "input_tokens_details": { "cached_tokens": 0 }, "output_tokens": 375, "output_tokens_details": { "reasoning_tokens": 334 }, "total_tokens": 715, "extra_properties": { "google": { "traffic_type": "ON_DEMAND" } }, "num_server_side_tools_used": 0, "num_sources_used": 0 }, "user": null, "frequency_penalty": 0, "presence_penalty": 0, "store": true }
REST
設定環境後,您可以使用 REST 測試文字提示詞。下列範例會將要求傳送至發布商模型端點。
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- RESPONSE_ID:要擷取的回覆 ID。
HTTP 方法和網址:
GET https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/RESPONSE_ID
如要傳送要求,請選擇以下其中一個選項:
curl
執行下列指令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/RESPONSE_ID"
PowerShell
執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://aiplatform.googleapis.com/v1beta1/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/v1beta1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/VkwfatGAFY2CifEPvPu-iQM"
回應範例:
{
"background": false,
"completed_at": 1780436062,
"created_at": 1780436060,
"error": null,
"frequency_penalty": 0,
"id": "VkwfatGAFY2CifEPvPu-iQM",
"incomplete_details": null,
"instructions": null,
"max_output_tokens": null,
"max_tool_calls": null,
"metadata": {
"system_fingerprint": "fp_39c5j0a3e9"
},
"model": "xai/grok-4.20-reasoning",
"object": "response",
"output": [
{
"content": [
{
"annotations": [],
"logprobs": [],
"text": "teal",
"type": "output_text"
}
],
"id": "msg_VkwfatGAFY2CifEPvPu-iQM",
"role": "assistant",
"status": "completed",
"type": "message"
}
],
"parallel_tool_calls": true,
"presence_penalty": 0,
"previous_response_id": "hkofavjWJuaRifEPntCBiA8",
"prompt_cache_key": null,
"reasoning": {
"effort": "medium",
"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": {
"extra_properties": {
"google": {
"traffic_type": "ON_DEMAND"
}
},
"input_tokens": 360,
"input_tokens_details": {
"cached_tokens": 320
},
"num_server_side_tools_used": 0,
"num_sources_used": 0,
"output_tokens": 314,
"output_tokens_details": {
"reasoning_tokens": 312
},
"total_tokens": 674
},
"user": null
}
刪除回覆
您可以依 ID 刪除先前生成的內容。
Python
在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Python 設定說明操作。
如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
執行這個範例前,請務必設定 OPENAI_BASE_URL 環境變數或設定 OAuth 認證。
詳情請參閱「驗證和憑證」。
import openai v1beta1_client = openai.OpenAI( base_url=f"https://aiplatform.googleapis.com/v1beta1/projects/{project_id}/locations/global/endpoints/openapi", api_key=credentials.token, )response_id = "RESPONSE_ID" delete_response = v1beta1_client.responses.delete(response_id)
print(delete_response)
- RESPONSE_ID:要刪除的回覆 ID。
回應範例:
{ "deleted": true, "id": "TekCarmCCOPYyOgPnrWSgAY", "object": "response" }
REST
設定環境後,您可以使用 REST 測試文字提示詞。下列範例會將要求傳送至發布商模型端點。
使用任何要求資料之前,請先修改下列項目的值:
- PROJECT_ID:您的 Google Cloud 專案 ID。
- RESPONSE_ID:要刪除的回覆 ID。
HTTP 方法和網址:
DELETE https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/RESPONSE_ID
如要傳送要求,請選擇以下其中一個選項:
curl
執行下列指令:
curl -X DELETE \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/RESPONSE_ID"
PowerShell
執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method DELETE `
-Headers $headers `
-Uri "https://aiplatform.googleapis.com/v1beta1/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/v1beta1/projects/PROJECT_ID/locations/global/endpoints/openapi/responses/TekCarmCCOPYyOgPnrWSgAY"
回應範例:
{
"deleted": true,
"id": "TekCarmCCOPYyOgPnrWSgAY",
"object": "response"
}
後續步驟
- 進一步瞭解 Grok 模型。
- 瞭解如何搭配 Responses API 使用函式呼叫。
- 瞭解如何使用 Responses API 取得結構化輸出內容。