Implementation:Run llama Llama index OpenAIFinetuneEngine Get Current Job
Overview
The get_current_job method on OpenAIFinetuneEngine retrieves the latest status of the finetuning job from the OpenAI API. It returns the full FineTuningJob object from the OpenAI Python SDK, enabling callers to check job status, access the finetuned model ID, and inspect training metadata.
Source File
- File:
llama-index-finetuning/llama_index/finetuning/openai/base.py - Lines: 94-102
- Import: Accessed as a method on
OpenAIFinetuneEngineinstances
Method Signature
def get_current_job(self) -> FineTuningJob:
"""Get current job."""
Parameters: None (instance method)
Returns: FineTuningJob -- An OpenAI FineTuningJob object (from openai.types.fine_tuning)
Raises: ValueError -- If finetune() has not been called and no start_job_id was provided to the constructor
Implementation Detail
def get_current_job(self) -> FineTuningJob:
"""Get current job."""
# validate that it works
if not self._start_job:
raise ValueError("Must call finetune() first")
# try getting id, make sure that run succeeded
job_id = self._start_job.id
return self._client.fine_tuning.jobs.retrieve(job_id)
The method performs two operations:
- Validation: Checks that
self._start_jobis populated (either fromfinetune()or from thestart_job_idconstructor parameter). If not, raises a descriptive error. - API retrieval: Extracts the job ID from the stored job object and calls
client.fine_tuning.jobs.retrieve(job_id)to get the latest state from the OpenAI API. This is important because the storedself._start_jobreflects the state at creation time, whileretrieve()returns the current state.
Return Value: FineTuningJob
The returned FineTuningJob object contains these key attributes:
| Attribute | Type | Description |
|---|---|---|
id |
str |
Unique job identifier (e.g., "ftjob-abc123")
|
status |
str |
One of: "validating_files", "queued", "running", "succeeded", "failed", "cancelled"
|
fine_tuned_model |
Optional[str] |
Model ID when status is "succeeded"; None otherwise
|
model |
str |
The base model being finetuned |
created_at |
int |
Unix timestamp of job creation |
finished_at |
Optional[int] |
Unix timestamp of job completion (if terminal) |
trained_tokens |
Optional[int] |
Total tokens processed during training |
error |
Optional[Error] |
Error details if the job failed |
Usage Example
from llama_index.finetuning import OpenAIFinetuneEngine
# After launching a finetuning job
engine = OpenAIFinetuneEngine(
base_model="gpt-3.5-turbo",
data_path="training_data.jsonl",
)
engine.finetune()
# Check status
job = engine.get_current_job()
print(f"Job ID: {job.id}")
print(f"Status: {job.status}")
print(f"Model: {job.fine_tuned_model}")
# Or reconnect to an existing job
engine = OpenAIFinetuneEngine(
base_model="gpt-3.5-turbo",
data_path="training_data.jsonl",
start_job_id="ftjob-abc123",
)
job = engine.get_current_job()
print(f"Status: {job.status}")
# Polling loop
import time
while job.status not in ("succeeded", "failed", "cancelled"):
time.sleep(60)
job = engine.get_current_job()
print(f"Status: {job.status}")