Fine-Tuning AI Models

Fine-Tuning AI Models

Understanding AI Models and Fine-Tuning

Artificial Intelligence

AI (Artificial Intelligence) focuses on the creation and development of intelligent machines that can perform tasks that would require human intelligence. AI has numerous applications across various fields, including healthcare, finance, transportation, entertainment, and more. It has the potential to automate repetitive tasks, enhance decision-making processes, improve efficiency, and lead to advancements in various industries.

AI Models

AI Models are the implementation of AI Algorithms, there are many algorithms for various use cases designed to mimic human intelligence and make predictions, generate text, recognize patterns, or solve complex problems. AI Models are evolving, eg. Deep Learning Models, Generative Adversarial Networks, Transformer Models, etc., along with underlying hardware architecture taking the usability of these models to the next level.

OpenAI Models

OpenAI's GPT (generative pre-trained transformer) models have been trained on natural language and code from the open internet. GPTs provide text outputs in response to their inputs. The inputs to GPTs are also referred to as "prompts" and the responses from GPT models as "completions". Few models like gpt-4, and gpt-3.5-turbo are available only via Chat API endpoints and a few older models like davinci, curie, babbage, ada is available for completion and fine-tuning.

Fine Tuning

Fine-tuning refers to the process of customizing a pre-trained language model, such as GPT-3, to better suit specific tasks or domains. We give the model capability to recognize new patterns and how to behave on them. OpenAI provides fine-tuning APIs that allow developers to build on top of their existing language models. The fine-tuning process involves taking a pre-trained language model and further training it on a more specific dataset or task. This dataset typically contains examples relevant to the target task. By fine-tuning, the model can adapt its learned representations to better understand and generate output specific to the given task.

Fine-Tuning in Action

Objective

Let's fine-tune OpenAI's davinci model to identify Indian Festival based on its description (a classification use case). The prompt will be a short description of an Indian Festival and the model will respond to us with the festival name as its completion. We will fine-tune the model to identify below mentioned Indian Festivals: Diwali, Holi, Janmashtami, Navratri, Ganesh Chaturthi, and Ram Navami

Data Collection

We have prepared training data using ChatGPT itself. We collected around 20 sample descriptions for each selected festival and created a JSONL file of this training data which will be used for fine-tuning the model. A few samples are mentioned below:

{"prompt":"A vibrant festival celebrated across India, marking the victory of good over evil. It involves lighting oil lamps, exchanging gifts, and bursting fireworks.","completion":" Diwali"}
{"prompt":"A time for celebration, where people decorate their homes with colorful rangoli patterns and light traditional oil lamps. Sweets and prayers are an integral part of this festival.","completion":" Diwali"}
{"prompt":"A major Hindu festival celebrated with great enthusiasm. It involves the lighting of lamps, performing religious rituals, and enjoying firework displays.","completion":" Diwali"}
{"prompt":"This festival is known for its exuberant display of colors. People apply colored powders on each other's faces, sing and dance to traditional songs, and relish festive delicacies.","completion":" Holi"}
{"prompt":"A vibrant celebration of love and friendship. People throw and apply colors to express joy and unity. It is a day of laughter, music, and bonding with friends and family.","completion":" Holi"}
{"prompt":"A sacred festival that honors Lord Krishna's birth. Devotees engage in religious ceremonies, sing bhajans (devotional songs), and break their fast at midnight. Temples are beautifully adorned, and the atmosphere is filled with devotion and spiritual fervor.","completion":" Janmashtami"}
{"prompt":"During this festival, devotees celebrate the birth of Lord Krishna with great devotion. They perform puja (ritual worship), engage in kirtan (devotional singing), and prepare elaborate feasts as offerings to the deity.","completion":" Janmashtami"}
{"prompt":"A nine-night festival filled with vibrant music, dance, and devotion. People participate in energetic Garba and Dandiya Raas dances, twirling to the beats of traditional music. It is a time of celebration, cultural unity, and paying homage to the divine feminine energy.","completion":" Navratri"}
{"prompt":"During this festive occasion, people come together to celebrate with Garba and Dandiya dances. Colorful attire, rhythmic music, and intricately designed dance moves create a lively atmosphere. It is a time of joy, community bonding, and reverence for the goddesses worshipped during Navratri.","completion":" Navratri"}
{"prompt":"A joyous occasion that commemorates the birth of Lord Ganesha. Pandals are set up in various neighborhoods, where intricately designed idols of Ganesha are placed. Devotees gather to seek his blessings, perform aarti, and immerse the idols in water at the end of the festival.","completion":" Ganesh Chaturthi"}
{"prompt":"This festival, public celebration initiated by Lokmanya Tilak, holds immense cultural significance. Elaborate pandals with beautifully crafted idols of Lord Ganesha are installed across the city. The air is filled with devotion as people offer prayers, chant hymns, and participate in processions.","completion":" Ganesh Chaturthi"}
{"prompt":"A festival that commemorates the birth of Lord Ram, is celebrated with great enthusiasm. Devotees gather at temples, recite scriptures, and sing bhajans.","completion":" Ram Navami"}
{"prompt":"The auspicious festival is observed on the Navami Tithi of Shukla Paksha in the month of Chaitra, honors the birth of Lord Ram. Devotees offer prayers, sing bhajans, and organize processions.","completion":" Ram Navami"}

Selecting the Pre-trained Model

As mentioned earlier, as of now only older GPT3 models are available for fine-tuning. We are selecting the most powerful model among them, i.e. davinci. The "Davinci" model is optimized for performance on a wide range of language tasks. It has a large number of parameters, which allows it to generate high-quality and contextually relevant responses to a wide variety of prompts.

Fine-tuning the Model

  • Setup

    • OpenAI command-line interface (CLI)
      pip install --upgrade openai

    • Set your OPENAI_API_KEY environment variable
      export OPENAI_API_KEY="<OPENAI_API_KEY>"

  • Prepare data

    Use OpenAI's CLI tool which validates, gives suggestions and reformats your training data

      openai tools fine_tunes.prepare_data -f <LOCAL_FILE>
    

  • Create fine-tune model

      openai api fine_tunes.create -t <TRAIN_FILE_ID_OR_PATH> -m <BASE_MODEL>
    

    Where BASE_MODEL is the name of the base model you're starting from (ada, babbage, curie, or davinci) For our use case, as mentioned earlier, we will use davinci.

  • Check job status
    We can use the below command to check the status of model creation, it displays the model name once the model is created

      openai api fine_tunes.follow -i <YOUR_FINE_TUNE_JOB_ID>
    

  • Trying out the fine-tuned model
    We can use OpenAI CLI to get the response

      openai api completions.create -m <FINE_TUNED_MODEL> -p <YOUR_PROMPT>
    

    But, I observed that this command doesn't behave correctly for me. So, let's use Python instead.

      import openai
      response = openai.Completion.create(
          model=FINE_TUNED_MODEL,
          prompt=YOUR_PROMPT,
          max_tokens=150,
          temperature=1,
          stop=["***"]
      )
      print(response.choices[0].text.strip())
    

    Below is how my models responded to a few prompts and identified the festivals correctly:

I hope this gave you a basic idea of how can we fine-tune a pre-trained AI model for a specific use case.

Happy Learning!