在 Model Garden 中使用模型

在Google Cloud 控制台中,使用 Model Garden 发现、测试、调优和部署模型。您还可以使用 Google Cloud CLI 部署 Model Garden 模型。

发送测试提示

  1. 在 Google Cloud 控制台中,前往 Model Garden 页面。

    转到 Model Garden

  2. 找到要测试的受支持模型,然后点击查看详细信息

  3. 点击打开提示设计

    您将转到提示设计页面。

  4. 提示符中,输入要测试的提示。

  5. 可选:配置模型参数。

  6. 点击提交

调整模型

  1. 在 Google Cloud 控制台中,前往 Model Garden 页面。

    转到 Model Garden

  2. 搜索模型中,输入 BERTT5-FLAN,然后点击放大镜以进行搜索。

  3. 点击 T5-FLANBERT 模型卡片上的查看详细信息

  4. 点击打开微调流水线

    您将进入 Vertex AI 流水线页面。

  5. 如需开始调优,请点击创建运行

在笔记本中调优

大多数开源基础模型和可微调模型的模型卡都支持在笔记本中进行调整。

  1. 在 Google Cloud 控制台中,前往 Model Garden 页面。

    转到 Model Garden

  2. 找到要调优的受支持模型,然后前往其模型卡片。

  3. 点击打开笔记本

部署开放模型

您可以使用 Google Cloud 控制台中的模型卡片或以编程方式部署模型。

如需详细了解如何设置 Google Gen AI SDK 或 Google Cloud CLI,请参阅 Google Gen AI SDK 概览或安装 Google Cloud CLI

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档

  1. 列出可部署的模型,并记录要部署的模型的 ID。您可以选择列出 Model Garden 中支持的 Hugging Face 模型,甚至可以按模型名称过滤这些模型。输出不包含任何经过调优的模型。

     import vertexai from vertexai import model_garden  # TODO(developer): Update and un-comment below lines # PROJECT_ID = "your-project-id" vertexai.init(project=PROJECT_ID, location="us-central1")  # List deployable models, optionally list Hugging Face models only or filter by model name. deployable_models = model_garden.list_deployable_models(list_hf_models=False, model_filter="gemma") print(deployable_models) # Example response: # ['google/gemma2@gemma-2-27b','google/gemma2@gemma-2-27b-it', ...] 
  2. 使用上一步中的模型 ID 查看模型的部署规范。您可以查看 Model Garden 针对特定模型验证过的机器类型、加速器类型和容器映像 URI。

     import vertexai from vertexai import model_garden  # TODO(developer): Update and un-comment below lines # PROJECT_ID = "your-project-id" # model = "google/gemma3@gemma-3-1b-it" vertexai.init(project=PROJECT_ID, location="us-central1")  # For Hugging Face modelsm the format is the Hugging Face model name, as in # "meta-llama/Llama-3.3-70B-Instruct". # Go to https://console.cloud.google.com/vertex-ai/model-garden to find all deployable # model names.  model = model_garden.OpenModel(model) deploy_options = model.list_deploy_options() print(deploy_options) # Example response: # [ #   dedicated_resources { #     machine_spec { #       machine_type: "g2-standard-12" #       accelerator_type: NVIDIA_L4 #       accelerator_count: 1 #     } #   } #   container_spec { #     ... #   } #   ... # ] 
  3. 将模型部署到端点。除非您指定其他参数和值,否则 Model Garden 会使用默认部署配置。

     import vertexai from vertexai import model_garden  # TODO(developer): Update and un-comment below lines # PROJECT_ID = "your-project-id" vertexai.init(project=PROJECT_ID, location="us-central1")  open_model = model_garden.OpenModel("google/gemma3@gemma-3-12b-it") endpoint = open_model.deploy(     machine_type="g2-standard-48",     accelerator_type="NVIDIA_L4",     accelerator_count=4,     accept_eula=True, )  # Optional. Run predictions on the deployed endoint. # endpoint.predict(instances=[{"prompt": "What is Generative AI?"}]) 

gcloud

