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="VERTEX_VIRTUAL_KEY", # Add your Vertex virtual key
    vertex_storage_bucket_name="your_bucket_name", # Specify the GCS bucket name
    provider_file_name="your_file_name.jsonl", # Specify the file name in GCS
    provider_model="gemini-1.5-flash-001" # Specify the model to fine-tune
)

# 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="VERTEX_VIRTUAL_KEY" # Add your Vertex virtual key
)

# Create a fine-tuning job
fine_tune_job = portkey.fine_tuning.jobs.create(
    model="gemini-1.5-pro-002", # Base model to fine-tune
    training_file="<file_id>", # Encoded GCS path to the training file 
    suffix="finetune_name", # Custom suffix for the fine-tuned model name
    hyperparameters={
        "n_epochs": 2
    }
)

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="VERTEX_VIRTUAL_KEY" # Add your Vertex 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="VERTEX_VIRTUAL_KEY" # Add your Vertex 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="VERTEX_VIRTUAL_KEY" # Add your Vertex 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 Google Vertex AI’s fine-tuning documentation for more information on the parameters and options available.