Outline
On August 22nd, 2023, OpenAI Announced fine-tuning for GPT 3.5 Turbo! This is a huge step forward for fine-tuning, and we're excited to see what the community builds with it. (Side note, the article also mentioned fine-tuning is coming to GPT-4 this fall!)
I recently had the opportunity to give fine-tuning a try and wanted to share my experience. I'll walk through the steps I took to fine-tune GPT 3.5 Turbo to always respond with JSON output - super useful when integrating AI into tools and APIs.
What is fine-tuning?
Fine-tuning is a technique that allows you to customize a pre-trained model to better suit your needs. For example, you can fine-tune GPT-3 Turbo to always respond with a joke or a poem.
Common use case
A common use case is to replace one-shot
or few-shot
prompts with a fine-tuned model.
One-Shot
one-shot
prompts are a single prompt with an example of the desired output. i.e.Q: What is the capital of France? A: Paris Q: What is the capital of Italy? A:
Few-Shot
few-shot
prompts are a single prompt with a few examples of the desired output. i.e.Q: What is the capital of France? A: Paris Q: What is the capital of Italy? A: Rome Q: What is the capital of Germany? A: Berlin Q: What is the capital of Spain? A:
The benefit of fine-tuning is that you can use a single prompt to get the same result as a one-shot
or few-shot
prompt without having to provide examples. This lowers the token count and allows you to get more out of your API calls.
How to fine-tune GPT 3.5 Turbo
I chose to fine-tune my AI model using Python, but you could just as easily use JavaScript, or any other language that supports OpenAI's API or HTTP requests.
- Note: I am assuming you have an OpenAI account/API Key. For more information on how to get that, read the Getting Started article by OpenAI.
Step 1: Install the OpenAI Python library
The first step is to install the OpenAI Python library. You can do this by running the following command in your terminal:
pip install --upgrade openai
Step 2: Create a new file
Next, create a new file called fine-tuning.py
and add the following code which will import the OpenAI library and define some helper functions:
# Import the OpenAI library
import openai
# ==================== CONSTANTS ====================
openAI_api_key = "YOUR_API_KEY" # TODO: Replace with your API key
# ==================== FUNCTIONS ====================
"""
View the models available to your API key
"""
def view_available_models():
response = openai.Engine.list()
print(response)
"""
View the fine-tuned jobs you have created and their status
- Use this to get the ID of the job you want to view
"""
def view_fined_tuned_jobs(limit=10):
response = openai.FineTuningJob.list(limit=limit)
print(response)
"""
View the details of a fine-tuned job
"""
def view_fined_tuned_job(job_id):
response = openai.FineTuningJob.retrieve(job_id)
print(response)
"""
View the events of a fine-tuned job
- Displays the different steps your fine-tuning job has gone through
"""
def view_fined_tuned_job_events(job_id, limit=10):
response = openai.FineTuningJob.list_events(job_id, limit=limit)
print(response)
"""
View the fine-tuning training files you have uploaded to OpenAI
- Use this to get the ID of the file you want to view/tune with
"""
def view_tuning_files():
response = openai.File.list()
print(response)
"""
Upload a fine-tuning training file to OpenAI
- The file must be a .jsonl file. This means that each line of the file must be a valid JSON object.
- Response returns the new file's ID
"""
def upload_tuning_file(file_path, purpose="fine_tune"):
response = openai.File.create(file=open(file_path, 'rb'), purpose=purpose)
print(response)
Note: Remember to replace the
openAI_api_key
constant with your API key.
Step 3: Upload a fine-tuning training file
Create a fine-tuning training file
Next, we need to upload a fine-tuning training file to OpenAI. This file will be used to train our model and is composed of single-line JSON message transcripts. The file must be a .jsonl
file. This means that each line of the file must be a valid JSON object. Create a fine-tuning-json-data.jsonl
file right next to your fine-tuning.py
file and add the following lines to train our model to only respond with JSON:
{"messages": [ {"role": "user", "content": "What's the current date?"}, {"role": "assistant", "content": "{\"date\": \"8/28/2023\"}"}]}
{"messages": [ {"role": "user", "content": "List all weekdays."}, {"role": "assistant", "content": "[\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"]"}]}
{"messages": [ {"role": "user", "content": "Tell me a joke."}, {"role": "assistant", "content": "{\"joke\": \"Why don't scientists trust atoms? Because they make up everything.\"}"}]}
{"messages": [ {"role": "user", "content": "Give me 5 prime numbers."}, {"role": "assistant", "content": "[2, 3, 5, 7, 11]"}]}
{"messages": [ {"role": "user", "content": "Who are the top 3 richest people?"}, {"role": "assistant", "content": "[\"Jeff Bezos\", \"Elon Musk\", \"Bernard Arnault\"]"}, {"role": "user", "content": "Where does Elon Musk work?"}, {"role": "assistant", "content": "{\"company\": \"SpaceX and Tesla, Inc.\"}"}]}
{"messages": [ {"role": "user", "content": "Which smartphones are the most popular in 2023?"}, {"role": "assistant", "content": "[\"iPhone 13\", \"Samsung Galaxy S23\", \"Google Pixel 7\"]"}, {"role": "user", "content": "What's the price of iPhone 13?"}, {"role": "assistant", "content": "{\"price\": \"$699\"}"}]}
{"messages": [ {"role": "user", "content": "What temperature is it in Phoenix?"}, {"role": "assistant", "content": "{\"weather\": \"sunny\", \"temperature\": \"101\"}"}, {"role": "user", "content": "What temperature is it in Miami?"}, {"role": "assistant", "content": "{\"weather\": \"rainy\", \"temperature\": \"78\"}"}]}
{"messages": [ {"role": "user", "content": "What is the capital of France?"}, {"role": "assistant", "content": "{\"capital\": \"Paris\"}"}]}
{"messages": [ {"role": "user", "content": "Who won the soccer World Cup in 2022?"}, {"role": "assistant", "content": "{\"winner\": \"TBD\"}"}, {"role": "user", "content": "When and where will the next World Cup take place?"}, {"role": "assistant", "content": "{\"year\": \"2026\", \"location\": \"Canada, Mexico, United States\"}"}]}
{"messages": [ {"role": "user", "content": "What's the highest grossing movie ever?"}, {"role": "assistant", "content": "{\"movie\": \"Avengers: Endgame\", \"gross\": \"$2.798 billion\"}"}]}
{"messages": [ {"role": "user", "content": "What type of content does WebFX primarily produce?"}, {"role": "assistant", "content": "{\"content\": \"Digital Marketing Solutions and Strategies\"}"}]}
{"messages": [ {"role": "user", "content": "What is the tallest building in 2023?"}, {"role": "assistant", "content": "{\"building\": \"Abraj Al-Bait Clock Tower\", \"height\": \"601 meters\"}"}, {"role": "user", "content": "In which city is it located?"}, {"role": "assistant", "content": "{\"city\": \"Mecca\"}"}]}
{"messages": [ {"role": "user", "content": "How far is the Moon from Earth?"}, {"role": "assistant", "content": "{\"distance\": \"238,855 miles\"}"}, {"role": "user", "content": "How far is Mars from Earth?"}, {"role": "assistant", "content": "{\"average distance\": \"140 million miles\"}"}]}
{"messages": [ {"role": "user", "content": "What is the most spoken language in the world?"}, {"role": "assistant", "content": "{\"language\": \"Mandarin Chinese\"}"}]}
{"messages": [ {"role": "user", "content": "Who is the current President of the United States?"}, {"role": "assistant", "content": "{\"name\": \"TBD\"}"}]}
{"messages": [ {"role": "user", "content": "Give me a list of all the oceans on earth."}, {"role": "assistant", "content": "[\"Pacific Ocean\", \"Atlantic Ocean\", \"Indian Ocean\", \"Southern Ocean\", \"Arctic Ocean\"]"}]}
{"messages": [ {"role": "user", "content": "What is the popular music genre in 2023?"}, {"role": "assistant", "content": "{\"genre\": \"Electronic Dance Music\"}"}]}
{"messages": [ {"role": "user", "content": "Name 3 highest mountains in the world."}, {"role": "assistant", "content": "[\"Mount Everest\", \"K2\", \"Kangchenjunga\"]"}, {"role": "user", "content": "What's the height of Mount Everest?"}, {"role": "assistant", "content": "{\"height\": \"8848.86 meters\"}"}]}
{"messages": [ {"role": "user", "content": "Who invented the telephone?"}, {"role": "assistant", "content": "{\"name\": \"Alexander Graham Bell\"}"}]}
{"messages": [ {"role": "user", "content": "What does AI stand for?"}, {"role": "assistant", "content": "{\"abbreviation\": \"Artificial Intelligence\"}"}, {"role": "user", "content": "What is Artificial Intelligence?"}, {"role": "assistant", "content": "{\"definition\": \"A branch of computer science that aims to create systems capable of performing tasks that normally require human intelligence.\"}"}]}
{"messages": [ {"role": "user", "content": "What is the population of New York City?"}, {"role": "assistant", "content": "{\"population\": \"8.4 million\"}"}]}
{"messages": [ {"role": "user", "content": "Who is the author of Harry Potter books?"}, {"role": "assistant", "content": "{\"author\": \"J.K. Rowling\"}"}, {"role": "user", "content": "How many books are in the series?"}, {"role": "assistant", "content": "{\"number_of_books\": \"7\"}"}]}
{"messages": [ {"role": "user", "content": "Who painted the Mona Lisa?"}, {"role": "assistant", "content": "{\"artist\": \"Leonardo da Vinci\"}"}, {"role": "user", "content": "When was it painted?"}, {"role": "assistant", "content": "{\"year\": \"Between 1503 and 1506\"}"}]}
{"messages": [ {"role": "user", "content": "What is the lifespan of a domestic cat?"}, {"role": "assistant", "content": "{\"lifespan\": \"13-17 years\"}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object of a song with fields: title, artist, duration, and genre."}, {"role": "assistant", "content": "{\"title\": \"Shape of You\", \"artist\": \"Ed Sheeran\", \"duration\": \"3:53\", \"genre\": \"Pop\"}"}]}
{"messages": [ {"role": "user", "content": "Generate a JSON list of four primary colors."}, {"role": "assistant", "content": "[\"Red\", \"Blue\", \"Green\", \"Yellow\"]"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object for a car with fields: make, model, year, and features (which should be a list)."}, {"role": "assistant", "content": "{\"make\": \"Tesla\", \"model\": \"Model S\", \"year\": \"2023\", \"features\": [\"Autopilot\", \"Electric\", \"Ludicrous speed\"]}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON list of three largest planets in solar system."}, {"role": "assistant", "content": "[\"Jupiter\", \"Saturn\", \"Uranus\"]"}, {"role": "user", "content": "Now, create a JSON object for Jupiter with fields: diameter, moons, and discovery year."}, {"role": "assistant", "content": "{\"diameter\": \"86,881 miles\", \"moons\": \"79\", \"discovery_year\": \"7th or 8th century BC\"}"}]}
{"messages": [ {"role": "user", "content": "Generate a JSON object of a movie named 'Interstellar' with fields: director, release year, and cast (as a JSON list)."}, {"role": "assistant", "content": "{\"director\": \"Christopher Nolan\", \"release_year\": \"2014\", \"cast\": [\"Matthew McConaughey\", \"Anne Hathaway\", \"Jessica Chastain\"]}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON list of three main meals of the day."}, {"role": "assistant", "content": "[\"Breakfast\", \"Lunch\", \"Dinner\"]"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object for a book with fields: title, author, pages, and genres (as a JSON list)."}, {"role": "assistant", "content": "{\"title\": \"To Kill a Mockingbird\", \"author\": \"Harper Lee\", \"pages\": \"281\", \"genres\": [\"Southern Gothic\", \"Bildungsroman\"]}"}]}
{"messages": [ {"role": "user", "content": "Generate a JSON object of a basketball player named 'LeBron James' with fields: team, height, and awards (as a JSON list)."}, {"role": "assistant", "content": "{\"team\": \"Los Angeles Lakers\", \"height\": \"6 ft 9 in\", \"awards\": [\"4x NBA champion\", \"4x NBA Most Valuable Player (MVP)\", \"2x Olympic gold medalist\"]}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON list of the seven wonders of the ancient world."}, {"role": "assistant", "content": "[\"Great Pyramid of Giza\", \"Hanging Gardens of Babylon\", \"Statue of Zeus at Olympia\", \"Temple of Artemis at Ephesus\", \"Mausoleum at Halicarnassus\", \"Colossus of Rhodes\", \"Lighthouse of Alexandria\"]"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object for an event with fields: title, date, location, and attendees (as a JSON list)."}, {"role": "assistant", "content": "{\"title\": \"Summer Music Festival\", \"date\": \"8/30/2023\", \"location\": \"New York City\", \"attendees\": [\"John Doe\", \"Jane Smith\", \"Adam Johnson\"]}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object for a country with fields: name, capital, population, and official_languages (as a JSON list)."}, {"role": "assistant", "content": "{\"name\": \"France\", \"capital\": \"Paris\", \"population\": \"67 million\", \"official_languages\": [\"French\"]}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON list of three most populated countries in the world."}, {"role": "assistant", "content": "[\"China\", \"India\", \"United States\"]"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object for a computer with fields: brand, model, memory_size, and features (as a JSON list)."}, {"role": "assistant", "content": "{\"brand\": \"Apple\", \"model\": \"MacBook Pro\", \"memory_size\": \"16GB\", \"features\": [\"Touch Bar\", \"Thunderbolt ports\", \"Backlit Magic Keyboard\"]}"}]}
{"messages": [ {"role": "user", "content": "Generate a JSON list of four elements of the periodic table."}, {"role": "assistant", "content": "[\"Hydrogen\", \"Helium\", \"Lithium\", \"Beryllium\"]"}, {"role": "user", "content": "Now, create a JSON object for Hydrogen with fields: atomic_number, symbol, and atomic_weight."}, {"role": "assistant", "content": "{\"atomic_number\": 1, \"symbol\": \"H\", \"atomic_weight\": \"1.008\"}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object of an album named 'Abbey Road' by 'The Beatles' with fields: release_year, and tracks (as a JSON list)."}, {"role": "assistant", "content": "{\"release_year\": \"1969\", \"tracks\": [\"Come Together\", \"Something\", \"Octopus's Garden\"]}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON list of five types of data in JSON."}, {"role": "assistant", "content": "[\"String\", \"Number\", \"Object\", \"Array\", \"Boolean\"]"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object for a restaurant with fields: name, address, rating, and menu_items (as a JSON list)."}, {"role": "assistant", "content": "{\"name\": \"Joe's Pizza\", \"address\": \"3rd Street, New York\", \"rating\": \"4.5\", \"menu_items\": [\"Pepperoni Pizza\", \"Margherita Pizza\", \"Caesar Salad\"]}"}]}
{"messages": [ {"role": "user", "content": "Generate a JSON object of a football player named 'Lionel Messi' with fields: current_club, position, and awards (as a JSON list)."}, {"role": "assistant", "content": "{\"current_club\": \"Paris Saint-Germain\", \"position\": \"forward\", \"awards\": [\"6× Ballon d'Or\", \"6× European Golden Shoe\"]}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON list of the three states of matter."}, {"role": "assistant", "content": "[\"Solid\", \"Liquid\", \"Gas\"]"}]}
{"messages": [ {"role": "user", "content": "Generate a JSON object for a TV series with fields: title, seasons, and main_characters (as a JSON list)."}, {"role": "assistant", "content": "{\"title\": \"Friends\", \"seasons\": \"10\", \"main_characters\": [\"Ross Geller\", \"Rachel Green\", \"Chandler Bing\", \"Monica Geller\", \"Joey Tribbiani\", \"Phoebe Buffay\"]}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON list of three classic novels."}, {"role": "assistant", "content": "[\"To Kill a Mockingbird\", \"1984\", \"Pride and Prejudice\"]"}]}
{"messages": [ {"role": "user", "content": "Generate a JSON object of a chess piece 'Knight' with fields: movement, and can_jump_over_pieces."}, {"role": "assistant", "content": "{\"movement\": \"L-shape\", \"can_jump_over_pieces\": true}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON list of five fruits."}, {"role": "assistant", "content": "[\"Apple\", \"Banana\", \"Orange\", \"Strawberry\", \"Grapes\"]"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object for a planet 'Mercury' with fields: position_from_sun, and has_life."}, {"role": "assistant", "content": "{\"position_from_sun\": 1, \"has_life\": false}"}]}
{"messages": [ {"role": "user", "content": "Generate a JSON list of three programming languages."}, {"role": "assistant", "content": "[\"Python\", \"JavaScript\", \"Java\"]"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object for a superhero with fields: name, alias, and superpowers (as a JSON list)."}, {"role": "assistant", "content": "{\"name\": \"Bruce Wayne\", \"alias\": \"Batman\", \"superpowers\": [\"Peak Human Conditioning\", \"Master Martial Artist\", \"Master Detective\"]}"}, {"role": "user", "content": "Create another JSON object for a superhero."}, {"role": "assistant", "content": "{\"name\": \"Peter Parker\", \"alias\": \"Spider-Man\", \"superpowers\": [\"Web Slinging\", \"Wall-Crawling\", \"Spider-Sense\"]}"}, {"role": "user", "content": "Can Batman and Spider-Man have a combat? Who will win?"}, {"role": "assistant", "content": "{\"combat\": \"Yes\", \"Winner\": \"It depends on preparation and circumstances\"}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON list of three musical instruments."}, {"role": "assistant", "content": "[\"Piano\", \"Violin\", \"Guitar\"]"}, {"role": "user", "content": "Now, create a JSON object for a violin with attributes: range, tunings (as a JSON list), and origin."}, {"role": "assistant", "content": "{\"range\": \"G3 to E7\", \"tunings\": [\"G3\", \"D4\", \"A4\", \"E5\"], \"origin\": \"Italy\"}"}, {"role": "user", "content": "What's the price range of a beginner violin?"}, {"role": "assistant", "content": "{\"Price range\": \"$100 - $500\"}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object for a planet with fields: name, distance_from_sun, and moons (as a JSON list)."}, {"role": "assistant", "content": "{\"name\": \"Jupiter\", \"distance_from_sun\": \"483.8 million miles\", \"moons\": [\"Europa\", \"Ganymede\", \"Io\", \"Callisto\"]}"}, {"role": "user", "content": "Create another JSON object for a planet."}, {"role": "assistant", "content": "{\"name\": \"Mars\", \"distance_from_sun\": \"141.6 million miles\", \"moons\": [\"Phobos\", \"Deimos\"]}"}, {"role": "user", "content": "What if Jupiter and Mars compete in a race around the sun? Who will finish first?"}, {"role": "assistant", "content": "{\"race\": \"Yes\", \"Winner\": \"Mars\"}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object for a coffee with fields: type, size, and extras (as a JSON list)."}, {"role": "assistant", "content": "{\"type\": \"Latte\", \"size\": \"Grande\", \"extras\": [\"Whipped cream\", \"Caramel syrup\"]}"}, {"role": "user", "content": "Create another JSON object for a coffee."}, {"role": "assistant", "content": "{\"type\": \"Cappuccino\", \"size\": \"Venti\", \"extras\": [\"Chocolate powder\"]}"}, {"role": "user", "content": "If these two coffees compete for the best coffee, who will win?"}, {"role": "assistant", "content": "{\"competition\": \"Yes\", \"Winner\": \"It depends on personal preference\"}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object of a bird with fields: species, lifespan, and diet (as a JSON list)."}, {"role": "assistant", "content": "{\"species\": \"African Grey Parrot\", \"lifespan\": \"50 years\", \"diet\": [\"Fruits\", \"Nuts\", \"Seeds\"]}"}, {"role": "user", "content": "Create another JSON object for a bird."}, {"role": "assistant", "content": "{\"species\": \"Peregrine Falcon\", \"lifespan\": \"15 years\", \"diet\": [\"Pigeons\", \"Ducks\", \"Songbirds\"]}"}, {"role": "user", "content": "Can these two birds participate in a flying competition? Who's faster?"}, {"role": "assistant", "content": "{\"competition\": \"Yes\", \"Faster\": \"Peregrine Falcon\"}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object of a constellation with fields: name, highest_level_of_brightness, number_of_stars."}, {"role": "assistant", "content": "{\"name\": \"Orion\", \"highest_level_of_brightness\": \"-0.5 mag\", \"number_of_stars\": \"81\"}"}, {"role": "user", "content": "Create another JSON object for a constellation."}, {"role": "assistant", "content": "{\"name\": \"Ursa Major\", \"highest_level_of_brightness\": \"1.8 mag\", \"number_of_stars\": \"22\"}"}, {"role": "user", "content": "Which constellation is brighter when viewed from earth?"}, {"role": "assistant", "content": "{\"brighter_constellation\": \"Orion\"}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON list of three types of yoga."}, {"role": "assistant", "content": "[\"Hatha\", \"Ashtanga\", \"Vinyasa\"]"}, {"role": "user", "content": "Now, create a JSON object for Ashtanga with fields: intensity, pace, and is_beginner_friendly."}, {"role": "assistant", "content": "{\"intensity\": \"High\", \"pace\": \"Fast\", \"is_beginner_friendly\": false}"}, {"role": "user", "content": "What is the recommended number of weekly sessions for optimal benefits?"}, {"role": "assistant", "content": "{\"Weekly_sessions\": \"3-5\"}"}]}
{"messages": [ {"role": "user", "content": "Create a JSON object of French dish 'Ratatouille' with fields: ingredients (as a JSON list) and time_to_cook."}, {"role": "assistant", "content": "{\"ingredients\": [\"Eggplant\", \"Zucchini\", \"Bell peppers\", \"Tomatoes\"], \"time_to_cook\": \"1 hour\"}"}, {"role": "user", "content": "Create another JSON object for a French dish."}, {"role": "assistant", "content": "{\"name\": \"Coq au Vin\", \"ingredients\": [\"Chicken\", \"Red wine\", \"Garlic\", \"Mushrooms\"], \"time_to_cook\": \"2 hours\"}"}, {"role": "user", "content": "If these two dishes are presented in a cooking competition, which one would rank higher based on their cooking time?"}, {"role": "assistant", "content": "{\"Competition\": \"Yes\", \"Winner\": \"Ratatouille\"}"}]}
Upload the fine-tuning training file
Now that we have our fine-tuning training file, we can upload it to OpenAI. To do this, we'll use the upload_tuning_file
function we defined earlier. Add the following code to the bottom of your fine-tuning.py
file:
# ==================== TRAINING DATA ====================
# Upload the fine-tuning training file
upload_response = upload_tuning_file("fine-tuning-json-data.jsonl")
# Get the ID of the uploaded file
file_id = upload_response['id']
Step 4: Fine-tune the model
Create a fine-tuning job
Next, we need to create a fine-tuning job. This will allow us to fine-tune our model using the training file we just uploaded. Add the following code to the bottom of your fine-tuning.py
file:
# ==================== FINE-TUNING ====================
# Create a fine-tuning job
fine_tuning_job = openai.FineTuningJob.create(
training_file=file_id,
model='gpt-3.5-turbo',
)
# Get the ID of the fine-tuning job
job_id = fine_tuning_job['id']
View the fine-tuning job
Now that we have a fine-tuning job, we can view it to see its status. Add the following code to the bottom of your fine-tuning.py
file:
# View the fine-tuning job
view_fined_tuned_job(job_id)
Wait for the fine-tuning job to complete
All that is left to do is wait for the fine-tuning job to complete. This can take anywhere from a few minutes to a few hours depending on the size of your training file. You will receive an email containing the name of the fine-tuned model when the job is complete. Alternatively, you can keep running the view_fined_tuned_job
function to check the status of the job (easy to do if you use a Jupiter notebook).
Bringing it all together
Here is the full code for fine-tuning.py
:
# Import the OpenAI library
import openai
# ==================== CONSTANTS ====================
openAI_api_key = "YOUR_API_KEY" # TODO: Replace with your API key
# ==================== FUNCTIONS ====================
"""
View the models available to your API key
"""
def view_available_models():
response = openai.Engine.list()
print(response)
"""
View the fine-tuned jobs you have created and their status
- Use this to get the ID of the job you want to view
"""
def view_fined_tuned_jobs(limit=10):
response = openai.FineTuningJob.list(limit=limit)
print(response)
"""
View the details of a fine-tuned job
"""
def view_fined_tuned_job(job_id):
response = openai.FineTuningJob.retrieve(job_id)
print(response)
"""
View the events of a fine-tuned job
- Displays the different steps your fine-tuning job has gone through
"""
def view_fined_tuned_job_events(job_id, limit=10):
response = openai.FineTuningJob.list_events(job_id, limit=limit)
print(response)
"""
View the fine-tuning training files you have uploaded to OpenAI
- Use this to get the ID of the file you want to view/tune with
"""
def view_tuning_files():
response = openai.File.list()
print(response)
"""
Upload a fine-tuning training file to OpenAI
- The file must be a .jsonl file. This means that each line of the file must be a valid JSON object.
- Response returns the new file's ID
"""
def upload_tuning_file(file_path, purpose="fine_tune"):
response = openai.File.create(file=open(file_path, 'rb'), purpose=purpose)
print(response)
# ==================== TRAINING DATA ====================
# Upload the fine-tuning training file
upload_response = upload_tuning_file("fine-tuning-json-data.jsonl")
# Get the ID of the uploaded file
file_id = upload_response['id']
# ==================== FINE-TUNING ====================
# Create a fine-tuning job
fine_tuning_job = openai.FineTuningJob.create(
training_file=file_id,
model='gpt-3.5-turbo',
)
# Get the ID of the fine-tuning job
job_id = fine_tuning_job['id']
# View the fine-tuning job
view_fined_tuned_job(job_id)
How to use our fine-tuned GPT-3.5 Turbo model
We can use our newly fine-tuned model exactly the same as any other of OpenAI's chat models. The only difference is that we need to pass in the model name from the tuning job completion email. Here is the usage code snippet from OpenAI's Tutorial:
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
completion = openai.ChatCompletion.create(
model="ft:gpt-3.5-turbo:my-org:custom_suffix:id",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)
print(completion.choices[0].message)
If you followed along with this article, you should now have a fine-tuned model that will always respond with JSON output. You can use this model to build your own AI-powered tools and APIs!
Conclusion
Fine-tuning is a powerful technique that allows you to customize a pre-trained model to better suit your needs. I hope you found this article helpful and that it inspires you to try fine-tuning for yourself!
Comments
Login to Add comments.