๐ŸŽฏ

Model Fine-tuning

๐Ÿ‘จโ€๐Ÿณ๐Ÿ‘‘ Master Chefโฑ๏ธ 60 minutes

๐Ÿ“‹ Suggested prerequisites

  • โ€ขPython
  • โ€ขOwn dataset (100+ examples)

What you'll build

Your own AI model trained with YOUR data for specific tasks in your business.

Have a support chatbot? Train it with the 100 most frequent questions and their ideal answers. The model learns your tone, your style, and responds as if it were you. Same process for ticket classification, content generation, or any repetitive task.

When finished, you'll know how to prepare a dataset in JSONL format, upload it to Google AI Studio, configure the training, and use your custom model. The model responds in your style, with your specific knowledge.


The prompt to start

Explain how to fine-tune Gemini:

  1. Prepare dataset in correct format
  2. Upload to Google AI Studio
  3. Configure training
  4. Use tuned model

Prepare dataset

# JSONL format for fine-tuning
import json

examples = [
    {
        "text_input": "How do I reset my password?",
        "output": "To reset your password: 1) Go to login 2) Click 'Forgot password' 3) Enter email 4) Check your inbox"
    },
    {
        "text_input": "I can't log in",
        "output": "Check: 1) Caps Lock off 2) Correct email 3) Try resetting password if persists"
    },
    # Minimum 100 examples recommended
]

with open("training_data.jsonl", "w") as f:
    for ex in examples:
        f.write(json.dumps(ex) + "\n")

Upload to AI Studio

  1. Go to https://aistudio.google.com
  2. Click "Tune a model"
  3. Upload your JSONL file
  4. Configure epochs (2-5 recommended)
  5. Start training

Use tuned model

import google.generativeai as genai
import os

genai.configure(api_key=os.environ["GEMINI_API_KEY"])

# Use your tuned model
model = genai.GenerativeModel(
    model_name="tunedModels/my-support-model-xxx"
)

response = model.generate_content(
    "How do I change my profile picture?"
)
print(response.text)
# โ†’ Response in your training data style

Alternative: OpenAI Fine-tuning

from openai import OpenAI
client = OpenAI()

# Upload file
file = client.files.create(
    file=open("training_data.jsonl", "rb"),
    purpose="fine-tune"
)

# Create fine-tune job
job = client.fine_tuning.jobs.create(
    training_file=file.id,
    model="gpt-4o-mini-2024-07-18"
)

# Use tuned model
response = client.chat.completions.create(
    model="ft:gpt-4o-mini:my-org::xxx",
    messages=[{"role": "user", "content": "How do I reset?"}]
)

Next level

โ†’ AI Code Review