כוונון של מודלי LLM לתרגום באמצעות כוונון מפוקח (SFT)

במאמר הזה מוסבר איך לכוונן מודל LLM של Translation באמצעות כוונון מפוקח (SFT).

לפני שמתחילים

לפני שמתחילים, צריך להכין מערך נתונים לכוונון מפוקח. הדרישות משתנות בהתאם לתרחיש לדוגמה.

מודלים נתמכים

  • ‫translation-llm-002 (תומך רק בכוונון טקסט)

יצירת משימת כוונון

אפשר ליצור משימת כוונון מפוקח באמצעות ה-API בארכיטקטורת REST או Google Gen AI SDK.

REST

כדי ליצור משימת כוונון של מודל, שולחים בקשת POST באמצעות ה-method‏ tuningJobs.create. חלק מהפרמטרים לא נתמכים בכל המודלים. חשוב לכלול רק את הפרמטרים הרלוונטיים למודל שאתם מכווננים.

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • ‫PROJECT_ID: מזהה הפרויקט. .
  • ‫TUNING_JOB_REGION: האזור שבו פועלת משימת הכוונון. זה גם האזור שבו המודל המכוונן מועלה כברירת מחדל. אזור נתמך: us-central1.
  • ‫BASE_MODEL: השם של מודל התרגום שרוצים לכוונן. ערכים נתמכים: translation-llm-002.
  • ‫TRAINING_DATASET_URI: ה-URI של מערך נתוני האימון ב-Cloud Storage. קבוצת הנתונים צריכה להיות בפורמט של קובץ JSONL. כדי לקבל את התוצאות הכי טובות, מומלץ לספק לפחות 100 עד 500 דוגמאות. מידע נוסף זמין במאמר מידע על מערך נתונים לכוונון מבוקר.
  • ‫VALIDATION_DATASET_URIאופציונלי: ה-URI של Cloud Storage של קובץ מערך הנתונים לאימות.
  • ‫TUNED_MODEL_DISPLAYNAMEאופציונלי: שם לתצוגה של המודל המכוונן. אם לא מגדירים שם, המערכת יוצרת שם אקראי.

ה-method של ה-HTTP וכתובת ה-URL:

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

גוף בקשת 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/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 שדומה לזו:

דוגמה לפקודת 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

import time

from google import genai
from google.genai.types import (
    CreateTuningJobConfig,
    EvaluationConfig,
    GcsDestination,
    HttpOptions,
    Metric,
    OutputConfig,
    TuningDataset,
)

# TODO(developer): Update and un-comment below line
# output_gcs_uri = "gs://your-bucket/your-prefix"

client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))

training_dataset = TuningDataset(
    gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini/text/sft_train_data.jsonl",
)
validation_dataset = TuningDataset(
    gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini/text/sft_validation_data.jsonl",
)

evaluation_config = EvaluationConfig(
    metrics=[
        Metric(
            name="FLUENCY",
            prompt_template="""Evaluate this {prediction}"""
        )
    ],
    output_config=OutputConfig(
        gcs_destination=GcsDestination(
            output_uri_prefix=output_gcs_uri,
        )
    ),
)

tuning_job = client.tunings.tune(
    base_model="gemini-3.5-flash",
    training_dataset=training_dataset,
    config=CreateTuningJobConfig(
        tuned_model_display_name="Example tuning job",
        validation_dataset=validation_dataset,
        evaluation_config=evaluation_config,
    ),
)

running_states = set([
    "JOB_STATE_PENDING",
    "JOB_STATE_RUNNING",
])

while tuning_job.state in running_states:
    print(tuning_job.state)
    tuning_job = client.tunings.get(name=tuning_job.name)
    time.sleep(60)

print(tuning_job.tuned_model.model)
print(tuning_job.tuned_model.endpoint)
print(tuning_job.experiment)
# Example response:
# projects/123456789012/locations/us-central1/models/1234567890@1
# projects/123456789012/locations/us-central1/endpoints/123456789012345
# projects/123456789012/locations/us-central1/metadataStores/default/contexts/tuning-experiment-2025010112345678

if tuning_job.tuned_model.checkpoints:
    for i, checkpoint in enumerate(tuning_job.tuned_model.checkpoints):
        print(f"Checkpoint {i + 1}: ", checkpoint)
    # Example response:
    # Checkpoint 1:  checkpoint_id='1' epoch=1 step=10 endpoint='projects/123456789012/locations/us-central1/endpoints/123456789000000'
    # Checkpoint 2:  checkpoint_id='2' epoch=2 step=20 endpoint='projects/123456789012/locations/us-central1/endpoints/123456789012345'

