Fine-Tuning AI Models with Bagel - Overview

Fine-Tuning AI Models with Bagel

In this tutorial, you'll discover how to leverage Bagel's APIs to fine-tune AI models.

Installation

To install the Bagel Python client, run the following command in your terminal:

pip install bagelML

Import the necessary modules

import os
import bagel
from getpass import getpass

This snippet imports the required modules for using Bagel.

Create the Bagel client

client = bagel.Client()

Create an instance of the Bagel client.

Step 1: Preparing Your RAW Asset

Before we can fine-tune a model, you need to have your Asset ready within Bagel. If you haven't already, use this code to create your Asset:

import os
import bagel
from getpass import getpass

# Create the asset using the API key from environment variables
client = bagel.Client()

# Copy & Paste the API Key from https://bakery.bagel.net/api-key
DEMO_KEY_IN_USE = getpass("Enter your API key: ")

# Set environment variable
api_key = os.environ['BAGEL_API_KEY'] = DEMO_KEY_IN_USE

payload = {
    "dataset_type": "input dataset type", # e.g "RAW", "VECTOR", "MODEL"
    "title": "", # e.g "Happy days"
    "category": "inpute category", # e.g "Cat1", "Cat2"
    "details": "input details", #e.g "testing"
    "tags": ["Optional, input tags"], # e.g ["AI", "DEMO", "TEST"] 
    "user_id": "input user id" # e.g "345281182308180743453"
}

# Calling the create asset method
response = client.create_asset(payload, api_key)
print(response)

dataset_type: Specifies the type of dataset (e.g., RAW, VECTOR, MODEL).

title: A string representing the name of the asset.

category: Specifies the category under which the asset falls (e.g., Cat1, Cat2).

details: Any additional information or description of the asset.

tags: Optional tags to categorize the asset (e.g., AI, DEMO).

user_id: Unique identifier of the user who owns the asset.

Note: This code prompts the user to enter their API key securely (without displaying it on the screen) and then sets it as an environment variable named BAGEL_API_KEY

In this example, we created an asset called 'Happy days'.

Create a MODEL (Can be used as Base Model)

Here we'll be creating a model that can be used for the finetuning process.

import os
import bagel
from getpass import getpass

# Create the asset using the API key from environment variables
client = bagel.Client()

# Copy & Paste the API Key from https://bakery.bagel.net/api-key
DEMO_KEY_IN_USE = getpass("Enter your API key: ")

# Set environment variable
api_key = os.environ['BAGEL_API_KEY'] = DEMO_KEY_IN_USE

payload = {
    "dataset_type": "MODEL",
    "title": "Python testing Create MODEL",
    "category": "Cat1",
    "details": "Testing",
    "tags": ["AI", "DEMO", "TEST"],
    "user_id": "101481188466180740994"
}

# Calling the create asset method
response = client.create_asset(payload, api_key)
print(response)

dataset_type is set to MODEL: Indicating that we are creating a model asset.

Includes metadata: Such as title, category, tags, and user ID.

The response will print an asset id for the Model

Purchase a Model (Base Model)

We can also purchase a Model from the Bakery Marketplace by using the following code:

import os
import bagel
from getpass import getpass

# Create the asset using the API key from environment variables
client = bagel.Client()

# Copy & Paste the API Key from https://bakery.bagel.net/api-key
DEMO_KEY_IN_USE = getpass("Enter your API key: ")

# Set environment variable
api_key = os.environ['BAGEL_API_KEY'] = DEMO_KEY_IN_USE

asset_id = "insert asset id"
user_id = "insert userid"

response = client.buy_asset(asset_id, user_id, api_key)
print(response)    

Buys a model from Bagel's marketplace: Uses the buy_asset API call.

Requires asset_id and user_id: The ID of the asset you want to purchase from the marketplace and the user making the purchase.

Prints the purchase response: Confirming that the transaction was successful.

After purchasing, the model will be available in your "My Models" page for further use, your response will generate a "Buy asset successful" prompt.

For this example, we purchased the Llama3-8b Model from the bakery marketplace using its asset id: 3323b6c4-06ef-4949-b239-1a2b220e211d

Your very won copy of the purchased model can be found on "My Models" with the tag "Copy" followed by the name of the purchase Model.

  • The Unique asset/dataset id can also be found in the URL. Allowing you to interact with the dataset/model in your local environment.

Upload file to RAW Asset

Navigate to your console and select my datasets on the left side of your page. Select the RAW asset you created and navigate to the files tab. Add a .csv, .json, .parquet, or .txt file of your choice. For this example we'll be uploading a file in parquet format.

You can run this code to upload the file from your computer:

import os
import bagel
from getpass import getpass

# Create the asset using the API key from environment variables
client = bagel.Client()

# Copy & Paste the API Key from https://bakery.bagel.net/api-key
DEMO_KEY_IN_USE = getpass("Enter your API key: ")

