ractogateway.finetune.gemini_tuner

Google Gemini fine-tuning adapter for RactoGateway.

Workflow

  1. Build a text-only RactoDataset of single-turn pairs (each example serialises to text_input / output).

  2. Call GeminiFineTuner.run_pipeline() for a one-shot run, or call the lower-level methods:

    1. create_job() → tuning operation

    2. wait_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-generativeai SDK 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 the to_gemini_dict() method on each example to get the contents format for Vertex AI.

class ractogateway.finetune.gemini_tuner.GeminiFineTuner(api_key=None)[source]

Bases: object

Fine-tune Google Gemini models using the Generative AI tuning API.

Parameters:

api_key (str | None) – Google AI API key. Falls back to the GEMINI_API_KEY environment 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. None uses the provider default.

Return type:

Any

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.

Parameters:

tuned_model_name (str) – Full tuned model name, e.g. "tunedModels/my-model-abc123".

Return type:

dict[str, Any]

Returns:

dict – Keys: name, display_name, state, base_model.

list_models()[source]

List all tuned models in this project.

Return type:

list[dict[str, Any]]

delete_model(tuned_model_name)[source]

Permanently delete a tuned model from your project.

Return type:

None

wait_for_completion(operation, *, poll_interval=60, verbose=True)[source]

Block until a tuning operation finishes.

Parameters:
  • operation (Any) – The object returned by create_job().

  • poll_interval (int) – Seconds between metadata checks.

  • verbose (bool) – Print progress to stdout.

Return type:

str

Returns:

str – Tuned model name (e.g. "tunedModels/my-model-abc123"). Pass directly to GoogleDeveloperKit(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.

  • learning_rate (float | None) – Learning rate override.

  • poll_interval (int) – Seconds between status polls.

  • verbose (bool) – Print progress to stdout.

Return type:

str

Returns:

str – Tuned model name — pass to GoogleDeveloperKit(model=...).