ractogateway.finetune.anthropic_tuner

Anthropic Claude fine-tuning adapter for RactoGateway.

Workflow

  1. Build a RactoDataset.

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

    1. upload_dataset()file_id

    2. create_job()job_id

    3. wait_for_completion()fine_tuned_model

Supported base models (as of 2025)

  • claude-3-haiku-20240307 — primary fine-tuning target

Notes

Anthropic fine-tuning requires access approval. Verify the exact API surface against the latest Anthropic documentation before using this adapter, as the fine-tuning API may evolve.

JSONL training record format (one line per example):

{
    "system": "Optional system prompt",
    "messages": [
        {"role": "user",      "content": "…"},
        {"role": "assistant", "content": "…"}
    ]
}
class ractogateway.finetune.anthropic_tuner.AnthropicFineTuner(api_key=None)[source]

Bases: object

Fine-tune Anthropic Claude models using the fine-tuning API.

Parameters:

api_key (str | None) – Anthropic API key. Falls back to the ANTHROPIC_API_KEY environment variable when not supplied.

Examples

End-to-end pipeline:

from ractogateway.finetune import RactoDataset, AnthropicFineTuner

ds = RactoDataset.from_pairs(
    [("Summarise this: …", "The text discusses…")],
    system="You are a concise summariser.",
)
tuner = AnthropicFineTuner()
model = tuner.run_pipeline(ds, model="claude-3-haiku-20240307")
print(model)   # "claude-3-haiku-20240307:ft:org-xxx:suffix:abc123"
upload_dataset(dataset)[source]

Upload dataset as an Anthropic training file.

Parameters:

dataset (RactoDataset) – The training examples to upload.

Return type:

str

Returns:

str – The Anthropic file ID used in create_job().

create_job(training_file, model='claude-3-haiku-20240307', *, validation_file=None, suffix=None, hyperparameters=None)[source]

Submit a fine-tuning job.

Parameters:
  • training_file (str) – File ID returned by upload_dataset().

  • model (str) – Base Claude model to fine-tune.

  • validation_file (str | None) – Optional validation file ID.

  • suffix (str | None) – Short label appended to the fine-tuned model name.

  • hyperparameters (dict[str, Any] | None) – Optional overrides, e.g. {"n_epochs": 3}.

Return type:

str

Returns:

str – The fine-tuning job ID.

get_status(job_id)[source]

Retrieve the current status of a fine-tuning job.

Return type:

dict[str, Any]

Returns:

dict – Keys: id, status, model, fine_tuned_model, created_at, finished_at, error.

list_jobs(limit=10)[source]

Return the most recent fine-tuning jobs (newest first).

Return type:

list[dict[str, Any]]

cancel_job(job_id)[source]

Cancel a running fine-tuning job.

Return type:

dict[str, Any]

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

Block until a fine-tuning job finishes.

Parameters:
  • job_id (str) – The job ID returned by create_job().

  • poll_interval (int) – Seconds between status-check API calls.

  • verbose (bool) – Print status lines to stdout.

Return type:

str

Returns:

str – Fine-tuned model name — pass directly to AnthropicDeveloperKit(model=...).

Raises:

RuntimeError – If the job ends in "failed" or "cancelled" state.

run_pipeline(dataset, model='claude-3-haiku-20240307', *, validation_dataset=None, suffix=None, hyperparameters=None, poll_interval=60, verbose=True)[source]

Validate → upload → train → wait in a single call.

Parameters:
  • dataset (RactoDataset) – Training examples.

  • model (str) – Base Claude model to fine-tune.

  • validation_dataset (RactoDataset | None) – Optional held-out validation set.

  • suffix (str | None) – Short label appended to the fine-tuned model name.

  • hyperparameters (dict[str, Any] | None) – Optional overrides, e.g. {"n_epochs": 3}.

  • poll_interval (int) – Seconds between status polls.

  • verbose (bool) – Print progress to stdout.

Return type:

str

Returns:

str – Fine-tuned model identifier — pass directly to AnthropicDeveloperKit(model=...).

Raises: