Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenAI API Cookbook

You're reading from   OpenAI API Cookbook Build intelligent applications including chatbots, virtual assistants, and content generators

Arrow left icon
Product type Paperback
Published in Mar 2024
Publisher Packt
ISBN-13 9781805121350
Length 192 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Henry Habib Henry Habib
Author Profile Icon Henry Habib
Henry Habib
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Chapter 1: Unlocking OpenAI and Setting Up Your API Playground Environment FREE CHAPTER 2. Chapter 2: OpenAI API Endpoints Explained 3. Chapter 3: Understanding Key Parameters and Their Impact on Generated Responses 4. Chapter 4: Incorporating Additional Features from the OpenAI API 5. Chapter 5: Staging the OpenAI API for Application Development 6. Chapter 6: Building Intelligent Applications with the OpenAI API 7. Chapter 7: Building Assistants with the OpenAI API 8. Index 9. Other Books You May Enjoy

Running a completion request in the OpenAI Playground

In this recipe, we will actually put the Playground in action and execute a completion request from OpenAI. Here, you will see the power of the OpenAI API and how it can be used to provide completions for virtually any prompt.

Getting ready

Ensure you have an OpenAI Platform account with available usage credits. If you don’t, please follow the Setting up your OpenAI Playground environment recipe. All the recipes in this chapter will have this same requirement.

How to do it…

Let’s go ahead and start testing the model with the Playground. Let’s create an assistant that writes marketing slogans:

  1. Navigate to the OpenAI Playground.
  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 writes engaging mystery novels.
  4. Select the Submit button on the bottom of the page.
  5. You should now see a completion response from OpenAI. In my case (Figure 1.2), the response is as follows:
    Unlock the Thrilling Pages of Suspense with Our Captivating Mystery Novels!
Figure 1.2 – The OpenAI Playground with prompt and completion

Figure 1.2 – The OpenAI Playground with prompt and completion

Note

Since OpenAI’s LLMs are probabilistic, you will likely not see the same outputs as me. In fact, if you run this recipe multiple times, you will likely see different answers, and that is expected because it is built into the randomness of the model.

How it works…

OpenAI’s text generation models utilize a specific neural network architecture termed a transformer. Before delving deeper into this, let’s unpack some of these terms:

  • Neural network architecture: At a high level, this refers to a system inspired by the human brain’s interconnected neuron structure. It’s designed to recognize patterns and can be thought of as the foundational building block for many modern AI systems.
  • Transformer: This is a type of neural network design that has proven particularly effective for understanding sequences, making it ideal for tasks involving human language. It focuses on the relationships between words and their context within a sentence or larger text segment.

In machine learning, unsupervised learning typically refers to training a model without any labeled data, letting the model figure out patterns on its own. However, OpenAI’s methodology is more nuanced. The models are initially trained on a vast corpus of text data, supervised with various tasks. This helps them predict the next word in a sentence, for instance. Subsequent refinements are made using Reinforcement Learning through Human Feedback (RLHF), where the model is further improved based on feedback from human evaluators.

Through this combination of techniques and an extensive amount of data, the model starts to capture the intricacies of human language, encompassing context, tone, humor, and even sarcasm.

In this case, the completion response is provided based on both the System Message and the Chat Log. The System Message serves a critical role in shaping and guiding the responses you receive from Open AI, as it dictates the model’s persona, role, tone, and context, among other attributes. In our case, the System Message contains the persona we want the model to take: You are an assistant that creates marketing slogans based on descriptions of companies.

The Chat Log contains the history of messages that the model has access to before providing its response, which contains our prompt, A company that writes engaging mystery novels.

Finally, the parameters contain more granular settings that you can change for the model, such as temperature. These significantly change the completion response from OpenAI. We will discuss temperature and other parameters in greater detail in Chapter 3.

There’s more…

It is worth noting that ChatGPT does not read and understand the meaning behind text – instead, the responses are based on statistical probabilities based on patterns it observed during training.

The model does not understand the text in the same way that humans do; instead, the completions are generated based on statistical associations and patterns that have been trained in the model’s neural network from a large body of similar text. Now, you know how to run completion requests with the OpenAI Playground. You can try this feature out for your own prompts and see what completions you get. Try creative prompts such as write me a song about lightbulbs or more professional prompts such as explain Newton's first law.

You have been reading a chapter from
OpenAI API Cookbook
Published in: Mar 2024
Publisher: Packt
ISBN-13: 9781805121350
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image