用於存取 Claude 的 Vertex API 與 Messages API 幾乎完全相同,並支援所有相同的選項,但有兩個主要差異:

  • 在 Vertex 中,model 不會在請求主體中傳遞。相反,它在 Google Cloud 端點 URL 中指定。
  • 在 Vertex 中,anthropic_version 在請求主體中傳遞(而不是作為標頭),並且必須設定為值 vertex-2023-10-16

Vertex 也受到 Anthropic 官方 客戶端 SDK 的支援。本指南將引導您完成在 Python 或 TypeScript 中向 Vertex AI 上的 Claude 發出請求的過程。

請注意,本指南假設您已經擁有能夠使用 Vertex AI 的 GCP 專案。有關所需設定的更多資訊以及完整的逐步說明,請參閱 使用來自 Anthropic 的 Claude 3 模型

安裝用於存取 Vertex AI 的 SDK

首先,為您選擇的語言安裝 Anthropic 的 客戶端 SDK

pip install -U google-cloud-aiplatform "anthropic[vertex]"

存取 Vertex AI

模型可用性

請注意,Anthropic 模型的可用性因地區而異。在 Vertex AI Model Garden 中搜尋「Claude」或前往 使用 Claude 3 以獲取最新資訊。

API 模型名稱

模型Vertex AI API 模型名稱
Claude Opus 4claude-opus-4@20250514
Claude Sonnet 4claude-sonnet-4@20250514
Claude Sonnet 3.7claude-3-7-sonnet@20250219
Claude Haiku 3.5claude-3-5-haiku@20241022
Claude Sonnet 3.5claude-3-5-sonnet-v2@20241022
Claude Opus 3 (公開預覽)claude-3-opus@20240229
Claude Sonnet 3claude-3-sonnet@20240229
Claude Haiku 3claude-3-haiku@20240307

發出請求

在執行請求之前,您可能需要執行 gcloud auth application-default login 以使用 GCP 進行身份驗證。

以下範例顯示如何從 Vertex AI 上的 Claude 生成文字:

from anthropic import AnthropicVertex

project_id = "MY_PROJECT_ID"
# 模型執行的位置
region = "us-east5"

client = AnthropicVertex(project_id=project_id, region=region)

message = client.messages.create(
    model="claude-opus-4@20250514",
    max_tokens=100,
    messages=[
        {
            "role": "user",
            "content": "Hey Claude!",
        }
    ],
)
print(message)

有關更多詳細資訊,請參閱我們的 客戶端 SDK 和官方 Vertex AI 文件

活動記錄

Vertex 提供 請求-回應記錄服務,允許客戶記錄與您的使用相關的提示和完成。

Anthropic 建議您至少以 30 天滾動基礎記錄您的活動,以便了解您的活動並調查任何潛在的濫用。

開啟此服務不會讓 Google 或 Anthropic 存取您的內容。

功能支援

您可以在 這裡 找到 Vertex 目前支援的所有功能。