top of page
Search

Fine tuning OpenAI model with your own data

  • Writer: Latera Tesfaye
    Latera Tesfaye
  • Jan 4, 2024
  • 7 min read

Fine-tuning OpenAI models with your own data is a targeted strategy to enhance the model's performance for specific tasks or industries. OpenAI offers a range of powerful, pre-trained models that have been developed using extensive and diverse data sets, enabling them to understand and generate human-like text. However, each business, project, or research initiative often has unique requirements and nuances. By fine-tuning these models with your specialized data, you can create a customized AI tool that aligns more closely with your specific objectives.


This introduction will delve into the specifics of fine-tuning OpenAI models, discussing the reasons for choosing this approach, the process involved, and the potential benefits and considerations. The focus will be on how you can leverage OpenAI's advanced capabilities and adapt them to your unique context, enhancing the model's accuracy, efficiency, and relevance to your particular tasks and challenges. Whether you're aiming to improve customer interactions, accelerate research, or enhance decision-making, fine-tuning OpenAI models is a crucial step toward more personalized and effective AI solutions.


In this blog we are going to go through all the necessary steps required to fine-tune an Open-AI model and deploy it for wider communication. As an example, I will fine-tune OpenAI model based on an abstract content of paper I recently wrote. The idea is to customize a chatbot to answer specific question about my paper based the abstract. I have outlined and described all the steps as follows:


Step 1: Getting your OpenAI API key

Create an OpenAI account if you haven't already. Then, navigate to the API Keys page at https://platform.openai.com/api-keys. Click on the "Create new secret key" button. In the pop-up option, enter your key name. Once you've named your key and pressed enter, your API key will be displayed. Make sure to copy and save it somewhere safe, especially if you're prone to forgetfulness like me. If you miss copying the API key in the first pop-up, you'll need to create a new secret key — and this time, don't forget to copy it. Note that the OpenAI APIs require payment per token. You can find the pricing information here: https://openai.com/pricing. As of the writing of this blog, fine-tuning was applicable for GPT-3.5 Turbo, which costs 0.001 USD per 1K tokens and 0.002 USD per 1K tokens for input and output respectively. And one more thing, DO NOT share your API key with anyone else. Having access to this key means they could potentially make API requests on your behalf — it's like giving someone the password to your diary but with potentially more expensive consequences. So, keep it secret, keep it safe, or else you might find your AI spilling more than just witty jokes!

Step 2: Connecting with OpenAI model

First thing first, install OpenAI package.


Then create a client object to OpenAI API using your API key.



Step 3: Creating training data

There are specific rules that need to be followed before fine-tuning the model. More information is provided in the OpenAI documentation here: https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset. As our training data, we will create a JSONL data file with the following format. The JSONL message format has 4 key components. The first one is 'role', which is defined as system, user, and assistant. The 'system' role is designated for content where we specify the role of the model. For example, in the following scenario, the content for the 'system' role is, 'You are a helpful, creative, and exceptional chatbot to answer questions about a paper titled, "Mapping Immunization Landscape Across 982 Ethiopian Districts."' This defines the function of the chatbot. The beauty of OpenAI is that you can define whatever content you envision the system (or models) to do. I love how this is a very high-level or human-like prompt. The 'user' role specifies the expectation of the users' question in the chatbot. For example, I used questions like 'What methods were used in this study?' in the content section of this role. Finally, the 'assistant' role defines content as the solution to the proposed question. This is what the model is trained on. For example, for the above question, 'The methodology involved extracting data on fully immunized children under one year from the District Health Information System (DHIS) for the years 2021/2022, alongside incorporating various denominator sources like Worldpop and adjustments based on the Ethiopian Demographic and Health Survey (EDHS) from 2016.' To train the model well, I provide various paraphrased versions of the above prompt.



The abstract consists of five main parts. The first is the 'objective,' providing a succinct description of the paper's purpose. The second is 'data sources,' outlining or listing all the data sources utilized. The third is 'method,' detailing the models and steps implemented to achieve the results. The fourth is the 'result section,' highlighting key findings from the paper. Finally, the 'conclusion and recommendation' provides an interpretation of the results and their implications for policy. OpenAI recommends having 10 to 20 training data examples. In our case, this means 10 - 20 different representations/writings of each section. For instance, for content in the user system that asks, 'What is the objective of this paper?' I provided 20 different ways the objective of the paper can be articulated. It is advised to divide the data into training and validation sets; however, in this case, we are using a single dataset. For more information, check OpenAI's fine-tuning documentation. The full dataset is available here.


