調整程式碼生成模型

調整預先訓練的程式碼生成模型。

程式碼範例

Python

在試用這個範例之前,請先按照「使用用戶端程式庫的 Vertex AI 快速入門導覽課程」中的 Python 設定說明操作。詳情請參閱 Vertex AI Python API 參考說明文件。

如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

import os

from google import genai

# TODO (Developer) Set environment variables
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
LOCATION_ID = os.getenv("LOCATION_ID", "us-central1")

# Resource format: 'publishers/google/models/{model_id}'
BASE_MODEL_RESOURCE = "publishers/google/models/gemini-2.5-flash"
TRAINING_DATASET = (
    "gs://cloud-samples-data/ai-platform/generative_ai/gemini/text/sft_train_data.jsonl"
)


def tune_code_generation_model() -> genai.types.TuningJob:
    """Submits a supervised fine-tuning job for a Gemini model on code/text tasks."""

    client = genai.Client(
        enterprise=True,
        project=PROJECT_ID,
        location=LOCATION_ID,
    )

    tuning_job = client.tunings.tune(
        base_model=BASE_MODEL_RESOURCE,
        training_dataset=genai.types.TuningDataset(
            gcs_uri=TRAINING_DATASET,
        ),
        config=genai.types.CreateTuningJobConfig(
            tuned_model_display_name="tuned_gemini_code_model",
            epoch_count=2,
            learning_rate_multiplier=1.0,
        ),
    )

    print(f"Tuning job submitted successfully: {tuning_job.name}")
    print(f"Current State: {tuning_job.state}")

    return tuning_job

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽工具。