使用監督式微調功能調整翻譯大型語言模型

本文說明如何使用監督式微調功能,調整 Translation LLM 模型。

事前準備

開始前,您必須準備監督式微調資料集。視用途而定,有不同的規定。

支援的機型

  • translation-llm-002 (僅支援文字調整)

建立微調工作

您可以使用 REST API 或 Agent Platform Python SDK 建立監督式微調工作。

REST

如要建立模型微調作業,請使用 tuningJobs.create 方法傳送 POST 要求。部分參數不支援所有模型,請務必只為要微調的模型加入適用參數。

使用任何要求資料之前,請先修改下列項目的值:

  • PROJECT_ID: 您的 [專案 ID](/resource-manager/docs/creating-managing-projects#identifiers)。 。
  • TUNING_JOB_REGION:調整工作執行的區域。這也是上傳調整後模型的預設區域。支援的區域:us-central1
  • BASE_MODEL:要調整的翻譯模型名稱。支援的值:translation-llm-002
  • TRAINING_DATASET_URI:訓練資料集的 Cloud Storage URI。資料集必須採用 JSONL 檔案格式。為獲得最佳結果,請提供至少 100 到 500 個範例。詳情請參閱「關於監督式調整資料集」。
  • VALIDATION_DATASET_URI選用:驗證資料集檔案的 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 回應。

cURL 指令範例

PROJECT_ID=myproject
LOCATION=us-central1
curl \
-X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
"https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/tuningJobs" \
-d \
$'{
   "baseModel": "translation-llm-002",
   "supervisedTuningSpec" : {
      "training_dataset_uri": "gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl",
      "validation_dataset_uri": "gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_validation_data.jsonl"
   },
   "tunedModelDisplayName": "tuned_translation_llm"
}'

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 控制台、適用於 Python 的 Agent Platform SDK,或使用 tuningJobs 方法傳送 GET 要求。

REST

如要查看模型微調作業清單,請使用 tuningJobs.list 方法傳送 GET 要求。

