部署具有自定义权重的模型

部署具有自定义权重的模型是一项预览版服务。您可以基于一组预定义的基础模型对模型进行微调,并在 Vertex AI Model Garden 上部署您的自定义模型。您可以将模型制品上传到项目中的 Cloud Storage 存储桶,从而部署使用自定义权重导入的自定义模型,这在 Vertex AI 中只需点击一下即可完成。

VPC Service Controls 支持自定义权重。

支持的模型

以下基础模型支持部署具有自定义权重的模型的公开预览版:

模型名称 版本
Llama
  • Llama-2:7B、13B
  • Llama-3.1:8B、70B
  • Llama-3.2:1B、3B
  • Llama-4:Scout-17B、Maverick-17B
  • CodeLlama-13B
Gemma
  • Gemma-2:9B、27B
  • Gemma-3:1B、4B、3-12B、27B
  • Medgemma:4B、27B-text
Qwen
  • Qwen2:1.5B
  • Qwen2.5:0.5B、1.5B、7B、32B
  • Qwen3:0.6B、1.7B、8B、32B、Qwen3-Coder-480B-A35B-Instruct、Qwen3-Next-80B-A3B-Instruct、Qwen3-Next-80B-A3B-Thinking
Deepseek
  • Deepseek-R1
  • Deepseek-V3
  • DeepSeek-V3.1
Mistral 和 Mixtral
  • Mistral-7B-v0.1
  • Mixtral-8x7B-v0.1
  • Mistral-Nemo-Base-2407
Phi-4
  • Phi-4-reasoning
OpenAI OSS
  • gpt-oss:20B、120B

限制

自定义权重不支持导入量化模型

模型文件

您必须以 Hugging Face 权重格式提供模型文件。如需详细了解 Hugging Face 权重格式,请参阅使用 Hugging Face 模型

如果未提供所需文件,模型部署可能会失败。

下表列出了模型文件类型,这些类型取决于模型的架构:

模型文件内容 文件类型
模型配置
  • config.json
模型权重
  • *.safetensors
  • *.bin
权重指数
  • *.index.json
词元化器文件
  • tokenizer.model
  • tokenizer.json
  • tokenizer_config.json

位置

您可以通过 Model Garden 服务在所有区域中部署自定义模型。

前提条件

本部分演示了如何部署自定义模型。

