Upload a file

Please follow to the bedrock file upload guide for more details.

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
)

fine_tune_job = portkey.fine_tuning.jobs.create(
    training_file="file_id", # encoded s3 file URI of the training data.
    model="model_id", # ex: modelId from bedrock for fine-tuning
    hyperparameters={
    "n_epochs": 1
    },
    role_arn="role_arn", # service role arn for bedrock job to assume when running.
    job_name="job_name", # name for the job, optional will created random if not provided.
    validation_file="file_id", # optional, must be encoded s3 file URI.
    suffix="finetuned_model_name",
    model_type="text" # optional, chat or text.
  )

print(fine_tune_job)

Notes:

  • Bedrock fine-tuning dataset format is a little bit different from OpenAI’s fine-tuning dataset format.
  • model_type field is required for the dataset transformation, currently gateway does the following dataset transformation:
    • chat -> text-to-text
    • chat -> chat.
  • model param should be the ModelID that is required for fine-tuning not for the inference. ModelID is different for inference and fine-tuning.

List of supported finetune models and their IDs are available at Bedrock documentation

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)

Retrieve 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="job_id"  # The ID of the fine-tuning job to retrieve
)

print(job)

Cancel 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="job_id"  # The ID of the fine-tuning job to cancel
)

print(cancelled_job)

References

  • Fine-tune Support types for models: Link
  • Fine-tuning Documentation: Link