ractogateway.finetune.gemini_tuner
Google Gemini fine-tuning adapter for RactoGateway.
Workflow
Build a text-only
RactoDatasetof single-turn pairs (each example serialises totext_input/output).Call
GeminiFineTuner.run_pipeline()for a one-shot run, or call the lower-level methods:create_job()→ tuning operationwait_for_completion()→tuned_model_name
Supported base models (tuning-enabled, as of 2025)
models/gemini-1.5-flash-001-tuning(recommended)models/gemini-1.0-pro-001
Notes
The
google-generativeaiSDK supports text-only, single-turn fine-tuning via the Generative Language Tuning API.Multimodal or multi-turn training requires Google Vertex AI (
google-cloud-aiplatform). Use theto_gemini_dict()method on each example to get thecontentsformat for Vertex AI.
- class ractogateway.finetune.gemini_tuner.GeminiFineTuner(api_key=None)[source]
Bases:
objectFine-tune Google Gemini models using the Generative AI tuning API.
- Parameters:
api_key (
str|None) – Google AI API key. Falls back to theGEMINI_API_KEYenvironment variable when not supplied.
Examples
End-to-end pipeline:
from ractogateway.finetune import RactoDataset, GeminiFineTuner ds = RactoDataset.from_pairs( [("capital of France?", "Paris"), ("capital of Japan?", "Tokyo")], ) tuner = GeminiFineTuner() model_name = tuner.run_pipeline( ds, base_model="models/gemini-1.5-flash-001-tuning", display_name="geography-tutor", ) print(model_name) # "tunedModels/geography-tutor-abc123"
- create_job(dataset, base_model='models/gemini-1.5-flash-001-tuning', *, display_name='', epoch_count=5, batch_size=4, learning_rate=None)[source]
Start a Gemini supervised fine-tuning job.
- Parameters:
dataset (
RactoDataset) – Training examples. Each example must be a single-turn text pair (text_input/output). Examples with attachments or multi-turn conversations are not supported by this adapter — use Vertex AI for those.base_model (
str) – Tuning-enabled Gemini model identifier.display_name (
str) – Human-readable label for the tuned model.epoch_count (
int) – Number of training epochs.batch_size (
int) – Training batch size.learning_rate (
float|None) – Learning rate.Noneuses the provider default.
- Return type:
- Returns:
google.generativeai.types.TunedModel (operation-like object) – Pass to
wait_for_completion().- Raises:
ValueError – If the dataset fails validation, or if any examples are multimodal / multi-turn (unsupported by this adapter).
- get_model(tuned_model_name)[source]
Retrieve metadata for a tuned model.
- delete_model(tuned_model_name)[source]
Permanently delete a tuned model from your project.
- Return type:
- wait_for_completion(operation, *, poll_interval=60, verbose=True)[source]
Block until a tuning operation finishes.
- Parameters:
- Return type:
- Returns:
str – Tuned model name (e.g.
"tunedModels/my-model-abc123"). Pass directly toGoogleDeveloperKit(model=...).- Raises:
RuntimeError – If the tuning job ends in a failed state.
- run_pipeline(dataset, base_model='models/gemini-1.5-flash-001-tuning', *, display_name='', epoch_count=5, batch_size=4, learning_rate=None, poll_interval=60, verbose=True)[source]
Validate → create → wait in a single call.
- Parameters:
dataset (
RactoDataset) – Text-pair training examples.base_model (
str) – Tuning-enabled Gemini model.display_name (
str) – Human-readable label for the tuned model.epoch_count (
int) – Training epochs.batch_size (
int) – Training batch size.poll_interval (
int) – Seconds between status polls.verbose (
bool) – Print progress to stdout.
- Return type:
- Returns:
str – Tuned model name — pass to
GoogleDeveloperKit(model=...).