准备工作

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  9. 本教程假定您使用 Cloud Shell 与 Google Cloud进行互动。如果您想使用其他 shell 取代 Cloud Shell,请执行以下额外的配置:

    1. Install the Google Cloud CLI.

    2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    3. To initialize the gcloud CLI, run the following command:

      gcloud init

    部署自定义模型

    本部分演示了如何部署自定义模型。

    如果您使用的是命令行界面 (CLI)、Python 或 JavaScript,请将以下变量替换为相应的值,以便代码示例正常运行:

    • REGION:您的区域。例如 uscentral1
    • MODEL_GCS:您的 Google Cloud 模型。例如 gs://custom-weights-fishfooding/meta-llama/Llama-3.2-1B-Instruct
    • PROJECT_ID:您的项目 ID。
    • MODEL_ID:您的模型 ID。
    • MACHINE_TYPE:您的机器类型。例如 g2-standard-12
    • ACCELERATOR_TYPE:您的加速器类型。例如 NVIDIA_L4
    • ACCELERATOR_COUNT:您的加速器数量。
    • PROMPT:您的文本提示。

    控制台

    以下步骤介绍了如何使用 Google Cloud 控制台部署具有自定义权重的模型。

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

      前往 Model Garden

    2. 点击部署具有自定义权重的模型。系统随即会显示部署具有自定义权重的模型窗格。

    3. 模型来源部分中,执行以下操作:

      1. 点击浏览,然后选择存储模型的存储桶,再点击选择

      2. 可选:在模型名称字段中输入模型名称。

    4. 部署设置部分,执行以下操作:

      1. 区域字段中,选择您的区域,然后点击确定

      2. 机器规格字段中,选择用于部署模型的机器规格。

      3. 可选:在端点名称字段中,默认会显示模型的端点。但是,您可以在相应字段中输入其他端点名称。

      4. 如果您的项目强制执行 VPC-SC,或者您希望使用私密访问,请从端点访问字段中选择私密 (Private Service Connect)。否则,请选择公开

      5. 如果您使用 Private Service Connect,请在项目 ID 字段中输入运行查询客户端的项目 ID,或点击选择项目 ID 以显示包含项目 ID 的对话框。

        如果您点击选择项目 ID,请执行以下操作:

        1. 找到包含尝试访问模型的代码的项目。
        2. 点击项目的复选框。
        3. 点击选择
    5. 点击部署

    gcloud CLI

    此命令演示了如何将模型部署到特定区域。

    gcloud ai model-garden models deploy --model=${MODEL_GCS} --region ${REGION}
    

    此命令演示了如何将模型部署到特定区域,并指定其机器类型、加速器类型和加速器数量。如果您想选择特定的机器配置,则必须设置所有这三个字段。

    gcloud ai model-garden models deploy --model=${MODEL_GCS} --machine-type=${MACHINE_TYE} --accelerator-type=${ACCELERATOR_TYPE} --accelerator-count=${ACCELERATOR_COUNT} --region ${REGION}
    

    Python

    import vertexai
    from google.cloud import aiplatform
    from vertexai.preview import model_garden
    
    vertexai.init(project=${PROJECT_ID}, location=${REGION})
    custom_model = model_garden.CustomModel(
      gcs_uri=GCS_URI,
    )
    endpoint = custom_model.deploy(
      machine_type="${MACHINE_TYPE}",
      accelerator_type="${ACCELERATOR_TYPE}",
      accelerator_count="${ACCELERATOR_COUNT}",
      model_display_name="custom-model",
      endpoint_display_name="custom-model-endpoint")
    
    endpoint.predict(instances=[{"prompt": "${PROMPT}"}], use_dedicated_endpoint=True)
    

    或者,您也可以不向 custom_model.deploy() 方法传递参数。

    import vertexai
    from google.cloud import aiplatform
    from vertexai.preview import model_garden
    
    vertexai.init(project=${PROJECT_ID}, location=${REGION})
    custom_model = model_garden.CustomModel(
      gcs_uri=GCS_URI,
    )
    endpoint = custom_model.deploy()
    
    endpoint.predict(instances=[{"prompt": "${PROMPT}"}], use_dedicated_endpoint=True)
    

    curl

    
    curl -X POST \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://${REGION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${REGION}:deploy" \
      -d '{
        "custom_model": {
        "gcs_uri": "'"${MODEL_GCS}"'"
      },
      "destination": "projects/'"${PROJECT_ID}"'/locations/'"${REGION}"'",
      "model_config": {
         "model_user_id": "'"${MODEL_ID}"'",
      },
    }'
    

    或者,您也可以使用 API 显式设置机器类型。

    
    curl -X POST \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://${REGION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${REGION}:deploy" \
      -d '{
        "custom_model": {
        "gcs_uri": "'"${MODEL_GCS}"'"
      },
      "destination": "projects/'"${PROJECT_ID}"'/locations/'"${REGION}"'",
      "model_config": {
         "model_user_id": "'"${MODEL_ID}"'",
      },
      "deploy_config": {
        "dedicated_resources": {
          "machine_spec": {
            "machine_type": "'"${MACHINE_TYPE}"'",
            "accelerator_type": "'"${ACCELERATOR_TYPE}"'",
            "accelerator_count": '"${ACCELERATOR_COUNT}"'
          },
          "min_replica_count": 1
        }
      }
    }'
    

    使用 API 进行部署

    VPC Service Controls 仅适用于专用私有端点。因此,您必须设置以下代码示例中的 private_service_connect_config,该示例演示了如何使用 API 进行部署:

    curl

      curl -X POST \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json" \
        "https://us-central1-aiplatform.googleapis.com/v1beta1/projects/YOUR_PROJECT/locations/us-central1:deploy" \
        -d '{
          "custom_model": {
            "model_id": "test-mg-deploy-092301",
            "gcs_uri": "gs://YOUR_GCS_BUCKET"
          },
          "destination": "projects/YOUR_PROJECT/locations/us-central1",
          "endpoint_config": {
            "endpoint_display_name": "psc-ep1",
            "private_service_connect_config": {
              "enablePrivateServiceConnect": true,
              "projectAllowlist": ["YOUR_PROJECT"]
            }
          },
          "deploy_config": {
            "dedicated_resources": {
              "machine_spec": {
                "machine_type": "g2-standard-24",
                "accelerator_type": "NVIDIA_L4",
                "accelerator_count": 2
              },
              "min_replica_count": 1,
              "max_replica_count": 1
            }
          }
        }'
    

    使用 Google Cloud 控制台获取端点 ID 和模型 ID

    部署完成后,请按以下步骤操作:

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

      前往 Model Garden

    2. 点击查看我的端点和模型

    3. 我的端点表格中,查看您刚刚从名称列部署的端点。系统会显示详细信息页面。

    4. 点击已部署的模型表格中的模型。

    5. 选择版本详细信息页面。模型 ID 会显示在表格的环境变量行中。

    设置 Private Service Connect

    您正在添加新端点以访问 Google API。此端点可在您选择的 VPC 网络的所有区域中使用。此外,还请考虑以下事项:

    • 对于使用混合连接来连接到端点 VPC 网络的网络,其中的客户端可以访问该端点。如需了解详情,请参阅通过端点访问Google API
    • 对等互连的 VPC 网络中的客户端无法访问端点。

    列出端点以获取服务连接

    此代码示例演示了如何列出端点以获取服务连接。

    curl

    $ curl \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://us-central1-aiplatform.googleapis.com/v1beta1/projects/YOUR_PROJECT/locations/us-central1/endpoints/YOUR_ENDPOINT_ID"
    

    这是列表端点响应。

      {
      "name": "projects/440968033208/locations/us-central1/endpoints/mg-endpoint-2c6ae2be-1491-43fe-b179-cb5a63e2c955",
      "displayName": "psc-ep1",
      "deployedModels": [
        {
          "id": "4026753529031950336",
          "model": "projects/440968033208/locations/us-central1/models/mg-custom-1758645924",
          "displayName": "null-null-null-1758645933",
          "createTime": "2025-09-23T16:45:45.169195Z",
          "dedicatedResources": {
            "machineSpec": {
              "machineType": "g2-standard-24",
              "acceleratorType": "NVIDIA_L4",
              "acceleratorCount": 2
            },
            "minReplicaCount": 1,
            "maxReplicaCount": 1
          },
          "enableContainerLogging": true,
          "privateEndpoints": {
            "serviceAttachment": "projects/qdb392d34e2a11149p-tp/regions/us-central1/serviceAttachments/gkedpm-fbbc4061323c91c14ab4d961a2f8b0"
          },
          "modelVersionId": "1",
          "status": {
            "lastUpdateTime": "2025-09-23T17:26:10.031652Z",
            "availableReplicaCount": 1
          }
        }
      ],
      "trafficSplit": {
        "4026753529031950336": 100
      },
      "etag": "AMEw9yPIWQYdbpHu6g6Mhpu1_10J062_oR9Jw9txrp8dFFbel7odLgSK8CGIogAUkR_r",
      "createTime": "2025-09-23T16:45:45.169195Z",
      "updateTime": "2025-09-23T17:13:36.320873Z",
      "privateServiceConnectConfig": {
        "enablePrivateServiceConnect": true,
        "projectAllowlist": [
          "ucaip-vpc-s-1605069239-dut-24"
        ]
      }
    }
    

    创建 Private Service Connect

    如需创建 Private Service Connect (PSC),请按以下步骤操作:

    1. 在 Google Cloud 控制台中,前往 Private Service Connect 页面。系统会显示已连接的端点页面。

      前往 Private Service Connect

    2. 点击 + 连接端点。系统会显示连接端点页面。

    3. 目标字段中选择一个选项。您可以选择 Google API(可访问大多数 Google API 和服务)或已发布服务(可访问已发布服务)。

    4. 目标详情部分中,从范围列表中选择一个值,然后从软件包类型列表中选择一个值。

    5. 端点详情部分中,执行以下操作:

      1. 端点名称字段中输入名称。
      2. 网络列表中选择一个值。选择位于您项目中的 VPC 网络。如果您必须在服务项目中创建一个在宿主项目内使用共享 VPC 网络的 PSC 端点,请使用 Google Cloud CLI 或发送 API 请求。
      3. IP 地址列表中选择一个值。
    6. 展开服务目录部分。

    7. 区域列表中选择一个区域。

    8. 命名空间列表中选择一个命名空间。

    9. 点击添加端点端点表格会更新,并添加一行来显示新端点。

    进行查询

    本部分介绍了如何创建公共端点和专用端点。

    向公共端点发出查询

    模型部署后,自定义权重支持公共专用端点。您可以使用 API 或 SDK 发送查询。

    在发送查询之前,您必须获取端点网址、端点 ID 和模型 ID,这些信息可在 Google Cloud 控制台中找到。

    请按照以下步骤获取相关信息:

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

      Model Garden

    2. 点击查看我的端点和模型

    3. 区域列表中选择您的区域。

    4. 如需获取端点 ID 和端点网址,请在我的端点部分中点击您的端点。

      您的端点 ID 会显示在端点 ID 字段中。

      您的公共端点网址会显示在专用端点字段中。

    5. 如需获取模型 ID,请在已部署的模型部分中找到您的模型,然后按照以下步骤操作:

      1. 点击模型字段中已部署模型的名称。
      2. 点击版本详细信息。您的模型 ID 会显示在模型 ID 字段中。

    获取端点和已部署模型信息后,请参阅以下代码示例,了解如何发送推理请求,或参阅向专用公共端点发送在线推理请求

    API

    以下代码示例演示了根据您的应用场景使用该 API 的不同方式。

    对话补全(一元)

    此示例请求会向模型发送完整的对话消息,并在生成整个回答后以单个块的形式获取回答。这类似于发送短信并获得一条完整的回复。

      curl -X POST \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json" \
        "https://${ENDPOINT_URL}/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT_ID}/chat/completions" \
        -d '{
        "model": "'"${MODEL_ID}"'",
        "temperature": 0,
        "top_p": 1,
        "max_tokens": 154,
        "ignore_eos": true,
        "messages": [
          {
            "role": "user",
            "content": "How to tell the time by looking at the sky?"
          }
        ]
      }'
    

    对话补全(流式传输)

    此请求是一元对话补全请求的流式传输版本。通过向请求添加 "stream": true,模型会在生成回答的过程中分段发送回答。这对于在聊天应用中创建实时打字机效果非常有用。

      curl -X POST \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json" \  "https://${ENDPOINT_URL}/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT_ID}/chat/completions" \
        -d '{
        "model": "'"${MODEL_ID}"'",
        "stream": true,
        "temperature": 0,
        "top_p": 1,
        "max_tokens": 154,
        "ignore_eos": true,
        "messages": [
          {
            "role": "user",
            "content": "How to tell the time by looking at the sky?"
          }
        ]
      }'
    

    预测

    此请求会直接发送提示,以从模型获取推理。这通常用于不一定需要对话的任务,例如文本摘要或分类。

      curl -X POST \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json" \
      "https://${ENDPOINT_URL}/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT_ID}:predict" \
        -d '{
        "instances": [
          {
            "prompt": "How to tell the time by looking at the sky?",
            "temperature": 0,
            "top_p": 1,
            "max_tokens": 154,
            "ignore_eos": true
          }
        ]
      }'
    

    原始预测

    此请求是预测请求的流式传输版本。通过使用 :streamRawPredict 端点并包含 "stream": true,此请求会发送直接提示,并接收模型在生成时作为连续数据流的输出,这与流式传输聊天完成请求类似。

      curl -X POST \
        -N \
        --output - \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json" \
        "https://${ENDPOINT_URL}/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT_ID}:streamRawPredict" \
        -d '{
        "instances": [
          {
            "prompt": "How to tell the time by looking at the sky?",
            "temperature": 0,
            "top_p": 1,
            "max_tokens": 154,
            "ignore_eos": true,
            "stream": true
          }
        ]
      }'
    

    SDK

    此代码示例使用 SDK 向模型发送查询,并从该模型获取回答。

      from google.cloud import aiplatform
    
      project_id = ""
      location = ""
      endpoint_id = "" # Use the short ID here
    
      aiplatform.init(project=project_id, location=location)
    
      endpoint = aiplatform.Endpoint(endpoint_id)
    
      prompt = "How to tell the time by looking at the sky?"
      instances=[{"text": prompt}]
      response = endpoint.predict(instances=instances, use_dedicated_endpoint=True)
      print(response.predictions)
    

    针对专用端点发出查询

    您可以使用允许的项目中的笔记本或虚拟机来测试查询。

    通过此示例查询,您可以将变量替换为您的 IP、项目、端点 ID 和模型 ID(从上面的部署步骤中获取)

    curl

    聊天补全

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" 'https://YOUR_IP/v1beta1/projects/YOUR_PROJECT_ID/locations/YOUR_LOCATION/endpoints/YOUR_ENDPOINT_ID/chat/completions' -d '{ "model": "YOUR_MODEL_ID", "max_tokens": 300, "messages": [{ "role": "user", "content": "how to tell the time by looking at sky?" }]}'
    

    预测

    $ curl -k -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" 'https:/YOUR_IP/v1beta1/projects/YOUR_PROJECT_ID/locations/YOUR_LOCATION/endpoints/YOUR_ENDPOINT_ID:predict' -d '{
      "instances": [
        {
          "prompt": "Summarize Goog stock performance",
          "temperature": 0,
          "top_p": 1,
          "max_tokens": 154,
          "ignore_eos": true
        }
      ]
    }'
    

    如需查看有关如何使用该 API 的另一个示例,请参阅导入自定义权重笔记本

    详细了解 Vertex AI 中的自行部署模型