Gemini Enterprise Agent Platform 上的 XAI Grok 模型支持用于生成回答的 Responses API。
本页介绍了如何使用 Responses API 调用 Grok 模型。
准备工作
如需将 Grok 模型与 Gemini Enterprise Agent Platform 搭配使用,您必须执行以下步骤。必须启用 Gemini Enterprise Agent Platform API (aiplatform.googleapis.com)。
-
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.
对 Responses API 进行非流式调用
以下示例展示了如何对 Responses API 进行非流式调用:
Python
试用此示例之前,请按照《Agent Platform 快速入门:使用客户端库》中的 Python 设置说明进行操作。
如需向代理平台进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
在运行此示例之前,请务必设置 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, ) print(response)
- MODEL:您要使用的模型名称,例如
xai/grok-4.20-reasoning。 - INPUT:模型的提示或输入。
- MAX_OUTPUT_TOKENS:回答中可生成的词元数量上限。词元约为 4 个字符。100 个词元对应大约 60-80 个单词。
指定较低的值可获得较短的回答,指定较高的值可获得可能较长的回答。
REST
设置环境后,您可以使用 REST 测试文本提示。以下示例会向发布方模型端点发送请求。
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目 ID。
- MODEL:您要使用的模型名称,例如
xai/grok-4.20-reasoning。 - INPUT:模型的提示或输入。
- MAX_OUTPUT_TOKENS:回答中可生成的词元数量上限。词元约为 4 个字符。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
}
如需发送请求,请选择以下方式之一:
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 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 }'
根据 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 设置说明进行操作。
如需向代理平台进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
在运行此示例之前,请务必设置 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, ) 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:回答中可生成的词元数量上限。词元约为 4 个字符。100 个词元对应大约 60-80 个单词。
指定较低的值可获得较短的回答,指定较高的值可获得可能较长的回答。
REST
设置环境后,您可以使用 REST 测试文本提示。以下示例会向发布方模型端点发送请求。
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目 ID。
- MODEL:您要使用的模型名称,例如
xai/grok-4.20-reasoning。 - INPUT:模型的提示或输入。
- MAX_OUTPUT_TOKENS:回答中可生成的词元数量上限。词元约为 4 个字符。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
}
如需发送请求,请选择以下方式之一:
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
后续步骤
- 详细了解 Grok 模型。
- 了解如何将函数调用与 Responses API 搭配使用。
- 了解如何使用 Responses API 进行结构化输出。