ractogateway.finetune.anthropic_tuner
Anthropic Claude fine-tuning adapter for RactoGateway.
Workflow
Build a
RactoDataset.Call
AnthropicFineTuner.run_pipeline()for a one-shot end-to-end run, or call the lower-level methods:upload_dataset()→file_idcreate_job()→job_idwait_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:
objectFine-tune Anthropic Claude models using the fine-tuning API.
- Parameters:
api_key (
str|None) – Anthropic API key. Falls back to theANTHROPIC_API_KEYenvironment 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:
- 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:
- Return type:
- Returns:
str – The fine-tuning job ID.
- get_status(job_id)[source]
Retrieve the current status of a fine-tuning job.
- list_jobs(limit=10)[source]
Return the most recent fine-tuning jobs (newest first).
- wait_for_completion(job_id, *, poll_interval=60, verbose=True)[source]
Block until a fine-tuning job finishes.
- Parameters:
- Return type:
- 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:
- Returns:
str – Fine-tuned model identifier — pass directly to
AnthropicDeveloperKit(model=...).- Raises:
ValueError – If dataset validation fails.
RuntimeError – If the fine-tuning job fails remotely.