使用任何要求資料之前,請先修改下列項目的值:

  • PROJECT_ID: 您的 [專案 ID](/resource-manager/docs/creating-managing-projects#identifiers)。 。
  • 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

import vertexai
from vertexai.tuning import sft

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"
vertexai.init(project=PROJECT_ID, location="us-central1")

responses = sft.SupervisedTuningJob.list()

for response in responses:
    print(response)
# Example response:
# <vertexai.tuning._supervised_tuning.SupervisedTuningJob object at 0x7c85287b2680>
# resource name: projects/12345678/locations/us-central1/tuningJobs/123456789012345

控制台

如要在 Google Cloud 控制台中查看微調工作,請前往 Agent Platform Studio 頁面。

前往 Agent Platform Studio

翻譯專用大型語言模型微調工作會列在「翻譯專用大型語言模型微調模型」部分下方的表格中。

取得調整工作的詳細資料

您可以使用 Google Cloud 控制台、適用於 Python 的 Agent Platform SDK,或使用 tuningJobs 方法傳送 GET 要求,取得目前專案中微調工作的詳細資料。

REST

如要查看模型微調作業清單,請使用 tuningJobs.get 方法傳送 GET 要求,並指定 TuningJob_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

import vertexai
from vertexai.tuning import sft

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# LOCATION = "us-central1"
vertexai.init(project=PROJECT_ID, location=LOCATION)

tuning_job_id = "4982013113894174720"
response = sft.SupervisedTuningJob(
    f"projects/{PROJECT_ID}/locations/{LOCATION}/tuningJobs/{tuning_job_id}"
)

print(response)
# Example response:
# <vertexai.tuning._supervised_tuning.SupervisedTuningJob object at 0x7cc4bb20baf0>
# resource name: projects/1234567890/locations/us-central1/tuningJobs/4982013113894174720

控制台

  1. 如要在 Google Cloud 控制台中查看微調模型的詳細資料,請前往「Agent Platform Studio」頁面。

    前往 Agent Platform Studio

  2. 在「Translation LLM tuned models」(已調整的 Translation LLM 模型) 表格中,找出您的模型並點選「Details」(詳細資料)

    系統會顯示模型詳細資料。

取消微調工作

如要取消目前專案的微調作業,可以使用 Google Cloud 控制台、Agent Platform SDK for Python,或使用 tuningJobs 方法傳送 POST 要求。

REST

如要查看模型微調作業清單,請使用 tuningJobs.cancel 方法傳送 GET 要求,並指定 TuningJob_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

import vertexai
from vertexai.tuning import sft

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# LOCATION = "us-central1"
vertexai.init(project=PROJECT_ID, location=LOCATION)

tuning_job_id = "4982013113894174720"
job = sft.SupervisedTuningJob(
    f"projects/{PROJECT_ID}/locations/{LOCATION}/tuningJobs/{tuning_job_id}"
)
job.cancel()

控制台

  1. 如要在 Google Cloud 控制台中取消微調作業,請前往「Agent Platform Studio」頁面。

    前往 Agent Platform Studio

  2. 在「Translation tuned models」(翻譯微調模型) 表格中,按一下「管理執行」

  3. 按一下「取消」

使用提示詞測試微調模型

如要測試目前專案中的微調工作,可以使用 Python 適用的 Agent Platform SDK,或使用 tuningJobs 方法傳送 POST 要求。

以下範例會提示模型回答「為什麼天空是藍的?」這個問題。

REST

如要使用提示測試微調模型,請傳送 POST 要求並指定 TUNED_ENDPOINT_ID

使用任何要求資料之前,請先修改下列項目的值:

  • PROJECT_ID:。
  • TUNING_JOB_REGION:調整工作執行的區域。這也是上傳調整後模型的預設區域。
  • ENDPOINT_ID:GET API 傳回的微調模型端點 ID。

HTTP 方法和網址:

POST https://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/endpoints/ENDPOINT_ID:generateContent

JSON 要求內文:

{
    "contents": [
        {
            "role": "USER",
            "parts": {
                "text" : "English: Hello. Spanish:"
            }
        }
    ],
}

如要傳送要求,請選擇以下其中一個選項:

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/endpoints/ENDPOINT_ID:generateContent"

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/endpoints/ENDPOINT_ID:generateContent" | 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))

調整及驗證指標

您可以設定模型微調工作,收集及回報模型微調和模型評估指標,然後在 Agent Platform Studio 中以視覺化方式呈現。

  1. 如要在 Google Cloud 控制台中查看微調模型的詳細資料,請前往 Agent Platform Studio 頁面。

    前往 Agent Platform Studio

  2. 在「微調和蒸餾」表格中,按一下要查看指標的微調模型名稱。

    調整指標會顯示在「監控」分頁下方。

模型調整指標

模型微調作業會自動收集 translation-llm-002 的下列微調指標。

  • /train_total_loss:訓練步驟中微調資料集的損失。
  • /train_fraction_of_correct_next_step_preds:訓練步驟的符記準確率。單一推論包含一連串的預測符記。這項指標會比較預測符記與微調資料集中的實際資料,藉此衡量預測符記的準確率。
  • /train_num_predictions: 訓練步驟中預測的權杖數量。

模型驗證指標:

您可以設定模型微調工作,為 translation-llm-002 收集下列驗證指標。

  • /eval_total_loss:驗證步驟中驗證資料集的損失。
  • /eval_fraction_of_correct_next_step_preds:驗證步驟中的詞元準確率。單一推論包含一系列預測的符記。這項指標會比較驗證資料集中的預測詞元與基準真相,藉此評估預測詞元的準確率。
  • /eval_num_predictions:驗證步驟中預測的權杖數量。

調整工作開始執行後,系統就會提供指標的視覺化資料。 調整作業進行時,系統會即時更新資料。 如果您在建立調整工作時未指定驗證資料集,系統只會提供調整指標的視覺化資料。

後續步驟