调优代码生成模型

对预训练的代码生成模型进行调优。

代码示例

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 示例浏览器。