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 System Message in the OpenAI Playground

In this recipe, we will observe how modifying the System Message affects the completion response that we receive from the model. This is important because as you begin to use the OpenAI API, you will likely adjust and refine the System Message to your specific needs, and the Playground is a great way to try that.

How to do it…

  1. Navigate to the OpenAI Playground.
  2. In the SYSTEM field, type in the following: You are an assistant that creates engaging and professional company names based on descriptions of companies.
  3. In the Chat Log, populate the USER message with the following: A company that helps you with your taxes.
  4. Select the Submit button on the bottom of the page.
  5. You should now see a completion response from OpenAI. In my case in Figure 1.3, the response is as follows:
    TaxGuardian.
Figure 1.3 – The OpenAI Playground with prompt and completion

Figure 1.3 – The OpenAI Playground with prompt and completion

  1. Hover over the ASSISTANT response and select the minus icon on the right-hand side to delete the model’s response from the Chat Log. This needs to be done because we want OpenAI to not only generate a response but generate one as the ASSISTANT.
  2. Modify the System Message to You are an assistant that creates potential customer segments and marketing strategies based on descriptions of companies.
  3. Select the Submit button on the bottom of the page.
  4. You should now see a much longer completion response from OpenAI. In my case, as shown in Figure 1.4, the response details potential customer segments:
Figure 1.4 – The OpenAI Playground response after modifying the System Message

Figure 1.4 – The OpenAI Playground response after modifying the System Message

How it works…

The model generates a completely different type of response after modifying the System Message, even though the prompt in the Chat Log is exactly the same. The response changed from a one-word answer to a multi-paragraph response because of the instructions from the System Message. We also had to remove the default Assistant response because we want OpenAI to generate its own response instead of feeding it a response.

The System Message, being the first message in the conversation, heavily influences the thinking of the model by providing a frame of reference or context. This context is crucial because, without it, the model would lack necessary guidance to respond appropriately to subsequent user inputs. It is the cornerstone of defining your interaction with the model, allowing you to provide important context and high-level directives that steer the conversation or task at hand.

As a result, when we begin to use the OpenAI API to create business applications, careful considerations must be made to the instructions that we put into the System Message.

There’s more…

The beauty of the System Message is that that you can be as simple or as intricate with your instructions as you want. For example, here are common system messages that can be used for various purposes:

  • You are an assistant that helps young students learn important concepts in science by explaining concepts in easy-to-understand language
  • You are an assistant that creates marketing slogans based on descriptions of companies that are provided to you
  • I am planning a birthday party 2 weeks from now for my 5 year old niece and you are my party planner that tells me what I should be doing