创建和管理模型
本文档介绍如何调优和使用自定义翻译 LLM 模型。
准备工作
在开始之前,您必须准备好监督式微调数据集。 根据您的应用场景,要求会有所不同。
- 准备用于调优的文本数据集:文本调优
启用 Vertex AI API
如需调优自定义 TLLM 模型,您需要执行以下操作:
启用 Vertex AI API。
启用 API 所需的角色
如需启用 API,您需要拥有 serviceusage.services.enable 权限。如果您创建了项目,则您可能已通过所有者角色 (roles/owner) 拥有此权限。否则,您可以通过 Service Usage Admin 角色 (roles/serviceusage.serviceUsageAdmin) 获得此权限。了解如何授予角色。
支持的模型
translation-llm-002(支持文本调优。处于预览版阶段。)
创建调优作业
您可以使用 REST API 或 Vertex AI SDK for Python 创建监督式微调作业。
REST
如需创建模型调优作业,请使用 tuningJobs.create 方法发送 POST 请求。某些参数只受部分模型支持。确保仅包含您要调优的模型的适用参数。
在使用任何请求数据之前, 请进行以下替换:
- PROJECT_ID:PROJECT_ID。
- TUNING_JOB_REGION:运行调优作业的区域。这也是上传调优后模型的默认区域。受支持的区域:
us-central1。 - BASE_MODEL:要调优的翻译模型的名称。支持的值:
translation-llm-002。 - TRAINING_DATASET_URI:训练数据集的 Cloud Storage URI。数据集必须采用 JSONL 文件格式。为获得最佳效果,请提供至少 100 到 500 个样本。如需了解详情,请参阅监督式调优数据集简介。
- VALIDATION_DATASET_URIOptional:验证数据集文件的 Cloud Storage URI。
- TUNED_MODEL_DISPLAYNAME可选:调优后模型的显示名称。如果未设置,则会生成随机名称。
HTTP 方法和网址:
POST https://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs
请求 JSON 正文:
{
"baseModel": "BASE_MODEL",
"supervisedTuningSpec" : {
"trainingDatasetUri": "TRAINING_DATASET_URI",
"validationDatasetUri": "VALIDATION_DATASET_URI",
},
"tunedModelDisplayName": "TUNED_MODEL_DISPLAYNAME"
}
如需发送请求,请选择以下方式之一:
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://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs"
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://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。
Python
from vertexai.generative_models import GenerativeModel
sft_tuning_job = sft.SupervisedTuningJob("projects/<PROJECT_ID>/locations/<TUNING_JOB_REGION>/tuningJobs/<TUNING_JOB_ID>")
tuned_model = GenerativeModel(sft_tuning_job.tuned_model_endpoint_name)
print(tuned_model.generate_content(content))
import time
import vertexai
from vertexai.tuning import sft
# TODO(developer): Update and un-comment below line.
# PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]
vertexai.init(project=PROJECT_ID, location="us-central1")
sft_tuning_job = sft.train(
source_model="translation-llm-002",
train_dataset="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl",
# The following parameters are optional
validation_dataset="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_validation_data.jsonl",
tuned_model_display_name="tuned_translation_llm_002",
)
# Polling for job completion
while not sft_tuning_job.has_ended:
time.sleep(60)
sft_tuning_job.refresh()
print(sft_tuning_job.tuned_model_name)
print(sft_tuning_job.tuned_model_endpoint_name)
print(sft_tuning_job.experiment)
# Example response:
# projects/123456789012/locations/us-central1/models/1234567890@1
# projects/123456789012/locations/us-central1/endpoints/123456789012345
# <google.cloud.aiplatform.metadata.experiment_resources.Experiment object at 0x7b5b4ae07af0>
查看调优作业列表
您可以使用 Google Cloud 控制台、Vertex AI SDK for Python 或使用 tuningJobs 方法发送 GET 请求来查看当前项目中的调优作业列表。
REST
如需查看模型调优作业列表,请使用 tuningJobs.list 方法发送 GET 请求。
在使用任何请求数据之前, 请进行以下替换:
- PROJECT_ID:PROJECT_ID。
- TUNING_JOB_REGION:运行调优作业的区域。这也是上传调优后模型的默认区域。
HTTP 方法和网址:
GET https://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。
Python
控制台
如需在 Google Cloud 控制台中查看调优作业,请前往 Vertex AI Studio 页面。
查看列在翻译 LLM 调优的模型 部分下的表格中的翻译 LLM 调优作业。
获取调优作业的详细信息
如需获取当前项目中调优作业的详细信息,您可以使用 Google Cloud 控制台、Vertex AI SDK for Python 或使用 tuningJobs 方法发送 GET 请求。
REST
如需查看模型调优作业列表,请使用 tuningJobs.get 方法发送 GET 请求并指定 TuningJob_ID。
在使用任何请求数据之前, 请进行以下替换:
- PROJECT_ID:PROJECT_ID。
- TUNING_JOB_REGION:运行调优作业的区域。这也是上传调优后模型的默认区域。
- TUNING_JOB_ID:调优作业的 ID。
HTTP 方法和网址:
GET https://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs/TUNING_JOB_ID
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs/TUNING_JOB_ID"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs/TUNING_JOB_ID" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。
Python
控制台
如需在 Google Cloud 控制台中查看调优的模型的详细信息,请前往 Vertex AI Studio 页面。
在翻译 LLM 调优的模型表中,找到您的模型,然后点击详细信息。
系统会显示模型的详细信息。
取消调优作业
您可以通过使用 Google Cloud 控制台、Vertex AI SDK for Python 或使用 tuningJobs 方法发送 POST 请求,在当前项目中测试调优作业。
REST
如需查看模型调优作业列表,请使用 tuningJobs.cancel 方法发送 GET 请求并指定 TuningJob_ID。
在使用任何请求数据之前, 请进行以下替换:
- PROJECT_ID:PROJECT_ID。
- TUNING_JOB_REGION:运行调优作业的区域。这也是上传调优后模型的默认区域。
- TUNING_JOB_ID:调优作业的 ID。
HTTP 方法和网址:
POST https://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs/TUNING_JOB_ID:cancel
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d "" \
"https://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs/TUNING_JOB_ID:cancel"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-Uri "https://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs/TUNING_JOB_ID:cancel" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应。
Python
控制台
如需在 Google Cloud 控制台中取消调优作业,请前往 Vertex AI Studio 页面。
在翻译调优的模型表中,点击 管理运行。
点击取消 。
获取模型的相关信息
训练完成后,您可以获取模型的相关信息,例如模型 ID。
如需查看可用模型的列表,请前往 Vertex AI 端点页面。
使用调整后模型
以下示例使用模型 ID 为 1395675701985363739 的自定义模型来翻译文本。如需使用自定义翻译 LLM,请将
models/translation-llm-custom/{model-id}
指定为模型 ID。
您可以使用
model
查询参数指定用于翻译的模型。
REST
在使用任何请求数据之前, 请进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目 ID。
- LOCATION:自定义模型所在的区域,例如
us-central1。
HTTP 方法和网址:
POST https://translation.googleapis.com/v3/projects/PROJECT_ID/locations/LOCATION:translateText
请求 JSON 正文:
{
"model": "projects/PROJECT_ID/locations/LOCATION/model/translation-llm-custom/1395675701985363739",
"sourceLanguageCode": "en",
"targetLanguageCode": "ru",
"contents": ["Dr. Watson, please discard your trash. You've shared unsolicited email with me.
Let's talk about spam and importance ranking in a confidential mode."]
}
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: PROJECT_ID" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://translation.googleapis.com/v3/projects/PROJECT_ID/locations/LOCATION:translateText"
PowerShell
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_ID" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://translation.googleapis.com/v3/projects/PROJECT_ID/locations/LOCATION:translateText" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{
"translation": {
"translatedText": "Доктор Ватсон, пожалуйста, откажитесь от своего мусора.
Вы поделились нежелательной электронной почтой со мной. Давайте поговорим о
спаме и важности рейтинга в конфиденциальном режиме.",
"model": "projects/PROJECT_NUMBER/locations/LOCATION/model/translation-llm-custom/1395675701985363739"
}
}
Python
from google.cloud import translate
def translate_text_with_model(
text: str = "YOUR_TEXT_TO_TRANSLATE",
project_id: str = "YOUR_PROJECT_ID",
model_id: str = "YOUR_MODEL_ID",
) -> translate.TranslationServiceClient:
"""Translates a given text using Translation custom model."""
client = translate.TranslationServiceClient()
location = "us-central1"
parent = f"projects/{project_id}/locations/{location}"
model_path = f"{parent}/models/translation-llm-custom/{model_id}"
# Supported language codes: https://cloud.google.com/translate/docs/languages
response = client.translate_text(
request={
"contents": [text],
"target_language_code": "ja",
"model": model_path,
"source_language_code": "en",
"parent": parent,
"mime_type": "text/plain", # mime types: text/plain, text/html
}
)
# Display the translation for each input text provided
for translation in response.translations:
print(f"Translated text: {translation.translated_text}")
return response
调优和验证指标
您可以配置模型调优作业,以收集和报告模型调优和模型评估指标,然后在 Vertex AI Studio 中直观呈现这些指标。
如需在 Google Cloud 控制台中查看调优的模型的详细信息,请前往 Vertex AI Studio 页面。
在调优和提炼表格中,点击要查看指标的调优模型的名称。
调优指标会显示在监控标签页下。
模型调优指标
模型调优作业会自动为 translation-llm-002 收集以下调优指标。
/train_total_loss:一个训练步长中调整数据集的损失。/train_fraction_of_correct_next_step_preds:一个训练步长中的词元准确率。单次推理由一系列预测的 token 组成。该指标衡量预测词元的准确率(与调优数据集中的标准答案相比)。/train_num_predictions::一个训练步长中的预测词元数。
模型验证指标:
您可以配置模型调优作业,以便为 translation-llm-002 收集以下验证指标。
/eval_total_loss:验证步骤中验证数据集的损失。/eval_fraction_of_correct_next_step_preds:一个验证步长中的词元准确率。单次推理由一系列预测的 token 组成。该指标衡量评估词元的准确率(与验证数据集中的标准答案相比)。/eval_num_predictions:一个验证步长中的预测词元数。
调优作业开始运行后,指标可视化即可使用。 随着调优的进行,它会实时更新。 如果您在创建调优作业时未指定验证数据集,则系统只提供调优指标的可视化内容。
后续步骤
- 如需了解如何在构建生成式 AI 知识库的解决方案中使用监督式微调,请参阅快速起步解决方案:生成式 AI 知识库。