在开始之前,请指定一个配额项目来运行以下命令。您运行的命令会计入相应项目的配额。如需了解详情,请参阅设置配额项目

  1. 运行 gcloud ai model-garden models list 命令,列出可部署的模型。此命令会列出所有模型 ID 以及您可以自行部署的模型的 ID。

    gcloud ai model-garden models list 

    在输出中,找到要部署的模型的 ID。以下示例显示了简略版输出。

    MODEL_ID                                      CAN_DEPLOY  CAN_PREDICT google/gemma2@gemma-2-27b                     Yes         No google/gemma2@gemma-2-27b-it                  Yes         No google/gemma2@gemma-2-2b                      Yes         No google/gemma2@gemma-2-2b-it                   Yes         No google/gemma2@gemma-2-9b                      Yes         No google/gemma2@gemma-2-9b-it                   Yes         No google/gemma3@gemma-3-12b-it                  Yes         No google/gemma3@gemma-3-12b-pt                  Yes         No google/gemma3@gemma-3-1b-it                   Yes         No google/gemma3@gemma-3-1b-pt                   Yes         No google/gemma3@gemma-3-27b-it                  Yes         No google/gemma3@gemma-3-27b-pt                  Yes         No google/gemma3@gemma-3-4b-it                   Yes         No google/gemma3@gemma-3-4b-pt                   Yes         No google/gemma3n@gemma-3n-e2b                   Yes         No google/gemma3n@gemma-3n-e2b-it                Yes         No google/gemma3n@gemma-3n-e4b                   Yes         No google/gemma3n@gemma-3n-e4b-it                Yes         No google/[email protected]                  Yes         No google/[email protected]            Yes         No google/[email protected]                  Yes         No google/[email protected]            Yes         No google/gemma@gemma-2b                         Yes         No google/gemma@gemma-2b-gg-hf                   Yes         No google/gemma@gemma-2b-it                      Yes         No google/gemma@gemma-2b-it-gg-hf                Yes         No google/gemma@gemma-7b                         Yes         No google/gemma@gemma-7b-gg-hf                   Yes         No google/gemma@gemma-7b-it                      Yes         No google/gemma@gemma-7b-it-gg-hf                Yes         No 

    输出不包含任何经过调优的模型或 Hugging Face 模型。如需查看支持哪些 Hugging Face 模型,请添加 --can-deploy-hugging-face-models 标志。

  2. 如需查看模型的部署规范,请运行 gcloud ai model-garden models list-deployment-config 命令。您可以查看 Model Garden 支持特定模型使用的机器类型、加速器类型和容器映像 URI。

    gcloud ai model-garden models list-deployment-config \     --model=MODEL_ID 

    MODEL_ID 替换为通过上一个 list 命令得到的模型 ID,例如 google/gemma@gemma-2bstabilityai/stable-diffusion-xl-base-1.0

  3. 运行 gcloud ai model-garden models deploy 命令,将模型部署到端点。Model Garden 会为您的端点生成显示名称,并使用默认部署配置,除非您另行指定其他参数和值。

    如需异步运行命令,请添加 --asynchronous 标志。

    gcloud ai model-garden models deploy \     --model=MODEL_ID \     [--machine-type=MACHINE_TYPE] \     [--accelerator-type=ACCELERATOR_TYPE] \     [--endpoint-display-name=ENDPOINT_NAME] \     [--hugging-face-access-token=HF_ACCESS_TOKEN] \     [--reservation-affinity reservation-affinity-type=any-reservation] \     [--reservation-affinity reservation-affinity-type=specific-reservation, key="compute.googleapis.com/reservation-name", values=RESERVATION_RESOURCE_NAME] \     [--asynchronous] 

    替换以下占位符:

    • MODEL_ID:通过上一个 list 命令得到的模型 ID。对于 Hugging Face 模型,请使用 Hugging Face 模型网址格式,例如 stabilityai/stable-diffusion-xl-base-1.0
    • MACHINE_TYPE:定义要为模型部署的资源集,例如 g2-standard-4
    • ACCELERATOR_TYPE:指定要添加到部署中的加速器,以帮助在处理密集型工作负载(例如 NVIDIA_L4)时提高性能。
    • ENDPOINT_NAME:已部署的 Vertex AI 端点的名称。
    • HF_ACCESS_TOKEN:对于 Hugging Face 模型,如果模型有门控限制,请提供访问令牌
    • RESERVATION_RESOURCE_NAME:如需使用特定的 Compute Engine 预留,请指定预留的名称。如果您指定了特定预留,则无法指定 any-reservation

    输出包括 Model Garden 使用的部署配置、端点 ID 和部署操作 ID(您可以用其来检查部署状态)。

    Using the default deployment configuration:  Machine type: g2-standard-12  Accelerator type: NVIDIA_L4  Accelerator count: 1  The project has enough quota. The current usage of quota for accelerator type NVIDIA_L4 in region us-central1 is 0 out of 28.  Deploying the model to the endpoint. To check the deployment status, you can try one of the following methods: 1) Look for endpoint `ENDPOINT_DISPLAY_NAME` at the [Vertex AI] -> [Online prediction] tab in Cloud Console 2) Use `gcloud ai operations describe OPERATION_ID --region=LOCATION` to find the status of the deployment long-running operation 
  4. 如需查看有关部署的详细信息,请运行 gcloud ai endpoints list --list-model-garden-endpoints-only 命令:

    gcloud ai endpoints list --list-model-garden-endpoints-only \     --region=LOCATION_ID 

    LOCATION_ID 替换为您在其中部署了模型的区域。

    输出包含从 Model Garden 创建的所有端点,以及端点 ID、端点名称、端点是否与所部署模型相关联等信息。如需查找您的部署,请查找上一个命令返回的端点名称。

