Upload a file

from portkey_ai import Portkey

# Initialize the Portkey client
portkey = Portkey(
    api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
    virtual_key="VIRTUAL_KEY" # Add your provider's virtual key
)

# Upload a file for fine-tuning
file = portkey.files.create(
    file=open("dataset.jsonl", "rb"),
    purpose="fine-tune"
)

print(file)

Create a fine-tuning job

from portkey_ai import Portkey

# Initialize the Portkey client
portkey = Portkey(
    api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
    virtual_key="VIRTUAL_KEY" # Add your provider's virtual key
)

# Create a fine-tuning job
fine_tune_job = portkey.fine_tuning.jobs.create(
    model="gpt-3.5-turbo", # Base model to fine-tune
    training_file="file_id", # ID of the uploaded training file
    validation_file="file_id", # Optional: ID of the uploaded validation file
    suffix="finetune_name", # Custom suffix for the fine-tuned model name
    hyperparameters={
        "n_epochs": 1
    }
)

print(fine_tune_job)

List fine-tuning jobs

from portkey_ai import Portkey

# Initialize the Portkey client
portkey = Portkey(
    api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
    virtual_key="VIRTUAL_KEY" # Add your provider's virtual key
)

# List all fine-tuning jobs
jobs = portkey.fine_tuning.jobs.list(
    limit=10  # Optional: Number of jobs to retrieve (default: 20)
)

print(jobs)

Get a fine-tuning job

from portkey_ai import Portkey

# Initialize the Portkey client
portkey = Portkey(
    api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
    virtual_key="VIRTUAL_KEY" # Add your provider's virtual key
)

# Retrieve a specific fine-tuning job
job = portkey.fine_tuning.jobs.retrieve(
    "job_id"  # The ID of the fine-tuning job to retrieve
)

print(job)

Cancel a fine-tuning job

from portkey_ai import Portkey

# Initialize the Portkey client
portkey = Portkey(
    api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
    virtual_key="VIRTUAL_KEY" # Add your provider's virtual key
)

# Cancel a fine-tuning job
cancelled_job = portkey.fine_tuning.jobs.cancel(
    "job_id"  # The ID of the fine-tuning job to cancel
)

print(cancelled_job)

Refer to OpenAI’s fine-tuning documentation for more information on the parameters and options available.

Was this page helpful?