Tune Code Generation Model

Tune a pre-trained code generation model.

Code sample

Python

Before trying this sample, follow the Python setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Python API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.