הצגת רשימה של משימות כוונון

כדי לראות את רשימת משימות הכוונון בפרויקט הנוכחי, אתם יכולים להשתמש במסוף Cloud de Confiance , ב-Google Gen AI SDK או לשלוח בקשת GET באמצעות ה-method‏ tuningJobs.

REST

כדי להציג רשימה של משימות לכוונון מודלים, שולחים בקשת GET באמצעות ה-method‏ tuningJobs.list.

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • ‫PROJECT_ID: מזהה הפרויקט.
  • ‫TUNING_JOB_REGION: האזור שבו פועלת משימת הכוונון. זה גם האזור שבו המודל המכוונן מועלה כברירת מחדל.

ה-method של ה-HTTP וכתובת ה-URL:

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

כדי לשלוח את הבקשה אתם צריכים לבחור אחת מהאפשרויות הבאות:

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

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/endpoints/ENDPOINT_ID:generateContent" | Select-Object -Expand Content

אתם אמורים לקבל תגובת JSON שדומה לזו:

Python

from google import genai
from google.genai.types import HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))

responses = client.tunings.list()
for response in responses:
    print(response.name)
    # Example response:
    # projects/123456789012/locations/us-central1/tuningJobs/123456789012345

המסוף

כדי לראות את משימות הכוונון במסוף Cloud de Confiance , נכנסים לדף Agent Studio.

עוברים אל Agent Studio.

משימות הכוונון של מודל ה-LLM לתרגום מופיעות בטבלה שבקטע Translation LLM tuned models.

קבלת פרטים על משימת כוונון

כדי לקבל את הפרטים של משימת כוונון בפרויקט הנוכחי, אתם יכולים להשתמש במסוף Cloud de Confiance , ב-Google Gen AI SDK או לשלוח בקשת GET באמצעות ה-method‏ tuningJobs.

REST

כדי לראות רשימה של משימות לכוונון מודלים, שולחים בקשת GET באמצעות ה-method‏ tuningJobs.get ומציינים את TuningJob_ID.

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • ‫PROJECT_ID: מזהה הפרויקט.
  • ‫TUNING_JOB_REGION: האזור שבו פועלת משימת הכוונון. זה גם האזור שבו המודל המכוונן מועלה כברירת מחדל.
  • ‫TUNING_JOB_ID: המזהה של משימת הכוונון.

ה-method של ה-HTTP וכתובת ה-URL:

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

כדי לשלוח את הבקשה אתם צריכים לבחור אחת מהאפשרויות הבאות:

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

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/endpoints/ENDPOINT_ID:generateContent" | Select-Object -Expand Content

אתם אמורים לקבל תגובת JSON שדומה לזו:

Python

from google import genai
from google.genai.types import HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))

# Get the tuning job and the tuned model.
# Eg. tuning_job_name = "projects/123456789012/locations/us-central1/tuningJobs/123456789012345"
tuning_job = client.tunings.get(name=tuning_job_name)

print(tuning_job.tuned_model.model)
print(tuning_job.tuned_model.endpoint)
print(tuning_job.experiment)
# Example response:
# projects/123456789012/locations/us-central1/models/1234567890@1
# projects/123456789012/locations/us-central1/endpoints/123456789012345
# projects/123456789012/locations/us-central1/metadataStores/default/contexts/tuning-experiment-2025010112345678

המסוף

  1. כדי לראות את הפרטים של מודל מכוונן במסוף Cloud de Confiance , נכנסים לדף Agent Studio.

    עוברים אל Agent Studio.

  2. בטבלה Translation LLM tuned models, מאתרים את המודל ולוחצים על Details.

    פרטי המודל מוצגים.

ביטול של משימת כוונון

אתם יכולים לבטל משימת כוונון בפרויקט הנוכחי באמצעות מסוף Cloud de Confiance , Google Gen AI SDK או שליחת בקשת POST באמצעות ה-method‏ tuningJobs.

REST

כדי לראות רשימה של משימות לכוונון מודלים, שולחים בקשת GET באמצעות ה-method‏ tuningJobs.cancel ומציינים את TuningJob_ID.

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • ‫PROJECT_ID: מזהה הפרויקט.
  • ‫TUNING_JOB_REGION: האזור שבו פועלת משימת הכוונון. זה גם האזור שבו המודל המכוונן מועלה כברירת מחדל.
  • ‫TUNING_JOB_ID: המזהה של משימת הכוונון.

ה-method של ה-HTTP וכתובת ה-URL:

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

