您可以使用 Anthropic 的 SDK 或 curl 命令通过以下模型名称向 Gemini Enterprise Agent Platform 端点发送请求:
- 对于 Google Cloud 上的 Claude Sonnet 5,请使用
claude-sonnet-5 - 对于 Google Cloud 上的 Claude Opus 5,请使用
claude-opus-5 - 对于 Google Cloud 上的 Claude Fable 5,请使用
claude-fable-5 - 对于 Google Cloud 上的 Claude Opus 4.8,请使用
claude-opus-4-8 - 对于 Google Cloud 上的 Claude Opus 4.7,请使用
claude-opus-4-7 - 对于 Google Cloud 上的 Claude Opus 4.6,请使用
claude-opus-4-6 - 对于 Google Cloud 上的 Claude Sonnet 4.6,请使用
claude-sonnet-4-6 - 对于 Google Cloud 上的 Claude Opus 4.5,请使用
claude-opus-4-5 - 对于 Google Cloud 上的 Claude Opus 4.1,请使用
claude-opus-4-1 - 对于 Google Cloud 上的 Claude Opus 4,请使用
claude-opus-4 - 对于 Google Cloud 上的 Claude Sonnet 4.5,请使用
claude-sonnet-4-5 - 对于 Google Cloud 上的 Claude Sonnet 4,请使用
claude-sonnet-4 - 对于 Google Cloud 上的 Claude 3.7 Sonnet,请使用
claude-3-7-sonnet - 对于 Google Cloud 上的 Claude 3.5 Sonnet v2,请使用
claude-3-5-sonnet-v2 - 对于 Google Cloud 上的 Claude Haiku 4.5,请使用
claude-haiku-4-5 - 对于 Google Cloud 上的 Claude 3.5 Haiku,请使用
claude-3-5-haiku - 对于 Google Cloud 上的 Claude 3.5 Sonnet,请使用
claude-3-5-sonnet - 对于 Google Cloud 上的 Claude 3 Opus,请使用
claude-3-opus - 对于 Google Cloud 上的 Claude 3 Haiku,请使用
claude-3-haiku
准备工作
Anthropic 实施了相关政策,禁止某些经销商转售其产品。如果您的 Google Cloud 结算账号由禁止的经销商管理,您将无法接受服务条款或启用 Claude 模型。如果出现这种情况,请直接与您的转销商联系。
如需将 Anthropic Claude 模型与 Gemini Enterprise Agent Platform 搭配使用,您必须执行以下步骤。必须启用 Agent Platform API (aiplatform.googleapis.com) 才能使用 Gemini Enterprise Agent Platform。如果您已有启用了 Agent Platform API 的项目,则可以使用该项目,而无需创建新项目。
确保您拥有启用和使用合作伙伴模型所需的权限。如需了解详情,请参阅授予所需权限。
- 登录您的 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.-
前往以下 Model Garden 模型卡片之一,然后点击启用:
- 前往 Google Cloud 上的 Claude Sonnet 5 模型卡片
- 前往 Google Cloud 上的 Claude Opus 5 模型卡片
- 前往 Google Cloud 上的 Claude Fable 5 模型卡片
- 前往 Google Cloud 上的 Claude Opus 4.8 模型卡片
- 前往 Google Cloud 上的 Claude Opus 4.7 模型卡片
- 前往 Google Cloud 上的 Claude Opus 4.6 模型卡片
- 前往 Google Cloud 上的 Claude Sonnet 4.6 模型卡片
- 前往 Google Cloud 上的 Claude Opus 4.5 模型卡片
- 前往 Google Cloud 上的 Claude Opus 4.1 模型卡片
- 前往 Google Cloud 上的 Claude Opus 4 模型卡片
- 前往 Google Cloud 上的 Claude Sonnet 4.5 模型卡片
- 前往 Google Cloud 上的 Claude Sonnet 4 模型卡片
- 前往 Google Cloud 上的 Claude Haiku 4.5 模型卡片
- 前往 Google Cloud 上的 Claude 3.5 Haiku 模型卡片
Anthropic 建议您启用提示和完成活动的 30 天日志记录,以记录任何模型滥用行为。如需启用日志记录,请参阅记录请求和响应。
使用 Anthropic SDK
您可以使用 Anthropic Claude SDK 向 Anthropic Claude 模型发出 API 请求。如需了解详情,请参阅以下内容:
- Claude Messages API 参考文档
- Anthropic Python API 库
- Anthropic Gemini Enterprise Agent Platform TypeScript API 库
使用 Anthropic Vertex SDK 对 Claude 模型进行流式调用
以下代码示例使用 Anthropic Vertex SDK 对 Claude 模型执行流式调用。
Python
如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档。
以下示例使用区域级端点。如需使用多区域端点或全球端点,请参阅指定多区域端点或指定全球端点。使用 Anthropic Vertex SDK 对 Claude 模型进行一元调用
以下代码示例使用 Anthropic Vertex SDK 对 Claude 模型执行一元调用。
Python
如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档。
以下示例使用区域级端点。如需使用多区域端点或全球端点,请参阅指定多区域端点或指定全球端点。使用 curl 命令
您可以使用 curl 命令向 Gemini Enterprise Agent Platform 端点发出请求。curl 命令指定要使用的受支持的 Claude 模型。
以下主题介绍如何创建 curl 命令并包含示例 curl 命令。
REST
如需使用 Agent Platform API 测试文本提示,请向发布方模型端点发送 POST 请求。
以下示例使用区域级端点。如需使用多区域端点或全球端点,请参阅指定多区域端点或指定全球端点。在使用任何请求数据之前,请先进行以下替换:
- LOCATION:支持 Anthropic Claude 模型的区域。如需使用全球端点,请参阅指定全球端点。
- MODEL:您要使用的模型名称。
- ROLE:与消息关联的角色。您可以指定
user或assistant。第一条消息必须使用user角色。 Claude 模型使用交替的user和assistant回合运行。如果最终消息使用assistant角色,则回答内容会立即从该消息中的内容继续。您可以使用它来限制模型的部分回答。 - STREAM:一个布尔值,用于指定是否流式传输回答。流式传输您的回答,以降低对最终使用延迟的感知度。设置为
true可流式传输回答,设置为false可一次性返回所有回答。 - CONTENT:
user或assistant消息的内容(如文本)。 - MAX_TOKENS:回答中可生成的词元数量上限。一个词元约为 3.5 个字符。100 个词元对应大约 60-80 个单词。
指定较低的值可获得较短的回答,指定较高的值可获得可能较长的回答。
- TOP_P(可选):Top-p 可更改模型选择输出词元的方式。系统会按照概率从最高到最低的顺序选择 token,直到所选 token 的概率总和等于 top-P 值。例如,如果词元 A、B 和 C 的概率分别为 0.3、0.2 和 0.1,并且 top-P 值为
0.5,则模型将选择 A 或 B 作为下一个词元(通过温度确定),并会排除 C,将其作为候选词元。指定较低的值可获得随机程度较低的回答,指定较高的值可获得随机程度较高的回答。
- TOP_K(可选):Top-K 可更改模型选择输出词元的方式。如果 top-K 设为
1,表示所选词元是模型词汇表的所有词元中概率最高的词元(也称为贪心解码)。如果 top-K 设为3,则表示系统将从 3 个概率最高的词元(通过温度确定)中选择下一个词元。在每个词元选择步骤中,系统都会对概率最高的 top-K 词元进行采样。然后,系统会根据 top-P 进一步过滤词元,并使用温度采样选择最终的词元。
指定较低的值可获得随机程度较低的回答,指定较高的值可获得随机程度较高的回答。
- TYPE:(适用于 Google Cloud 上的 Claude 3.7 Sonnet 和更新的 Claude 模型)如需启用扩展思考模式,请指定
enabled。 - BUDGET_TOKENS:如果您启用了扩展思考,则必须指定模型可用于其内部推理作为输出内容的一部分的 token 数。更大的预算可以让您对复杂问题进行更全面的分析,并提高回答质量。您必须指定一个大于或等于
1024但小于MAX_TOKENS的值。
HTTP 方法和网址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/MODEL:streamRawPredict
请求 JSON 正文:
{
"anthropic_version": "vertex-2023-10-16",
"messages": [
{
"role": "ROLE",
"content": "CONTENT"
}],
"max_tokens": MAX_TOKENS,
"stream": STREAM,
"thinking": {
"type": "TYPE",
"budget_tokens": BUDGET_TOKENS
}
}
如需发送请求,请选择以下方式之一:
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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/MODEL:streamRawPredict"
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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/MODEL:streamRawPredict" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。
示例 curl 命令
MODEL_ID="MODEL"
LOCATION="us-central1"
PROJECT_ID="PROJECT_ID"
curl \
-X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/anthropic/models/${MODEL_ID}:streamRawPredict -d \
'{
"anthropic_version": "vertex-2023-10-16",
"messages": [{
"role": "user",
"content": "Hello!"
}],
"max_tokens": 50,
"stream": true}'
工具使用(函数调用)
Anthropic Claude 模型支持工具和函数调用,以增强模型的功能。如需了解详情,请参阅 Anthropic 文档中的工具使用概览。Gemini Enterprise Agent Platform 上的 Claude 模型支持客户端工具,但不支持服务器工具。
以下示例演示了如何使用 SDK 或 curl 命令来使用工具。这些示例会搜索旧金山附近正在营业的餐厅。
Python
如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档。
以下示例使用区域级端点。如需使用多区域端点或全球端点,请参阅指定多区域端点或指定全球端点。REST
以下示例使用区域级端点。如需使用多区域端点或全球端点,请参阅指定多区域端点或指定全球端点。在使用任何请求数据之前,请先进行以下替换:
- LOCATION:支持 Anthropic Claude 模型的区域。如需使用全球端点,请参阅指定全球端点。
- MODEL:要使用的模型名称。
- ROLE:与消息关联的角色。您可以指定
user或assistant。第一条消息必须使用user角色。 Claude 模型使用交替的user和assistant回合运行。如果最终消息使用assistant角色,则回答内容会立即从该消息中的内容继续。您可以使用它来限制模型的部分回答。 - STREAM:一个布尔值,用于指定是否流式传输回答。流式传输您的回答,以降低对最终使用延迟的感知度。设置为
true可流式传输回答,设置为false可一次性返回所有回答。 - CONTENT:
user或assistant消息的内容(如文本)。 - MAX_TOKENS:响应中可生成的词元数量上限。一个词元约为 3.5 个字符。100 个词元对应大约 60-80 个单词。
指定较低的值可获得较短的回答,指定较高的值可获得可能较长的回答。
HTTP 方法和网址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/MODEL:rawPredict
请求 JSON 正文:
{
"anthropic_version": "vertex-2023-10-16",
"max_tokens": MAX_TOKENS,
"stream": STREAM,
"tools": [
{
"name": "text_search_places_api",
"description": "Returns information about a set of places based on a string",
"input_schema": {
"type": "object",
"properties": {
"textQuery": {
"type": "string",
"description": "The text string on which to search"
},
"priceLevels": {
"type": "array",
"description": "Price levels to query places, value can be one of [PRICE_LEVEL_INEXPENSIVE, PRICE_LEVEL_MODERATE, PRICE_LEVEL_EXPENSIVE, PRICE_LEVEL_VERY_EXPENSIVE]",
},
"openNow": {
"type": "boolean",
"description": "Describes whether a place is open for business at
the time of the query."
},
},
"required": ["textQuery"]
}
}
],
"messages": [
{
"role": "user",
"content": "What are some affordable and good Italian restaurants that are open now in San Francisco??"
}
]
}
如需发送请求,请选择以下方式之一:
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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/MODEL:rawPredict"
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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/MODEL:rawPredict" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。
在 Gemini Enterprise Agent Platform 上使用 Agent Studio
对于某些 Anthropic Claude 模型,您可以使用 Gemini Enterprise Agent Platform 上的 Agent Studio 在 Google Cloud 控制台中快速对生成式 AI 模型进行原型开发和测试。例如,您可以使用 Gemini Enterprise Agent Platform 上的 Agent Studio 将 Claude 模型的回答与其他受支持的模型(例如 Google Gemini)的回答进行比较。
如需了解详情,请参阅快速入门:使用 Gemini Enterprise Agent Platform 上的 Agent Studio 向 Gemini 发送文本提示。