使用自訂權重部署模型為預先發布版功能。您可以根據預先定義的一組基礎模型微調模型,並在 Vertex AI Model Garden 中部署自訂模型。您可以透過上傳模型構件至專案中的 Cloud Storage bucket,使用自訂權重匯入功能部署自訂模型,在 Vertex AI 中只要按一下即可完成。
支援的模型
使用自訂權重部署模型的公開測試版支援下列基礎模型:
| 模型名稱 | 版本 | 
|---|---|
| Llama | 
 | 
| Gemma | 
 | 
| Qwen | 
 | 
| Deepseek | 
 | 
| Mistral 和 Mixtral | 
 | 
| Phi-4 | 
 | 
| OpenAI OSS | 
 | 
限制
自訂權重不支援匯入量化模型。
模型檔案
您必須以 Hugging Face 權重格式提供模型檔案。如要進一步瞭解 Hugging Face 權重格式,請參閱「使用 Hugging Face 模型」。
如果未提供必要檔案,模型部署作業可能會失敗。
下表列出模型檔案類型,這些類型取決於模型的架構:
| 模型檔案內容 | 檔案類型 | 
|---|---|
| 模型設定 | 
 | 
| 模型權重 | 
 | 
| 權重指數 | 
 | 
| 分詞器檔案 | 
 | 
位置
您可以透過 Model Garden 服務,在所有區域部署自訂模型。
必要條件
本節說明如何部署自訂模型。
事前準備
- 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.
- 
    
    
      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
      (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 Vertex AI API. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. 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
      (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 Vertex AI API. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
    
    In the Google Cloud console, 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. 
- 
      Install the Google Cloud CLI. 
- 
          If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity. 
- 
        To initialize the gcloud CLI, run the following command: gcloud init
- 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 控制台的「Model Garden」頁面。 
- 按一下「Deploy model with custom weights」(使用自訂權重部署模型)。系統會顯示「Deploy a model with custom weights on Vertex AI」(在 Vertex AI 上部署具有自訂權重的模型) 窗格。 
- 在「模型來源」部分執行下列操作: - 按一下「瀏覽」,選擇儲存模型的 bucket,然後按一下「選取」。 
- 選用:在「模型名稱」欄位中輸入模型名稱。 
 
- 在「部署設定」部分,執行下列操作: - 從「區域」欄位選取您的區域,然後按一下「確定」。 
- 在「Machine Spec」(機器規格) 欄位中,選取用於部署模型的機器規格。 
- 選用:在「端點名稱」欄位中,系統會預設顯示模型的端點。不過,您可以在欄位中輸入其他端點名稱。 
 
- 按一下「Deploy model with custom weights」(使用自訂權重部署模型)。 
- 前往 Google Cloud 控制台的「Model Garden」頁面。 
- 按一下「查看我的端點與模型」。 
- 從「Region」(區域) 清單中選取區域。 
- 如要取得端點 ID 和端點網址,請按一下「我的端點」部分中的端點。 - 端點 ID 會顯示在「端點 ID」欄位中。 - 公開端點網址會顯示在「專屬端點」欄位中。 
- 如要取得模型 ID,請在「已部署的模型」部分中找出模型,然後按照下列步驟操作: - 在「Model」欄位中,按一下已部署的模型名稱。
- 按一下「版本詳細資料」。型號 ID 會顯示在「型號 ID」欄位。
 
- 如要進一步瞭解自行部署的模型,請參閱自行部署的模型總覽。
- 如要進一步瞭解 Model Garden,請參閱「Model Garden 總覽」。
- 如要進一步瞭解如何部署模型,請參閱「在 Model Garden 中使用模型」。
- 使用 Gemma 開放式模型
- 使用 Llama 開放式模型
- 使用 Hugging Face 開放原始碼模型
本教學課程假設您使用 Cloud Shell 與 Google Cloud互動。如要使用 Cloud Shell 以外的 Shell,請執行下列額外設定:
部署自訂模型
本節說明如何部署自訂模型。
如果您使用指令列介面 (CLI)、Python 或 JavaScript,請將下列變數替換為值,程式碼範例才能正常運作:
主控台
下列步驟說明如何使用 Google Cloud 控制台,部署具有自訂權重的模型。
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 或 SDK 傳送查詢。
傳送查詢前,請先取得端點網址、端點 ID 和模型 ID,這些資訊可在 Google Cloud 控制台中找到。
如要取得這類資訊,請按照下列步驟操作:
取得端點和已部署模型資訊後,請參閱下列程式碼範例,瞭解如何傳送推論要求,或參閱「將線上推論要求傳送至專用公開端點」。
API
以下程式碼範例會根據您的用途,示範使用 API 的不同方式。
Chat completion (unary)
這個範例要求會將完整的即時通訊訊息傳送至模型,並在生成完整的回覆後,以單一區塊的形式取得回覆。這與傳送簡訊並取得單一完整回覆類似。
  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
      }
    ]
  }'
原始預測
這項要求是 Predict 要求的串流版本。使用 :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)
如需使用 API 的其他範例,請參閱匯入自訂權重筆記本。