יצירה וניהול של מודלים של LLM לתרגום

במאמר הזה מוסבר איך לשפר ולהשתמש במודל LLM מותאם אישית של Translation.

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

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

הפעלת Vertex AI API

כדי לכוונן מודל TLLM בהתאמה אישית, צריך לבצע את הפעולות הבאות:

מפעילים את Vertex AI API, אם הוא עדיין לא מופעל.

התפקידים שנדרשים כדי להפעיל ממשקי API

כדי להפעיל ממשקי API, צריך את ההרשאה serviceusage.services.enable. אם יצרתם את הפרויקט, סביר להניח שכבר יש לכם את ההרשאה הזו דרך התפקיד "בעלים" (roles/owner). אחרת, תוכלו לקבל את ההרשאה הזו דרך התפקיד "אדמין בממשק 'שימוש בשירות'" (roles/serviceusage.serviceUsageAdmin). איך מקצים תפקידים

להפעלת ה-API

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

  • 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/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

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'

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

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

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/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

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

המסוף

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

    עוברים אל Agent Platform Studio.

  2. משימות הכוונון של Translation LLM מופיעות בטבלה בקטע Translation LLM tuned models.

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

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

REST

כדי לראות את הפרטים של משימת כוונון מודל, שולחים בקשת GET באמצעות השיטה tuningJobs.get ומציינים את הערך של TUNING_JOB_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/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

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. כדי לראות את הפרטים של מודל מכוונן במסוף Google Cloud , נכנסים לדף Agent Platform Studio.

    עוברים אל Agent Platform Studio.

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

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

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

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

REST

כדי לבטל משימת כוונון של מודל, שולחים בקשת POST באמצעות ה-method‏ tuningJobs.cancel ומציינים את TUNING_JOB_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/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


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. כדי לבטל משימת כוונון במסוף Google Cloud , נכנסים לדף Agent Platform Studio.

    עוברים אל Agent Platform Studio.

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

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

קבלת מידע על מודל

בסיום האימון, תוכלו לקבל מידע על המודל, כמו מזהה המודל.

כדי לראות את רשימת המודלים הזמינים, נכנסים לדף Agent Platform Studio.

עוברים אל Agent Platform Studio.

שימוש במודל המכוונן

בדוגמה הבאה, הטקסט מתורגם באמצעות מודל בהתאמה אישית עם מזהה המודל 1395675701985363739. כדי להשתמש במודל ה-LLM המותאם אישית לתרגום, מציינים את models/translation-llm-custom/{model-id} כמזהה המודל.

אפשר לציין את המודל שבו רוצים להשתמש לתרגום באמצעות פרמטר השאילתה model.

REST

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

  • ‫PROJECT_ID: מזהה הפרויקט ב- Google Cloud .
  • ‫LOCATION: האזור שבו נמצא המודל בהתאמה אישית, למשל us-central1.

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

POST https://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs/TUNING_JOB_ID:cancel

גוף בקשת 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://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs/TUNING_JOB_ID:cancel"

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://TUNING_JOB_REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/TUNING_JOB_REGION/tuningJobs/TUNING_JOB_ID:cancel" | Select-Object -Expand Content

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

{
  "translation": {
    "translatedText": "Доктор Ватсон, пожалуйста, откажитесь от своего мусора.
    Вы поделились нежелательной электронной почтой со мной. Давайте поговорим о
    спаме и важности рейтинга в конфиденциальном режиме.",
    "model": "projects/PROJECT_NUMBER/locations/LOCATION/model/translation-llm-custom/1395675701985363739"
  }
}

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 Platform Studio.

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

    עוברים אל Agent Platform 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: מספר האסימונים החזויים בשלב אימות.

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

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