Book Image

OpenAI API Cookbook

By : Henry Habib
Book Image

OpenAI API Cookbook

By: Henry Habib

Overview of this book

As artificial intelligence continues to reshape industries with OpenAI at the forefront of AI research, knowing how to create innovative applications such as chatbots, virtual assistants, content generators, and productivity enhancers is a game-changer. This book takes a practical, recipe-based approach to unlocking the power of OpenAI API to build high-performance intelligent applications in diverse industries and seamlessly integrate ChatGPT in your workflows to increase productivity. You’ll begin with the OpenAI API fundamentals, covering setup, authentication, and key parameters, and quickly progress to the different elements of the OpenAI API. Once you’ve learned how to use it effectively and tweak parameters for better results, you’ll follow advanced recipes for enhancing user experience and refining outputs. The book guides your transition from development to live application deployment, setting up the API for public use and application backend. Further, you’ll discover step-by-step recipes for building knowledge-based assistants and multi-model applications tailored to your specific needs. By the end of this book, you’ll have worked through recipes involving various OpenAI API endpoints and built a variety of intelligent applications, ready to apply this experience to building AI-powered solutions of your own.
Table of Contents (10 chapters)

Using the Chat Log to modify the model’s behavior

In this recipe, we will learn how to modify the Chat Log and how it impacts the completion response that we receive from the model. This is important because developers often find this to be the best way to fine tune a model, without actually needing to create a new model. This also follows a prompt engineering must-have of providing the model with suitable examples.

How to do it…

We can add examples of prompts and responses to the Chat Log to modify the model’s behavior. Let’s observe this with the following steps:

  1. Navigate to the OpenAI Playground. If you already have messages populated, refresh the page to start afresh.
  2. In the System Message, type in the following: You are an assistant that creates marketing slogans based on descriptions of companies. Here, we are clearly instructing the model of its role and context.
  3. In the Chat Log, populate the USER message with the following: A company that makes ice cream.

    Select the Add message button located underneath the USER label to add a new message. Ensure that the label of the message says ASSISTANT. If it does not, select the label to toggle between USER and ASSISTANT.

    Now, type the following into the ASSISTANT message: Sham - the ice cream that never melts!.

  4. Select the Add message button and ensure that the label of the message says USER instead now. Type the following into the USER message: A company that produces comedy movies.
  5. Select the Add message button, and ensure that the label of the message says ASSISTANT. Type the following into the ASSISTANT message: Sham - the best way to tickle your funny bone!.
  6. Repeat steps 4-5 once more, with the following USER and ASSISTANT messages, respectively: A company that provides legal assistance to businesses, and Sham - we know business law!. At this point, you should see the following:
Figure 1.5 – The OpenAI Playground with Chat Logs populated

Figure 1.5 – The OpenAI Playground with Chat Logs populated

  1. Finally, select the Add message button, and create a USER message with the following: A company that writes engaging mystery novels.
  2. Select the Submit button on the bottom of the page.
  3. You should now see a completion response from OpenAI. In my case (Figure 1.6), the response is as follows:
    Sham – unravel the secrets with our captivating mysteries!

    Yours may be different, but the response you see will definitely start with the word “Sham –” and end with an exclamation point. In this way, we have trained the model to only give us completion responses in that format.

Figure 1.6 – The OpenAI Playground with completion, after changing the Chat Log

Figure 1.6 – The OpenAI Playground with completion, after changing the Chat Log

How it works…

As we learned in the Running a completion request in the OpenAI Playground recipe, ChatGPT and its GPT models are built on a transformer architecture, which processes input and generates responses based on the immediate chat history it has been given. It doesn’t have an ongoing memory of past interactions or a stored understanding of context outside the immediate conversation. The Chat Log has a significant impact on the model’s completions. When the model receives a prompt, it takes into account the most recent prompt, the System Message, and all the preceding messages in the Chat Log.

We can observe this in the Playground by providing our own sets of User and Assistant messages, and then see how the model changes its completion, as we did in the preceding steps.

In particular, the model has detected two patterns in the Chat Log and then generated the completion to follow that behavior:

  • The model detected that all manual Assistant completions begin with the word Sham, and so it added that prefix to its completion
  • The model identified that all slogans end with an exclamation point, and so when it generated the completion, it also added in an exclamation point

Overall, the Chat Log can be used to train the model to generate certain types of completions that the user wants to create. In addition, the Chat Log helps the model understand and maintain the context of the bigger conversation.

For example, if you added a User message with What is an airplane? and followed it up with another User message of How do they fly?, the model would understand that they refers to the airplane because of the Chat Log.

Prompt engineering

The Chat Log plays a pivotal role in influencing the model’s completions, and this observation is a glimpse into the broader realm of prompt engineering. Prompt engineering is a technique where the input or prompt given to a model is carefully crafted to guide the model towards producing a desired output.

Within the sphere of prompt engineering, there are a few notable concepts, as follows:

  • Zero-shot prompting: Here, the model is given a task that it hasn’t been explicitly trained on. It relies entirely on its pre-existing knowledge and training to generate a relevant response. In essence, it’s like asking the model to perform a task cold, without any prior examples.
  • Few-shot prompting: This involves providing the model with a small number of examples related to the desired task. The aim is to nudge the model into recognizing the pattern or context and then generating a relevant completion based on the few examples given.

Understanding these nuances in how prompts can be engineered allows users to leverage ChatGPT’s capabilities more effectively, tailoring interactions to their specific needs.

Overall, the Chat Log (and the System Message, as we learned in the earlier recipe) is a great low-touch method of aligning the completion responses from OpenAI to a desired target, without needing to fine-tune the model itself. Now that we’ve used the Playground to test prompts and completions, it’s time to use the actual OpenAI API.