部署具有自定义权重的模型是一项预览版服务。您可以基于一组预定义的基础模型对模型进行微调,并在 Vertex AI Model Garden 上部署您的自定义模型。您可以将模型制品上传到项目中的 Cloud Storage 存储桶,从而部署使用自定义权重导入的自定义模型,这在 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.create
permission. 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.enable
permission. 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.create
permission. 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.enable
permission. 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 页面。
点击部署具有自定义权重的模型。系统随即会显示在 Vertex AI 上部署具有自定义权重的模型窗格。
在模型来源部分中,执行以下操作:
点击浏览,然后选择存储模型的存储桶,再点击选择。
可选:在模型名称字段中输入模型名称。
在部署设置部分,执行以下操作:
在区域字段中,选择您的区域,然后点击确定。
在机器规格字段中,选择用于部署模型的机器规格。
可选:在端点名称字段中,默认会显示模型的端点。但是,您可以在相应字段中输入其他端点名称。
点击部署具有自定义权重的模型。
在 Google Cloud 控制台中,前往 Model Garden 页面。
点击查看我的端点和模型。
从区域列表中选择您的区域。
如需获取端点 ID 和端点网址,请在我的端点部分中点击您的端点。
您的端点 ID 会显示在端点 ID 字段中。
您的公共端点网址会显示在专用端点字段中。
如需获取模型 ID,请在已部署的模型部分中找到您的模型,然后按照以下步骤操作:
- 点击模型字段中已部署模型的名称。
- 点击版本详细信息。您的模型 ID 会显示在模型 ID 字段中。
- 如需详细了解自行部署的模型,请参阅自行部署的模型概览。
- 如需详细了解 Model Garden,请参阅 Model Garden 概览。
- 如需详细了解如何部署模型,请参阅使用 Model Garden 中的模型。
- 使用 Gemma 开放模型
- 使用 Llama 开放模型
- 使用 Hugging Face 开放模型
本教程假定您使用 Cloud Shell 与 Google Cloud进行互动。如果您想使用其他 shell 取代 Cloud 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 的不同方式。
对话补全(一元)
此示例请求会向模型发送完整的对话消息,并在生成整个回答后以单个块的形式获取回答。这类似于发送短信并获得一条完整的回复。
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)
如需查看有关如何使用该 API 的另一个示例,请参阅导入自定义权重笔记本。