Unleash the Power of GPT: A Quick Guide to OpenAI API Fine-Tuning

Hey, fellow devs! 👋 It’s been a while since I graced your screens with some tech wisdom, but fear not, because today we’re diving into the realms of artificial intelligence with the finesse of a seasoned developer.

So, you’ve heard about GPT-3.5 Turbo, the brainchild of OpenAI, and you’re itching to tap into its infinite potential. Well, strap in, because I’m about to guide you through the labyrinth of fine-tuning, subscription payments, and all that jazz in the shortest, most entertaining way possible.

Explaining Fine-tuning to a kid

Let’s break down fine-tuning in a way even a kid could understand:

Alright, imagine you have a magic-talking robot friend named Bob. Bob is smart but he doesn’t know everything in the world yet. He’s like a blank slate, waiting to learn new things.

Now, fine-tuning is like teaching Bob something new. Let’s say you want Bob to be good at telling bedtime stories. You start by giving him many bedtime stories to read and learn from. This is like uploading data to OpenAI.

After Bob has read all those stories, he’s getting better, but he’s still not perfect at telling stories. That’s where fine-tuning comes in. It’s like giving Bob extra practice with bedtime stories until he gets good at it.

So, you keep asking Bob to tell you bedtime stories, and each time he does, he gets a little better because of the fine-tuning. Eventually, he becomes so good at it that he can tell the most amazing and creative stories ever!

And that’s how fine-tuning helps make Bob, or in our case, an AI model, better at what it does. It’s like giving it extra practice so it can do its job even better!

Quick Summary

In the upcoming process, I will outline the steps involved in training and constructing a personalized model tailored to my needs.

+-------------------------------------------+
|                                           |
|       Prepare JSON Data File              |
|                                           |
+-----------------------+-------------------+
                        |
                        v
+-------------------------------------------+
|                                           |
|      Upload File to OpenAI Platform       |
|                                           |
+-----------------------+-------------------+
                        |
                        v
+-----------------------+-------------------+
|                                           |
|       Create Fine-Tuning Job              |
|                                           |
+-----------------------+-------------------+
                        |
              Fine-Tune Model Name
                        |
                        v
+-------------------------------------------+
|                                           |
|   Utilize Fine-Tuned Model (API Call)     |
|                                           |
+-------------------------------------------+

Steps

Now, let’s breeze through the steps involved in this fine-tuning process:

Step 1: Secure Your Access with Payment
Before we embark on this adventure, let’s ensure you have access to the OpenAI API. Head over to the OpenAI Pricing page and choose a subscription plan that suits your needs. Once you’ve subscribed (starting at just $10!), you’ll receive your API key, which is your golden ticket to AI wonderland. Guard it with your life!

Step 2: Obtain Your API Token
Now that you’re subscribed, let’s grab that API token. Log in to your OpenAI account and navigate to the API section. You’ll find your API token listed there. Copy it to your clipboard, and let’s move on to the next step.

Step 3: Prep Your Playground with Postman
I used Postman to interact with the OpenAI API. If you haven’t already, download and install Postman for your platform. Once installed, create a new request and set up the necessary headers and parameters to make API calls. You can use curl as well

Step 4: Upload Your Data (cURL Example)
Before we dive into fine-tuning, you’ll need some data to train your model on. Prepare your dataset, ensuring it follows the format provided below, and upload it to the platform using the following cURL command:

curl https://api.openai.com/v1/files \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Organization: YOUR_ORGANIZATION_ID" \
-F purpose="fine-tune" \
-F file="@mydata.jsonl"

Example mydata.jsonl:

{"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "What's the capital of France?"}, {"role": "assistant", "content": "Paris, as if everyone doesn't know that already."}]}
{"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "Who wrote 'Romeo and Juliet'?"}, {"role": "assistant", "content": "Oh, just some guy named William Shakespeare. Ever heard of him?"}]}
{"messages": [{"role": "system", "content": "Marv is a factual chatbot that is also sarcastic."}, {"role": "user", "content": "How far is the Moon from Earth?"}, {"role": "assistant", "content": "Around 384,400 kilometers. Give or take a few, like that really matters."}]}

You need minimum 10 examples in the file to create the fine-tuning job

Step 5: Get the file name

Once the file is uploaded you can use the file to train a fine-tuning model. Now you need to get the file id

curl https://api.openai.com/v1/files \
  -H "Authorization: Bearer $OPENAI_API_KEY"
{
  "data": [
    {
      "id": "file-abc123",
      "object": "file",
      "bytes": 175,
      "created_at": 1613677385,
      "filename": "salesOverview.pdf",
      "purpose": "assistants",
    },
    {
      "id": "file-abc123",
      "object": "file",
      "bytes": 140,
      "created_at": 1613779121,
      "filename": "puppy.jsonl",
      "purpose": "fine-tune",
    }
  ],
  "object": "list"
}

Step 6: Create a Fine-Tuning Job (cURL Example)

To create a fine-tuning job, use the following cURL command:

curl https://api.openai.com/v1/fine_tuning/jobs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Organization: YOUR_ORGANIZATION_ID" \
  -d '{
    "training_file": "file-abc123",
    "model": "gpt-3.5-turbo"
  }'

You need to use the uploaded JSON file id as the training_file.

Example Response:

{
  "object": "fine_tuning.job",
  "id": "ftjob-abc123",
  "model": "gpt-3.5-turbo-0125",
  "created_at": 1614807352,
  "fine_tuned_model": null,
  "organization_id": "org-123",
  "result_files": [],
  "status": "queued",
  "validation_file": null,
  "training_file": "file-abc123"
}

Step 7: Get the fine-tuning id

List your organization’s fine-tuning jobs. Example request

curl https://api.openai.com/v1/fine_tuning/jobs?limit=2 \
  -H "Authorization: Bearer $OPENAI_API_KEY"

Example response

{
  "object": "list",
  "data": [
    {
      "object": "fine_tuning.job.event",
      "id": "ft-event-TjX0lMfOniCZX64t9PUQT5hn",
      "created_at": 1689813489,
      "level": "warn",
      "message": "Fine tuning process stopping due to job cancellation",
      "data": null,
      "type": "message"
    },
    { ... },
    { ... }
  ], "has_more": true
}

Step 8: Utilize the Fine-Tuned Model
Once the fine-tuning job is complete, it’s time to put your model to the test. Use the API to call chat completion or any other functionality you desire, and marvel at the results.

curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Organization: YOUR_ORGANIZATION_ID" \
-d '{"model": "finetune", "prompt": "Once upon a time", "temperature": 0.7, "max_tokens": 50}' \ 
https://api.openai.com/v1/engines/davinci-codex/completions

Example response

{
  "choices": [
    {
      "finish_reason": "length",
      "index": 0,
      "logprobs": null,
      "text": " there was a magical unicorn named Sparkle who lived in the enchanted forest."
    }
  ],
  "model": "finetune",
  "object": "text_completion"
}

Share the Love

Once you’ve conquered the AI realm, don’t keep all that knowledge to yourself! Spread the word, share your insights, and let’s revolutionize the world, one line of code at a time.

And there you have it, folks! A whirlwind tour of fine-tuning with OpenAI API v1, sprinkled with a dash of humour and a dollop of wisdom. Now go forth, my fellow devs, and unleash the full potential of GPT-3.5 Turbo upon the world!

Until next time, happy coding! 🚀

References

I would like to hear your thoughts