כדי לשלוח את הבקשה אתם צריכים לבחור אחת מהאפשרויות הבאות:

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

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/endpoints/ENDPOINT_ID:generateContent" | Select-Object -Expand Content

אתם אמורים לקבל תגובת JSON שדומה לזו:

Python


from google import genai
from google.genai.types import HttpOptions


def cancel_tuning_job(tuning_job_name: str) -> None:
    client = genai.Client(http_options=HttpOptions(api_version="v1"))

    # Cancel the tuning job.
    # Eg. tuning_job_name = "projects/123456789012/locations/us-central1/tuningJobs/123456789012345"
    client.tunings.cancel(name=tuning_job_name)

המסוף

  1. כדי לבטל משימת כוונון במסוף Cloud de Confiance , נכנסים לדף Agent Studio.

    עוברים אל Agent Studio.

  2. בטבלה Translation tuned models, לוחצים על Manage run.

  3. לוחצים על ביטול.

בדיקת המודל המכוונן באמצעות הנחיה

כדי לבדוק משימת כוונון בפרויקט הנוכחי, אתם יכולים להשתמש ב-Google Gen AI SDK או לשלוח בקשת POST באמצעות ה-method‏ tuningJobs.

בדוגמה הבאה, שולחים למודל את ההנחיה "Why is sky blue?‎".

REST

כדי לבדוק מודל מכוונן באמצעות הנחיה, שולחים בקשת POST ומציינים את ה-TUNED_ENDPOINT_ID.

לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: .
  • ‫TUNING_JOB_REGION: האזור שבו פועלת משימת הכוונון. זה גם האזור שבו המודל המכוונן מועלה כברירת מחדל.
  • ‫ENDPOINT_ID: מזהה נקודת הקצה של המודל המכוונן מ-GET API.

ה-method של ה-HTTP וכתובת ה-URL:

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 google import genai
from google.genai.types import HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))

# Get the tuning job and the tuned model.
# Eg. tuning_job_name = "projects/123456789012/locations/us-central1/tuningJobs/123456789012345"
tuning_job = client.tunings.get(name=tuning_job_name)

contents = "Why is the sky blue?"

# Predicts with the tuned endpoint.
response = client.models.generate_content(
    model=tuning_job.tuned_model.endpoint,
    contents=contents,
)
print(response.text)
# Example response:
# The sky is blue because ...

מדדי כוונון ואימות

אתם יכולים להגדיר משימת כוונון מודל כדי לאסוף ולדווח על מדדי כוונון והערכה של המודל, ואז להציג אותם באופן חזותי ב-Agent Studio.

  1. כדי לראות את הפרטים של מודל מכוונן במסוף Cloud de Confiance , נכנסים לדף Agent Studio.

    עוברים אל Agent Studio.

  2. בטבלה Tune and Distill, לוחצים על שם המודל המכוונן שרוצים לראות את המדדים שלו.

    מדדי הכוונון מופיעים בכרטיסייה Monitor.

מדדים של כוונון מודל

משימת כוונון המודל אוספת באופן אוטומטי את מדדי הכוונון הבאים של translation-llm-002.

  • ‫/train_total_loss: אובדן של מערך הנתונים לכוונון בשלב אימון.
  • /train_fraction_of_correct_next_step_preds: דיוק האסימון בשלב אימון. הסקה אחת מורכבת מרצף של אסימונים חזויים. המדד הזה בודק את הדיוק של הטוקנים החזויים בהשוואה לאמת הקרקע (ground truth) במערך הנתונים לכוונון.
  • ‫/train_num_predictions: מספר הטוקנים החזויים בשלב אימון.

מדדי אימות של המודל:

אתם יכולים להגדיר משימת כוונון מודל כדי לאסוף את מדדי האימות הבאים של translation-llm-002.

  • ‫/eval_total_loss: אובדן של מערך נתוני האימות בשלב אימות.
  • ‫/eval_fraction_of_correct_next_step_preds: דיוק הטוקן בשלב האימות. הסקה אחת מורכבת מרצף של אסימונים חזויים. המדד הזה בודק את הדיוק של הטוקנים החזויים בהשוואה לאמת הקרקע (ground truth) במערך הנתונים לאימות.
  • /eval_num_predictions: מספר האסימונים החזויים בשלב אימות.

התרשימים של המדדים זמינים אחרי שמתחילים להריץ את משימת הכוונון. הוא יתעדכן בזמן אמת ככל שהכוונון יתקדם. אם לא תציינו קבוצת נתונים לאימות כשתצרו את משימת הכוונון, רק התצוגות החזותיות של מדדי הכוונון יהיו זמינות.

המאמרים הבאים