Chuyển đến nội dung chính

Lesson 7: Fine-tune Gemini Flash — Supervised Tuning Step-by-step

Hands-on fine-tune Gemini 2.0 Flash on Vertex AI. Upload dataset to GCS. Configure tuning job. Monitor training. Deploy endpoint. Test model.

🧠 AI & ML — Lesson 6 Lesson 7: Fine-tune Gemini Flash — Supervised Tuning Step-by-step

Fine-tuning LLM: The Art of AI Tuning

Part 3: Fine-tuning on Google Gemini / Vertex AI

xdev.asia

Introduction

This is the most important lesson — you will fine-tune the Gemini model from A to Z on Vertex AI.


1. Prepare Dataset

import json

# Dataset format cho Gemini
training_examples = [
    {
        "messages": [
            {"role": "user", "content": "Phân tích code này và đề xuất cải thiện:\n```python\ndef calc(x,y): return x+y```"},
            {"role": "model", "content": "## Code Review\n\n### Vấn đề:\n1. Tên hàm `calc` không rõ ràng\n2. Thiếu type hints\n3. Thiếu docstring\n\n### Đề xuất:\n```python\ndef add_numbers(x: float, y: float) -> float:\n \"\"\"Sum two numbers.\"\"\"\n return x + y\n```"}
        ]
    },
    # ... 100+ examples
]

# Save as JSONL
with open("gemini_training.jsonl", "w") as f:
    for ex in training_examples:
        f.write(json.dumps(ex, ensure_ascii=False) + "\n")

2. Upload & Launch Tuning Job

from google.cloud import aiplatform

aiplatform.init(project="my-project", location="us-central1")

# Launch supervised tuning
tuning_job = aiplatform.SupervisedTuningJob(
    source_model="gemini-2.0-flash",
    train_dataset="gs://my-bucket/gemini_training.jsonl",
    validation_dataset="gs://my-bucket/gemini_validation.jsonl",
    tuned_model_display_name="code-reviewer-v1",
    epochs=3,
    learning_rate_multiplier=1.0,
)

tuning_job.run()
print(f"Tuned model: {tuning_job.tuned_model_endpoint_name}")

3. Test Fine-tuned vs Base Model

from google import genai

client = genai.Client()

# Base model
base_response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents="Review this code: def f(x): return x*2"
)

# Fine-tuned model
ft_response = client.models.generate_content(
    model=tuning_job.tuned_model_name,
    contents="Review this code: def f(x): return x*2"
)

print("=== BASE ===")
print(base_response.text)
print("\n=== FINE-TUNED ===")
print(ft_response.text)

Summary

  • Gemini fine-tuning = upload JSONL → launch job → wait → test
  • Vertex AI manages the GPU itself, you only need the data
  • Compare base vs fine-tuned → measure improvement
  • Iterate: adjust data, retrain if not achieved

Exercises

  1. Fine-tune Gemini Flash with 100+ examples for the use case you choose
  2. Compare output: base model vs fine-tuned (10 test cases)
  3. Try changing epochs (2 vs 3 vs 5) → measure improvement
  4. Calculate the actual cost of this fine-tune