Step 4: Defining purpose and creating training object


In the above code we defined the purpose of the work, as fine-tune. Finally, tr.id will give us our training object id.


Step 5: Submitting fine-tuning job


The suffix name can be any descriptive term related to your project. In this instance, we provided the training ID from the previous step and also defined the OpenAI model that we want to fine-tune as 'gpt-3.5-turbo-1106'. This was the latest available version for tuning at the time of writing this blog. The code mentioned above submits a fine-tuning job and also provides us with a job ID to track progress.


Step 6: Tracking the progress of the submitted job

To track the submitted job, you can navigate to the fine-tune section in your account to see the progress of all submitted jobs. The interface provides a comprehensive overview of the job. Alternatively, in Python, you can use the following command to check the status.



In the output, look for 'fine_tuned_model'. If this is empty, it likely means that the job is still running; this process might take up to 10 minutes. If, after a while, 'fine_tuned_model' is still empty or 'None', and 'status' also says 'Failed', it indicates that the job has failed. In such cases, check the interface on the OpenAI website for more information and try to debug your code. Carefully verify that the data format is correct. If completed successfully, 'fine_tuned_model' will provide you with an ID. We will use this ID later.


Step 7: Testing the new fine tuned model


Here, the ID refers to the fine-tuned model ID obtained from step 5. Then, we create a prompt that a user might ask about the abstract. This could be anything, and based on its training data, it will provide answers, similar to ChatGPT. For instance, for the prompt provided, it answered, 'For the study of childhood immunization in Ethiopia, the DHIS2 platform for data collection and analysis in 2021 was used.', which was accurate based on the training data provided. The beauty of fine-tuning is that, for example, the user can simply ask, 'What is the objective of this study?' and the response will be based on the training data provided. Generally, for questions or inputs from the users that are related to the training data, the model adheres to the data. However, if someone asked what is the distance between Earth and the Moon, the model will rely on the general data it was trained on from OpenAI. Now, let's do something cool. Let's build a web app with an interface where users can interact with the model, just like ChatGPT. We will provide areas where users can ask questions and a separate area where prompts are provided.


Step 8: Deploying the model

In the development of our web app, we will need the Flask Python web application development tool (this helps us to call and run the model within a web service), HTML, CSS, and a deployment server.



The above code goes to app.py of our Flask framework.


Looking at the above code:

  • Flask: A micro web framework for Python, used to create web applications.

  • request: An object to handle incoming requests to the server.

  • jsonify: A function to turn the response data into a JSON response.

  • render_template: A utility to render an HTML template.

openai.api_key: This sets your OpenAI API key, which is essential for authenticating requests to the OpenAI API. (Note: API keys should be kept secret and not hard-coded into scripts.)


Route definitions:

  • @app.route('/'): The root route ('/') of the web server. When visited, it renders and returns an HTML page (index.html).

  • @app.route('/ask', methods=['POST']): Defines a route (/ask) that accepts POST requests. This is where the interaction with the OpenAI API takes place.

  • def index(): The function that's called when someone visits the root route. It renders index.html, which would be your main page.

Handling the ask route:

  • def ask(): The function that's called when someone sends a POST request to /ask. It's meant to handle user queries and interact with the OpenAI API.

  • data = request.json: Extracts the JSON data from the incoming request. This is expected to contain the user's query.

  • user_query = data['query']: Extracts the user's query from the data.

  • response = openai.ChatCompletion.create(...): Calls the OpenAI API, specifically the ChatCompletion endpoint, to get a response for the user's query. The message to the API includes a predefined role and content, alongside the user's query.

  • return jsonify(...): The API's response is returned as a JSON object to whoever made the POST request.


Now lets create template/index.html file.


I will not delve into the details of the above code, but the basic idea is that this code will create an interface that enables users to input their own questions, to which it then provides answers. The interface looks something like this:


screen shot of the page
Screen shot of the page

Not super complex or ugly.


To deploy this Flask app, we will use https://www.pythonanywhere.com/, a free hosting platform for Python-based web apps. All you have to do is upload your app.py and templates/index.html. Here is the link to the deployed app of the example we've been discussing: http://latera.pythonanywhere.com/. Keep in mind that each request from users is considered a token, and it will cost you when people use it. For this link, I have limited this specific OpenAI usage to only $6 USD, after which the model becomes unresponsive.


I hope this was helpful :)

Comentarios

Obtuvo 0 de 5 estrellas.
Aún no hay calificaciones

Agrega una calificación
Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page