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:
Import the necessary modules
This snippet imports the required modules for using Bagel.
Create the 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:
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.
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:
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:
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:
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_id
of 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
The response contains information about the job id.
Step 4: Monitoring the Fine-Tuning Job - Get Job
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:
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
Need extra help or just want to connect with other developers? Join our community on Discord for additional support 👾
Last updated