REST

列出所有可部署的模型,然后获取要部署的模型的 ID。然后,您可以使用默认配置和端点部署模型。或者,您也可以选择自定义部署,例如设置特定的机器类型或使用专用端点。

1. 列出可部署的模型

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的 Google Cloud 项目 ID。
  • QUERY_PARAMETERS:如需列出 Model Garden 模型,请添加以下查询参数 listAllVersions=True&filter=is_deployable(true)。如需列出 Hugging Face 模型,请将过滤条件设置为 alt=json&is_hf_wildcard(true)+AND+labels.VERIFIED_DEPLOYMENT_CONFIG%3DVERIFIED_DEPLOYMENT_SUCCEED&listAllVersions=True

HTTP 方法和网址:

GET https://us-central1-aiplatform.googleapis.com/v1/publishers/*/models?QUERY_PARAMETERS

如需发送请求,请选择以下方式之一:

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: PROJECT_ID" \
"https://us-central1-aiplatform.googleapis.com/v1/publishers/*/models?QUERY_PARAMETERS"

PowerShell

执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_ID" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://us-central1-aiplatform.googleapis.com/v1/publishers/*/models?QUERY_PARAMETERS" | Select-Object -Expand Content

您会收到类似以下内容的 JSON 响应。

 {   "publisherModels": [     {       "name": "publishers/google/models/gemma3",       "versionId": "gemma-3-1b-it",       "openSourceCategory": "GOOGLE_OWNED_OSS_WITH_GOOGLE_CHECKPOINT",       "supportedActions": {         "openNotebook": {           "references": {             "us-central1": {               "uri": "https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_gradio_streaming_chat_completions.ipynb"             }           },           "resourceTitle": "Notebook",           "resourceUseCase": "Chat Completion Playground",           "resourceDescription": "Chat with deployed Gemma 2 endpoints via Gradio UI."         },         "deploy": {           "modelDisplayName": "gemma-3-1b-it",           "containerSpec": {             "imageUri": "us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:20250312_0916_RC01",             "args": [               "python",               "-m",               "vllm.entrypoints.api_server",               "--host=0.0.0.0",               "--port=8080",               "--model=gs://vertex-model-garden-restricted-us/gemma3/gemma-3-1b-it",               "--tensor-parallel-size=1",               "--swap-space=16",               "--gpu-memory-utilization=0.95",               "--disable-log-stats"             ],             "env": [               {                 "name": "MODEL_ID",                 "value": "google/gemma-3-1b-it"               },               {                 "name": "DEPLOY_SOURCE",                 "value": "UI_NATIVE_MODEL"               }             ],             "ports": [               {                 "containerPort": 8080               }             ],             "predictRoute": "/generate",             "healthRoute": "/ping"           },           "dedicatedResources": {             "machineSpec": {               "machineType": "g2-standard-12",               "acceleratorType": "NVIDIA_L4",               "acceleratorCount": 1             }           },           "publicArtifactUri": "gs://vertex-model-garden-restricted-us/gemma3/gemma3.tar.gz",           "deployTaskName": "vLLM 128K context",           "deployMetadata": {             "sampleRequest": "{\n    \"instances\": [\n        {\n          \"@requestFormat\": \"chatCompletions\",\n          \"messages\": [\n              {\n                  \"role\": \"user\",\n                  \"content\": \"What is machine learning?\"\n              }\n          ],\n          \"max_tokens\": 100\n        }\n    ]\n}\n"           }         },         ... 

2. 部署模型

部署 Model Garden 中的模型或 Hugging Face 中的模型。您还可以通过指定其他 JSON 字段来自定义部署。

使用默认配置部署模型。

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION:将在其中部署模型的区域。
  • PROJECT_ID:您的 Google Cloud 项目 ID。
  • MODEL_ID:要部署的模型的 ID,您可以通过列出所有可部署的模型来获取此 ID。该 ID 采用以下格式:publishers/PUBLISHER_NAME/models/MODEL_NAME@MODEL_VERSION

HTTP 方法和网址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION:deploy

请求 JSON 正文:

 {   "publisher_model_name": "MODEL_ID",   "model_config": {     "accept_eula": "true"   } } 

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:

cat > request.json << 'EOF' {   "publisher_model_name": "MODEL_ID",   "model_config": {     "accept_eula": "true"   } } EOF

然后,执行以下命令以发送 REST 请求:

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:deploy"

PowerShell

将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:

@' {   "publisher_model_name": "MODEL_ID",   "model_config": {     "accept_eula": "true"   } } '@  | Out-File -FilePath request.json -Encoding utf8

然后,执行以下命令以发送 REST 请求:

$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:deploy" | Select-Object -Expand Content

您会收到类似以下内容的 JSON 响应。

 {   "name": "projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID",   "metadata": {     "@type": "type.googleapis.com/google.cloud.aiplatform.v1.DeployOperationMetadata",     "genericMetadata": {       "createTime": "2025-03-13T21:44:44.538780Z",       "updateTime": "2025-03-13T21:44:44.538780Z"     },     "publisherModel": "publishers/google/models/gemma3@gemma-3-1b-it",     "destination": "projects/PROJECT_ID/locations/LOCATION",     "projectNumber": "PROJECT_ID"   } } 

部署 Hugging Face 模型

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION:将在其中部署模型的区域。
  • PROJECT_ID:您的 Google Cloud 项目 ID。
  • MODEL_ID:要部署的 Hugging Face 模型的 ID,您可以通过列出所有可部署的模型来获取此 ID。该 ID 采用以下格式: PUBLISHER_NAME/MODEL_NAME
  • ACCESS_TOKEN:如果模型有门控限制,请提供访问令牌

HTTP 方法和网址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION:deploy

请求 JSON 正文:

 {   "hugging_face_model_id": "MODEL_ID",   "hugging_face_access_token": "ACCESS_TOKEN",   "model_config": {     "accept_eula": "true"   } } 

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:

cat > request.json << 'EOF' {   "hugging_face_model_id": "MODEL_ID",   "hugging_face_access_token": "ACCESS_TOKEN",   "model_config": {     "accept_eula": "true"   } } EOF

然后,执行以下命令以发送 REST 请求:

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:deploy"

PowerShell

将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:

@' {   "hugging_face_model_id": "MODEL_ID",   "hugging_face_access_token": "ACCESS_TOKEN",   "model_config": {     "accept_eula": "true"   } } '@  | Out-File -FilePath request.json -Encoding utf8

然后,执行以下命令以发送 REST 请求:

$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:deploy" | Select-Object -Expand Content

您会收到类似以下内容的 JSON 响应。

 {   "name": "projects/PROJECT_ID/locations/us-central1LOCATION/operations/OPERATION_ID",   "metadata": {     "@type": "type.googleapis.com/google.cloud.aiplatform.v1.DeployOperationMetadata",     "genericMetadata": {       "createTime": "2025-03-13T21:44:44.538780Z",       "updateTime": "2025-03-13T21:44:44.538780Z"     },     "publisherModel": "publishers/PUBLISHER_NAME/model/MODEL_NAME",     "destination": "projects/PROJECT_ID/locations/LOCATION",     "projectNumber": "PROJECT_ID"   } } 

使用自定义设置部署模型

在使用任何请求数据之前,请先进行以下替换:

  • LOCATION:将在其中部署模型的区域。
  • PROJECT_ID:您的 Google Cloud 项目 ID。
  • MODEL_ID:要部署的模型的 ID,您可以通过列出所有可部署的模型来获取此 ID。该 ID 采用以下格式:publishers/PUBLISHER_NAME/models/MODEL_NAME@MODEL_VERSION,例如 google/gemma@gemma-2bstabilityai/stable-diffusion-xl-base-1.0
  • MACHINE_TYPE:定义要为模型部署的资源集,例如 g2-standard-4
  • ACCELERATOR_TYPE: 指定要添加到部署中的加速器,以帮助在处理密集型工作负载(例如 NVIDIA_L4)时提高性能
  • ACCELERATOR_COUNT:部署中要使用的加速器数量。
  • reservation_affinity_type:如需为部署使用现有的 Compute Engine 预留,请指定任意预留或特定预留。如果您指定了此值,则请勿指定 spot
  • spot:指示是否为部署使用 Spot 虚拟机。
  • IMAGE_URI:要使用的容器映像的位置,例如 us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:20241016_0916_RC00_maas
  • CONTAINER_ARGS:在部署期间传递给容器的参数。
  • CONTAINER_PORT:容器的端口号。
  • fast_tryout_enabled:在测试模型时,您可以选择使用更快的部署。此选项仅适用于部分机器类型上的常用模型。如果启用该选项,您将无法指定模型或部署配置。

HTTP 方法和网址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION:deploy

请求 JSON 正文:

 {   "publisher_model_name": "MODEL_ID",   "deploy_config": {     "dedicated_resources": {       "machine_spec": {         "machine_type": "MACHINE_TYPE",         "accelerator_type": "ACCELERATOR_TYPE",         "accelerator_count": ACCELERATOR_COUNT,         "reservation_affinity": {           "reservation_affinity_type": "ANY_RESERVATION"         }       },       "spot": "false"     }   },   "model_config": {     "accept_eula": "true",     "container_spec": {       "image_uri": "IMAGE_URI",       "args": [CONTAINER_ARGS ],       "ports": [         {           "container_port": CONTAINER_PORT         }       ]     }   },   "deploy_config": {     "fast_tryout_enabled": false   }, } 

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:

cat > request.json << 'EOF' {   "publisher_model_name": "MODEL_ID",   "deploy_config": {     "dedicated_resources": {       "machine_spec": {         "machine_type": "MACHINE_TYPE",         "accelerator_type": "ACCELERATOR_TYPE",         "accelerator_count": ACCELERATOR_COUNT,         "reservation_affinity": {           "reservation_affinity_type": "ANY_RESERVATION"         }       },       "spot": "false"     }   },   "model_config": {     "accept_eula": "true",     "container_spec": {       "image_uri": "IMAGE_URI",       "args": [CONTAINER_ARGS ],       "ports": [         {           "container_port": CONTAINER_PORT         }       ]     }   },   "deploy_config": {     "fast_tryout_enabled": false   }, } EOF

然后,执行以下命令以发送 REST 请求:

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:deploy"

PowerShell

将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:

@' {   "publisher_model_name": "MODEL_ID",   "deploy_config": {     "dedicated_resources": {       "machine_spec": {         "machine_type": "MACHINE_TYPE",         "accelerator_type": "ACCELERATOR_TYPE",         "accelerator_count": ACCELERATOR_COUNT,         "reservation_affinity": {           "reservation_affinity_type": "ANY_RESERVATION"         }       },       "spot": "false"     }   },   "model_config": {     "accept_eula": "true",     "container_spec": {       "image_uri": "IMAGE_URI",       "args": [CONTAINER_ARGS ],       "ports": [         {           "container_port": CONTAINER_PORT         }       ]     }   },   "deploy_config": {     "fast_tryout_enabled": false   }, } '@  | Out-File -FilePath request.json -Encoding utf8

然后,执行以下命令以发送 REST 请求:

$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:deploy" | Select-Object -Expand Content

您会收到类似以下内容的 JSON 响应。

 {   "name": "projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID",   "metadata": {     "@type": "type.googleapis.com/google.cloud.aiplatform.v1.DeployOperationMetadata",     "genericMetadata": {       "createTime": "2025-03-13T21:44:44.538780Z",       "updateTime": "2025-03-13T21:44:44.538780Z"     },     "publisherModel": "publishers/google/models/gemma3@gemma-3-1b-it",     "destination": "projects/PROJECT_ID/locations/LOCATION",     "projectNumber": "PROJECT_ID"   } } 

控制台

  1. 在 Google Cloud 控制台中,前往 Model Garden 页面。

    转到 Model Garden

  2. 找到要部署的受支持模型,然后点击其模型卡片。

  3. 点击部署以打开部署模型窗格。

  4. 部署模型窗格中,指定部署的详细信息。

    1. 使用或修改生成的模型和端点名称。
    2. 选择要在其中创建模型端点的位置。
    3. 为部署的每个节点选择要使用的机器类型。
    4. 如需使用 Compute Engine 预留,请在部署设置部分下选择高级

    预留类型字段中,选择预留类型。预留必须符合您指定的机器规格。

    • 自动使用已创建的预留:Vertex AI 会自动选择具有匹配属性的允许预留。如果自动选择的预留中没有容量,Vertex AI 会使用常规 Google Cloud 资源池。
    • 选择特定预留:Vertex AI 会使用特定预留。如果所选预留没有空位,系统会抛出错误。
    • 不使用(默认):Vertex AI 会使用常规Google Cloud 资源池。此值的效果与不指定预留相同。
  5. 点击部署

Terraform

如需了解如何应用或移除 Terraform 配置,请参阅基本 Terraform 命令。 如需了解详情,请参阅 Terraform 提供程序参考文档

部署模型

以下示例使用默认配置将 gemma-3-1b-it 模型部署到 us-central1 中的新 Vertex AI 端点。

terraform {   required_providers {     google = {       source = "hashicorp/google"       version = "6.45.0"     }   } }  provider "google" {   region  = "us-central1" }  resource "google_vertex_ai_endpoint_with_model_garden_deployment" "gemma_deployment" {   publisher_model_name = "publishers/google/models/gemma3@gemma-3-1b-it"   location = "us-central1"   model_config {     accept_eula = True   } } 

如需部署具有自定义功能的模型,请参阅 Vertex AI 端点与 Model Garden 部署了解详情。

应用配置

terraform init terraform plan terraform apply 

应用配置后,Terraform 会预配新的 Vertex AI 端点并部署指定的开放模型。

清理

如需删除端点和模型部署,请运行以下命令:

terraform destroy 

部署合作伙伴模型并发出预测请求

在 Google Cloud 控制台中,前往 Model Garden 页面,然后使用模型集合过滤条件查看自行部署合作伙伴模型。 从可自行部署的合作伙伴模型列表中进行选择,然后点击启用购买相应模型。

您必须按照其 Model Garden 模型卡片上的“推荐的硬件配置”部分所述,在合作伙伴要求的机器类型上进行部署。部署后,模型部署资源会位于由 Google 管理的安全项目中。

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档

替换代码中的以下占位符:

  • LOCATION:您计划在其中部署模型和端点的区域。
  • PROJECT_ID:您的项目 ID。
  • DISPLAY_NAME:关联资源的描述性名称。
  • PUBLISHER_NAME:提供要上传或部署的模型的合作伙伴的名称。
  • PUBLISHER_MODEL_NAME:要上传的模型的名称。
  • MACHINE_TYPE:定义要为模型部署的资源集,例如 g2-standard-4。必须与合作伙伴提供的某种配置一致。
  • ACCELERATOR_TYPE:指定要添加到部署中的加速器,以帮助在处理密集型工作负载(例如 NVIDIA_L4)时提高性能。必须与合作伙伴提供的某种配置一致。
  • ACCELERATOR_COUNT:要使用的加速器数量。必须与合作伙伴提供的某种配置一致。
  • REQUEST_PAYLOAD:要包含在预测请求中的字段和值。查看合作伙伴的 Model Garden 模型卡片,了解有哪些可用字段。
from google.cloud import aiplatform  aiplatform.init(project=PROJECT_ID, location=LOCATION)  # Upload a model model = aiplatform.Model.upload(     display_name="DISPLAY_NAME_MODEL",     model_garden_source_model_name = f"publishers/PUBLISHER_NAME/models/PUBLISHER_MODEL_NAME", )  # Create endpoint my_endpoint = aiplatform.Endpoint.create(display_name="DISPLAY_NAME_ENDPOINT")  # Deploy model MACHINE_TYPE = "MACHINE_TYPE"  # @param {type: "string"} ACCELERATOR_TYPE = "ACCELERATOR_TYPE" # @param {type: "string"} ACCELERATOR_COUNT = ACCELERATOR_COUNT # @param {type: "number"}  model.deploy(     endpoint=my_endpoint,     deployed_model_display_name="DISPLAY_NAME_DEPLOYED_MODEL",     traffic_split={"0": 100},     machine_type=MACHINE_TYPE,     accelerator_type=ACCELERATOR_TYPE,     accelerator_count=ACCELERATOR_COUNT,     min_replica_count=1,     max_replica_count=1, )  # Unary call for predictions PAYLOAD = {     REQUEST_PAYLOAD }  request = json.dumps(PAYLOAD)  response = my_endpoint.raw_predict(     body = request,     headers = {'Content-Type':'application/json'} )  print(response)  # Streaming call for predictions PAYLOAD = {     REQUEST_PAYLOAD }  request = json.dumps(PAYLOAD)  for stream_response in my_endpoint.stream_raw_predict(     body = request,     headers = {'Content-Type':'application/json'} ):     print(stream_response) 

REST

列出所有可部署的模型,然后获取要部署的模型的 ID。然后,您可以使用默认配置和端点部署模型。或者,您也可以选择自定义部署,例如设置特定的机器类型或使用专用端点。

在示例 curl 命令中,替换以下占位符:

  • LOCATION:您计划在其中部署模型和端点的区域。
  • PROJECT_ID:您的项目 ID。
  • DISPLAY_NAME:关联资源的描述性名称。
  • PUBLISHER_NAME:提供要上传或部署的模型的合作伙伴的名称。
  • PUBLISHER_MODEL_NAME:要上传的模型的名称。
  • ENDPOINT_ID:端点的 ID。
  • MACHINE_TYPE:定义要为模型部署的资源集,例如 g2-standard-4。必须与合作伙伴提供的某种配置一致。
  • ACCELERATOR_TYPE:指定要添加到部署中的加速器,以帮助在处理密集型工作负载(例如 NVIDIA_L4)时提高性能。必须与合作伙伴提供的某种配置一致。
  • ACCELERATOR_COUNT:要使用的加速器数量。必须与合作伙伴提供的某种配置一致。
  • REQUEST_PAYLOAD:要包含在预测请求中的字段和值。查看合作伙伴的 Model Garden 模型卡片,了解有哪些可用字段。
  1. 上传模型以将其添加到 Model Registry。

    curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://LOCATION-aiplatform.googleapi.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/models:upload \ -d '{ "model": {   "displayName": "DISPLAY_NAME_MODEL",   "baseModelSource": {     "modelGardenSource": {       "publicModelName": f"publishers/PUBLISHER_NAME/models/PUBLISHER_MODEL_NAME",     }   } } }' 
  2. 创建端点。

    curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://LOCATION-aiplatform.googleapi.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints \ -d '{ "displayName": "DISPLAY_NAME_ENDPOINT" }' 
  3. 将上传的模型部署到端点。

    curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://LOCATION-aiplatform.googleapi.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints/ENDPOINT_ID:deployModel \ -d '{ "deployedModel": {   "model": f"projects/PROJECT_ID/locations/LOCATION/models/MODEL_ID",   "displayName": "DISPLAY_NAME_DEPLOYED_MODEL",   "dedicatedResources": {    "machineSpec": {       "machineType": "MACHINE_TYPE",       "acceleratorType": "ACCELERATOR_TYPE",       "acceleratorCount":"ACCELERATOR_COUNT",    },    "minReplicaCount": 1,    "maxReplicaCount": 1   }, }, "trafficSplit": {   "0": 100 } }' 
  4. 部署模型后,您可以进行一元或流式预测调用。查看合作伙伴的 Model Garden 模型卡片,了解支持哪些 API 方法。

    • 一元调用示例:
    curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://LOCATION-aiplatform.googleapi.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints/ENDPOINT_ID:rawPredict \ -d 'REQUEST_PAYLOAD' 
    • 流式调用示例:
    curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://LOCATION-aiplatform.googleapi.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints/ENDPOINT_ID:streamRawPredict \ -d 'REQUEST_PAYLOAD' 

控制台

  1. 在 Google Cloud 控制台中,前往 Model Garden 页面。

    转到 Model Garden

  2. 如需查找特定模型,请在 Model Garden 搜索框中输入其名称。

  3. 如需查看您可以自行部署的所有模型,请在过滤条件窗格的模型集合部分中选择自行部署合作伙伴模型。生成的列表包含所有可自行部署的合作伙伴模型。

  4. 点击要部署的模型的名称,系统随即会打开其模型卡片。

  5. 点击部署选项

  6. 在 Vertex AI 上部署窗格中,配置部署,例如位置和机器类型。

  7. 点击部署

部署完成后,您可以使用 SDK 或 API 请求预测。如需查看更多相关说明,请参阅模型卡片上的“文档”部分。

查看或管理端点

如需查看和管理端点,请前往 Vertex AI 在线预测页面。

转到“在线预测”

Vertex AI 会列出您项目中特定区域的所有端点。点击某个端点可查看其详细信息,例如有哪些模型部署到该端点。

取消部署模型并删除资源

如要让所部署的模型不再使用项目中的资源,请从其端点取消部署该模型。您必须先取消部署模型,然后才能删除端点和模型。

取消部署模型

从端点取消部署模型。

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档

在您的代码中,替换以下内容:

  • PROJECT_ID 替换为您的项目 ID。
  • LOCATION 替换为您的区域,例如“us-central1”
  • ENDPOINT_ID 替换为您的端点 ID
from google.cloud import aiplatform  aiplatform.init(project=PROJECT_ID, location=LOCATION)  # To find out which endpoints are available, un-comment the line below: # endpoints = aiplatform.Endpoint.list()  endpoint = aiplatform.Endpoint(ENDPOINT_ID) endpoint.undeploy_all() 

gcloud

在这些命令中,替换以下内容:

  • PROJECT_ID 替换为您的项目名称
  • LOCATION_ID 替换为您在其中部署了模型和端点的区域
  • ENDPOINT_ID 替换为端点 ID
  • MODEL_ID 替换为通过 list model 命令得到的模型 ID
  • DEPLOYED_MODEL_ID 替换为所部署模型的 ID
  1. 运行 gcloud ai endpoints list 命令,找到与您的部署关联的端点 ID。

    gcloud ai endpoints list \     --project=PROJECT_ID \     --region=LOCATION_ID 
  2. 运行 gcloud ai models list 命令,找到模型 ID。

    gcloud ai models list \     --project=PROJECT_ID \     --region=LOCATION_ID 
  3. 使用通过上一个命令得到的模型 ID,运行 gcloud ai models describe 命令来获取所部署模型的 ID。

    gcloud ai models describe MODEL_ID \     --project=PROJECT_ID \     --region=LOCATION_ID 

    简略版输出类似于以下示例。在输出中,该 ID 称为 deployedModelId

    Using endpoint [https://us-central1-aiplatform.googleapis.com/] artifactUri: [URI removed] baseModelSource:   modelGardenSource:     publicModelName: publishers/google/models/gemma2 ... deployedModels: -   deployedModelId: '1234567891234567891'   endpoint: projects/12345678912/locations/us-central1/endpoints/12345678912345 displayName: gemma2-2b-it-12345678912345 etag: [ETag removed] modelSourceInfo:   sourceType: MODEL_GARDEN name: projects/123456789123/locations/us-central1/models/gemma2-2b-it-12345678912345 ... 
  4. 运行 gcloud ai endpoints undeploy-model 命令,使用通过之前的命令得到的端点 ID 和所部署模型的 ID 从端点取消部署模型。

    gcloud ai endpoints undeploy-model ENDPOINT_ID \     --project=PROJECT_ID \     --region=LOCATION_ID \     --deployed-model-id=DEPLOYED_MODEL_ID 

    此命令没有任何输出。

控制台

  1. 在 Google Cloud 控制台中,前往在线预测页面上的端点标签页。

    转至 Endpoints

  2. 区域下拉列表中,选择端点所在的区域。

  3. 点击端点名称以打开详情页面。

  4. 在模型对应的行中,点击 操作,然后选择从端点取消部署模型

  5. 从端点取消部署模型对话框中,点击取消部署

删除端点

删除与模型部署相关联的 Vertex AI 端点。

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档

在您的代码中,替换以下内容:

  • PROJECT_ID 替换为您的项目 ID。
  • LOCATION 替换为您的区域,例如“us-central1”
  • ENDPOINT_ID 替换为您的端点 ID
from google.cloud import aiplatform  aiplatform.init(project=PROJECT_ID, location=LOCATION)  # To find out which endpoints are available, un-comment the line below: # endpoints = aiplatform.Endpoint.list()  endpoint = aiplatform.Endpoint(ENDPOINT_ID) endpoint.delete() 

gcloud

在这些命令中,替换以下内容:

  • PROJECT_ID 替换为您的项目名称
  • LOCATION_ID 替换为您在其中部署了模型和端点的区域
  • ENDPOINT_ID 替换为端点 ID
  1. 运行 gcloud ai endpoints list 命令,获取要删除的端点的 ID。此命令会列出项目中所有端点的 ID。

    gcloud ai endpoints list \     --project=PROJECT_ID \     --region=LOCATION_ID 
  2. 运行 gcloud ai endpoints delete 命令以删除端点。

    gcloud ai endpoints delete ENDPOINT_ID \     --project=PROJECT_ID \     --region=LOCATION_ID 

    出现提示时,输入 y 进行确认。此命令没有任何输出。

控制台

  1. 在 Google Cloud 控制台中,前往在线预测页面上的端点标签页。

    转至 Endpoints

  2. 区域下拉列表中,选择端点所在的区域。

  3. 在端点对应行末尾,点击 操作,然后选择删除端点

  4. 在确认提示中,点击确认

删除模型

删除与模型部署相关联的模型资源。

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档

在您的代码中,替换以下内容:

  • PROJECT_ID 替换为您的项目 ID。
  • LOCATION 替换为您的区域,例如“us-central1”
  • MODEL_ID 替换为您的模型 ID
from google.cloud import aiplatform  aiplatform.init(project=PROJECT_ID, location=LOCATION)  # To find out which models are available in Model Registry, un-comment the line below: # models = aiplatform.Model.list()  model = aiplatform.Model(MODEL_ID) model.delete() 

gcloud

在这些命令中,替换以下内容:

  • PROJECT_ID 替换为您的项目名称
  • LOCATION_ID 替换为您在其中部署了模型和端点的区域
  • MODEL_ID 替换为通过 list model 命令得到的模型 ID
  1. 运行 gcloud ai models list 命令,找到要删除的模型的 ID。

    gcloud ai models list \     --project=PROJECT_ID \     --region=LOCATION_ID 
  2. 运行 gcloud ai models delete 命令,通过提供模型 ID 和模型位置来删除模型。

    gcloud ai models delete MODEL_ID \     --project=PROJECT_ID \     --region=LOCATION_ID 

控制台

  1. 在 Google Cloud 控制台中,从 Vertex AI 部分进入 Model Registry 页面。

    进入 Model Registry 页面

  2. 区域下拉列表中,选择您在其中部署了模型的区域。

  3. 在模型对应的行中,点击 操作,然后选择删除模型

    删除模型时,所有关联的模型版本和评估都会从 Google Cloud 项目中一并删除。

  4. 在确认提示中,点击删除

查看代码示例

特定于任务的解决方案模型的大多数模型卡都包含您可以复制和测试的代码示例。

  1. 在 Google Cloud 控制台中,前往 Model Garden 页面。

    转到 Model Garden

  2. 找到要查看其代码示例的支持模型,然后点击文档标签页。

  3. 页面会滚动到文档部分,其中嵌入了示例代码。

创建视觉应用

适用计算机视觉模型的模型卡支持创建视觉应用。

  1. 在 Google Cloud 控制台中,前往 Model Garden 页面。

    转到 Model Garden

  2. 在“针对特定任务的解决方案”部分中找到要用于创建视觉应用的视觉模型,然后点击查看详细信息

  3. 点击构建应用

    您将进入 Vertex AI Vision。

  4. 应用名称中,输入应用的名称,然后点击继续

  5. 选择结算方案,然后点击创建

    您将进入 Vertex AI Vision Studio,可以继续创建计算机视觉应用。