# Set environment variable
api_key = os.environ['BAGEL_API_KEY'] = DEMO_KEY_IN_USE

asset_id = "input asset id" # e.g "eR57yd73b-7gt4-4a71-b0fd-afKJUIY6578Ge"

file_path = "input file path" # e.g "product_catalog.csv" or "path/dir/file.csv"

# Calling the file upload method
response = client.file_upload(file_path, asset_id, api_key)
print(response)

By Navigating to the files section on our Happy days raw asset, we can find the uploaded dataset.

Step 2: Initiating the Fine-Tuning Process

With our Assets in place, we can now leverage Bagel's fine-tuning capabilities to train a custom model. The POST /api/v1/asset endpoint allows us to initiate a fine-tuning job for a pre-trained model using our Asset:

import os
import bagel
from getpass import getpass

# Create the asset using the API key from environment variables
client = bagel.Client()

# Copy & Paste the API Key from https://bakery.bagel.net/api-key
DEMO_KEY_IN_USE = getpass("Enter your API key: ")

# Set environment variable
api_key = os.environ['BAGEL_API_KEY'] = DEMO_KEY_IN_USE

user_id = "" #insert user id
asset_id = "" #insert RAW asset id containing dataset 
base_model = "" # model id of purchased base model from marketplace 
file_name = "" # name of file in raw asset 
title = ""  # choose a title 

response = client.fine_tune(title=title,
                            user_id=user_id,
                            asset_id = asset_id,
                            file_name = file_name,
                            base_model = base_model,
                            epochs = 3,
                            learning_rate = 0.01
                            )
print(response)

title: Name of the fine-tuning job.

user_id and asset_id: Identifiers of the user and the raw dataset to be used.

file_name: The dataset file uploaded to the raw asset.

base_model: The model to be fine-tuned. This will be purchased from the Bakery Marketplace.

epochs: The number of training iterations.

learning_rate: Determines how much the model adjusts per iteration.

Once the fine-tuning job is started, Bagel will return a asset_idof the fine-tuned model. We can use this to retrieve a job id which will allow us monitor the progress of the fine-tuning process.

Searching for the finetuned model on the Bakery, allows you montior the finetuning process as shown below:

Step 3: Monitoring the Fine-Tuning Job - Getting Job by Asset

import os
import bagel
from getpass import getpass

# Create the asset using the API key from environment variables
client = bagel.Client()

# Copy & Paste the API Key from https://bakery.bagel.net/api-key
DEMO_KEY_IN_USE = getpass("Enter your API key: ")

# Set environment variable
api_key = os.environ['BAGEL_API_KEY'] = DEMO_KEY_IN_USE

asset_id = "" # Replace with asset ID of fine-tuned model. e.g "eR57yd73b-7gt4-4a71-b0fd-afKJUIY6578Ge" 

# Function to get job by asset

# Call the function
response = client.get_job_by_asset_id(asset_id, api_key)
print(response)

The response contains information about the job id.

Step 4: Monitoring the Fine-Tuning Job - Get Job

import os
import bagel
from getpass import getpass

# Create the asset using the API key from environment variables
client = bagel.Client()

# Copy & Paste the API Key from https://bakery.bagel.net/api-key
DEMO_KEY_IN_USE = getpass("Enter your API key: ")

# Set environment variable
api_key = os.environ['BAGEL_API_KEY'] = DEMO_KEY_IN_USE

job_id = "" # Replace with the actual job ID. e.g "8566657552882860032" 

response = client.get_job(job_id, api_key)
print(response)

The response contains information about the job, including its status (e.g., "running", "completed", "failed") and progress (e.g., a percentage value indicating how much of the job has been completed).

You can periodically check the job status and progress until the fine-tuning process is complete.

List Jobs

You can also list all jobs using this code:

import os
import bagel
from getpass import getpass

# Create the asset using the API key from environment variables
client = bagel.Client()

# Copy & Paste the API Key from https://bakery.bagel.net/api-key
DEMO_KEY_IN_USE = getpass("Enter your API key: ")

# Set environment variable
api_key = os.environ['BAGEL_API_KEY'] = DEMO_KEY_IN_USE

user_id = "" # e.g "231243124466184567909"


# Function to list jobs created by a user

# Call the function
response = client.list_jobs(user_id, api_key)
print(response)

Step 5: Accessing the Fine-Tuned Model

Once the fine-tuning job is complete, you can download the fine-tuned model and its associated files

import os
import bagel
from getpass import getpass

# Create the asset using the API key from environment variables
client = bagel.Client()

# Copy & Paste the API Key from https://bakery.bagel.net/api-key
DEMO_KEY_IN_USE = getpass("Enter your API key: ")

# Set environment variable
api_key = os.environ['BAGEL_API_KEY'] = DEMO_KEY_IN_USE

asset_id = ""

response = client.download_model(asset_id, api_key)
print(response)

Need extra help or just want to connect with other developers? Join our community on Discord for additional support 👾

Last updated