社区集成

Google Cloud Vertex AI

通过 Google Cloud 托管平台访问 xAI Grok 模型,并获得企业级安全、治理和统一计费。

本指南介绍如何在 Google Cloud Vertex AI / Gemini Enterprise Agent Platform 上设置和使用 Grok 模型。Vertex AI 中的 Grok 作为 Partner Model,通过兼容 OpenAI 的 API 访问,包括 Responses API 和 Chat Completions。模型通过 Model Garden 启用。

前置条件

开始前,请确保具备:

  • 已启用计费的有效 Google Cloud Platform(GCP)Project。

  • 启用 API 和访问 Model Garden 的权限,例如 Vertex AI User 或 Project Editor Role。

  • Project 中已启用 aiplatform.googleapis.com API 或等效的 Agent Platform API。

  • 已安装 Google Cloud CLI(gcloud),并完成 Application Default Credentials(ADC)身份验证。

设置 ADC 和 Project:

Bash

gcloud auth application-default login
gcloud config set project YOUR_PROJECT_ID

如果尚未启用所需 API,请执行:

Bash

gcloud services enable aiplatform.googleapis.com

安装所需 Package

Bash

pip install -U openai google-cloud-aiplatform

在 Model Garden 中启用 Grok 模型

  1. 前往 Google Cloud Console 的 Model Garden,或在 Console 中搜索“Model Garden”。

  2. 搜索“Grok”,或按 Publisher xAI 浏览。

  3. 选择所需的 Grok 模型,例如 Grok 4.2 或 Grok 4.3。

  4. 查看 Model Card 中的能力、quota、价格和 Region 信息。

  5. 点击 EnableDeploy / request access(如果出现提示)。

  6. 启用后,即可通过 API 调用该模型。

请使用 Model Garden 中显示的 Model ID。Vertex model name 可能包含 Publisher Prefix,例如:

  • xai/grok-4.5

模型可用性通常与 xAI API 一致,但受 Google Cloud Region 可用性和 quota 限制。

发起首次 API 调用

Vertex 上的 Grok 使用兼容 OpenAI 的 interface,可以使用标准 openai Python Library。

身份验证

使用 Application Default Credentials。Client 可以读取 gcloud 身份验证或 Service Account Credential。

你可能需要通过 Environment Variable 或直接在 Client 中设置兼容 Vertex/OpenAI 的 Base URL 或 Endpoint。请使用 Model Card 或 Google Agent Platform 文档提供的精确 endpoint。

Bash

export OPENAI_BASE_URL="https://YOUR_VERTEX_ENDPOINT"

Responses API 示例

Python (OpenAI)

from openai import OpenAI

client = OpenAI()  # Uses ADC / env vars automatically

response = client.responses.create(
    model="xai/grok-4.5",
    input="Explain the advantages of using Grok for agentic workflows with parallel tool calling.",
    max_output_tokens=800,
)

print(response.output_text)

Chat Completions 示例

Python (OpenAI)

from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(
    model="xai/grok-4.5",
    messages=[
        {
            "role": "user",
            "content": "Which city has a higher temperature right now, Boston or New Delhi, and by how much in Fahrenheit?",
        }
    ],
    tools=[
        {
            "type": "function",
            "function": {
                "name": "get_current_weather",
                "description": "Get the current weather in a given location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g., San Francisco, CA",
                        },
                        "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                    },
                    "required": ["location"],
                },
            },
        }
    ],
    tool_choice="auto",
)

print(response.choices[0].message.content)

两个 interface 均支持 Streaming,以获得更低延迟的体验。

Function Calling 与 Tool Use

Grok 在 Responses API 和 Chat Completions interface 中都擅长 Tool Use 和 Parallel Function Calling。请为 Tool 定义清晰、严格的 schema,使模型能够可靠选择和调用。

数据保留与合规

Google Cloud 上 Grok 模型的数据保留和处理受 Google Cloud Vertex AI Policy 约束。

  • 许多 deployment 支持 Zero Data Retention(ZDR)选项。

  • 请查看具体 Model Card 和组织的 Google Cloud Data Governance 设置。

  • 可以启用 Vertex AI Request-Response Logging,用于审计和调试。

详情请参阅 Google Cloud 关于 Vertex AI Data Governance 和 Logging 的文档。

功能支持

支持的能力包括:

  • Responses API 和 Chat Completions。

  • Function Calling 和 Tool Use,包括 Parallel Function Calling。

  • Reasoning Mode / Extended Thinking。

  • Structured Output / JSON Mode。

  • Streaming。

  • 通过 Google Cloud 提供固定 quota 和 Committed Use Discount。

Context window 因模型而异。请在 Model Garden 中查看具体 Grok Model Card 了解当前限制。

Global、Multi-region 与 Regional Endpoint

Vertex AI / Gemini Enterprise Agent Platform 提供灵活的 endpoint routing:

  • Global Endpoint:通过 Dynamic Routing 实现最高可用性,推荐用于大多数场景。

  • Regional Endpoint:通过指定 Region 路由,以满足严格的合规要求。

最佳实践

  • 选择与延迟、throughput 和 reasoning 要求匹配的 Grok 模型及 endpoint 配置。

  • 优先使用 Application Default Credentials 和 IAM Role,而不是长期有效的 Key。Production workload 应使用 Service Account。

  • 在 Google Cloud Billing 和 Quotas 页面监控用量,并按需申请提高 quota。

  • 使用清晰的 Tool Schema 和明确的 Output Format。

  • 启用 Request Logging,并与 Google Cloud Monitoring / Logging 集成。

  • 从直接调用 xAI API 迁移时,请更新 Base URL、Client 配置和 Model Prefix。大多数 prompt 和 Tool Definition 只需少量更改即可迁移。

故障排查

问题检查项
身份验证错误运行 gcloud auth application-default login,并验证 Project Permission。
找不到模型确认模型已在 Model Garden 中启用,并使用精确的 xai/... ID。
超出 quota在 Google Cloud Console 中检查 quota,并按需申请提高。
Endpoint / Base URL 问题使用 Model Card 或 Google 文档中的精确 endpoint 或 Environment Variable。

如果可用,请先在 Google Cloud Console Playground / Model Garden interface 中开始,再转到代码。

后续步骤

  • 在 Model Garden 中探索已启用的模型。

  • 利用 Grok 的 Tool Calling 优势构建 Agent 应用。

  • 与 Cloud Functions、Vertex AI Pipelines 等 Google Cloud Service 集成。

  • 查看完整的 xAI Grok 文档和 Model Card,了解 prompting 技巧与能力。