Download as:
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Language:EN

Click the add file option and add new file named search

Steve Tingiris

Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Technical Editor: Devanshi Ayare

Copy Editor: Safis Editing

First published: July 2021

Production reference: 1100621

B3 2PB, UK.

ISBN 978-1-80056-319-3

Technology Review claimed the GPT-3 was shockingly good. Many long-time AI researchers expressed both enthusiasm and surprise at its capabilities. But it was the demos that really captured everyone's attention. With a few lines of sample content and a request, the technology was generating poetry, website programming code, analogies, and answers to math questions, to name just a few examples. No one had ever seen a computer create and respond creatively to such a wide range of queries.

GPT-3 seemed to possess magical abilities. In many ways, GPT-3 is very simple. It predicts what words are the most likely to follow in a sequence. However, it is also the finest current example of the potential of Generative Adversarial Networks (GANs). And, it has shown that transformers can be applied to language models at an extremely large scale. GPT-3 provides an intriguing new technical capability while simultaneously resetting expectations about what is possible. As just one example, maybe chatbots don't have to choose between a set number of deterministic responses.

Contributors

About the author

Thanks to Russell, Bakz, Ryan, and Bram for agreeing to be technical reviewers. You guys were so helpful to me in the OpenAI Slack channel when I first got started with GPT-3, I feel so fortunate to have had the opportunity to collaborate together on this book. Again, thank you!

Next, thank you to my work family at Dabble Lab. Kirk Owen for providing early feedback that helped refine the direction, and Mohamad Khalid, Manuel Fernandez, Mark
Hovsepyan, Sohini Pattanayak, Shubham Prakash, and Daniela Ramirez, for picking up the slack while I spent much more time than I'd anticipated working on the book.

Table of Contents

Preface

2
GPT-3 Applications and Use Cases

20 Getting started with the Playground 20
22
20
20

ii Table of Contents

Section 2: Getting Started with GPT-3

3
Working with the OpenAI Playground

53

Temperature and Top P

54

44

59
60
44
61

Developer resources

46

Inject Start Text and Inject Restart Text

61
46
63
48
49

Working with presets

64

Member management

50
65
67

Parse unstructured data

68

Choosing the right engine

51
68

Table of Contents iii

Understanding API authentication

80
89
82
94

Making an authenticated request to the OpenAI API

83

Summary

98
86

Section 3: Using the OpenAI API

6
Content Filtering

127
128
140

Understanding content filtering 129

Filtering content with Python 142

Testing the content filtering process

133
Flagging unsafe words with Python 146
148

Uploading files

203

Understanding text classification

204
202

text classification

Classifying text by language 212
Content filtering is a text classification 202

Classifying text from keywords 217

221

9
Building a GPT-3-Powered Question-Answering App

238

Creating the API endpoint

230

Generating relevant and factual answers 242

232
243

Summary

249

Integrating the Answers

Preface

What if this book was written by artificial intelligence? Would you read it? I hope so because parts of it were. Yes, GPT-3 was used to create parts of this book. It's a bit meta I know, a book about GPT-3 written by GPT-3. But creating content is one of the many great uses for GPT-3. So why not? Also, for me, content generation was the use case that most piqued my interest. I wondered if GPT-3 could be used in a product I was working on to automate the generation of technical learning material.

viii Preface

What this book covers

Chapter 5, Calling the OpenAI API in Code, is an introduction to using the OpenAI API with both Node.js/JavaScript and Python.

Chapter 6, Content Filtering, explains how to implement content filtering.

To get the most out of this book

All of the code examples in this book wer a web-based Integrated
Development Environment (IDE) from . A free replit.com account is sufficient to follow the examples. To use rhat is required is a modern web browser and a replit.com account. The code has also been tested on macOS using Visual Studio Code, although it should work with any code editor and properly configured operating system. Code examples are provided in both Node.js/JavaScript and Python. For Node.js, version 12.16.1 is used and for Python, version 3.8.2 is used.

All of the code examples will require an OpenAI API PI. You can request access to the OpenAI API by visiting .

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles.

Here is an example: "However, suppose you don't want the completion to generate the human side of the conversation and you want to use the label AI: rather than Assistant:?"

Tips or important notes

Appear like this.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have founl if you would report this to us. Please visit , selecting your book, clicking on the Errata ils.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

Section 1:
Understanding GPT-3 and the OpenAI API

The buzz about Generative Pre-trained Transformer Version 3 (GPT-3) started with a blog post from a leading Artificial Intelligence (AI) research lab, OpenAI, on June 11, 2020. The post began as follows:

We're releasing an API for accessing new AI models developed by OpenAI. Unlike most AI systems which are designed for one use-case, the API today provides a general-purpose "text in, text out" interface, allowing users to try it on virtually any English language task.

• Democratizing NLP

• Understanding prompts, completions, and tokens

Introduction to GPT-3

In short, GPT-3 is a language model: a statistical model that calculates the probability distribution over a sequence of words. In other words, GPT-3 is a system for guessing which text comes next when text is given as an input.

Introduction to GPT-3 5

While understanding things such as word meanings and their appropriate usage seems trivial to humans, NLP tasks can be challenging for machines. This is especially true for more complex language processing tasks such as recognizing irony or sarcasm—tasks that even challenge humans at times.

GPT-3 was trained with a massive dataset comprised of text from the internet, books, and other sources, containing roughly 57 billion words and 175 billion parameters. That's 10 times larger than GPT-2 and the next-largest language model. To put the model size into perspective, the average human might read, write, speak, and hear upward of a billion words in an entire lifetime. So, GPT-3 has been trained on an estimated 57 times the number of words most humans will ever process.

The GPT-3 language model is massive, so it isn't something you'll be downloading and dabbling with on your laptop. But even if you could (which you can't because it's not available to download), it would cost millions of dollars in computing resources each time you wanted to build the model. This would put GPT-3 out of reach for most small companies and virtually all individuals if you had to rely on your own computer resource to use it. Thankfully, you don't. OpenAI makes GPT-3 available through an API that is both affordable and easy to use. So, anyone can use some of the most advanced AI ever created!

As you might guess, the quality of a completion depends heavily on the prompt. GPT-3 uses all of the text in a prompt to help generate the most relevant completion. Each and every word, along with how the prompt is structured, helps improve the language model prediction results. So, understanding how to write and test prompts is the key to unlocking GPT-3's true potential.

Understanding prompts, completions, and tokens

When you're writing prompts, the main thing to keep in mind is that GPT-3 is trying to figure out which text should come next, so including things such as instructions and examples provides context that helps the model figure out the best possible completion. Also, quality matters— for example, spelling, unclear text, and the number of examples provided will have an effect on the quality of the completion.

Another key consideration is the prompt size. While a prompt can be any text, the prompt and the resulting completion must add up to fewer than 2,048 tokens. We'll discuss tokens a bit later in this chapter, but that's roughly 1,500 words.

• One-shot prompts

• Few-shot prompts

Subject:

8 Introducing GPT-3 and the OpenAI API

One-shot prompts

A one-shot prompt provides one example that GPT-3 can use to learn how to best complete a task. Here is an example of a one-shot prompt that provides a task description (the first line) and a single example (the second line):

Figure 1.2 – One-shot prompt example

Exploring GPT-3 and someone who is reading the book.

You'll learn what GPT-3 is and how to get started using it.

Steve: No. Even if you've never written a line of code before,

Figure 1.3 – Few-shot prompt example

Understanding prompts, completions, and tokens 11

12 Introducing GPT-3 and the OpenAI API

Now that you have an understanding of prompts, let's talk about how GPT-3 uses them to generate a completion.

GPT-3 works differently, as it isn't trained for any specific task. Rather, it was designed to recognize patterns in the prompt text and to continue the pattern(s) by using the underlying general-purpose model. This approach is referred to as meta-learning because the prompt is used to teach GPT-3 how to generate the best possible completion, without the need for retraining. So, in effect, the different prompt types (zero-shot, one-shot, and few-shot) can be used to program GPT-3 for different types of tasks, and you can provide a lot of instructions in the prompt—up to 2,048 tokens. Alright—now is a good time to talk about tokens.

Tokens

This is located just under the large text input, on the bottom right. The magnified area in the following screenshot shows the token count. If you hover your mouse over the number, you'll also see the total count with the completion. For our example, the prompt Do or do not. There is no try.—the wise words from Master Yoda—uses 10 tokens and 74 tokens with the completion:

Figure 1.7 – Usage statistics

So, the engines or models each has a different cost but the one you'll need depends on the task you're performing. Let's look at the different engine options next.

Introducing Davinci, Babbage, Curie, and Ada

Davinci

Davinci is the most capable model and can do anything that any other model can do, and much more—often with fewer instructions. Davinci is able to solve logic problems, determine cause and effect, understand the intent of text, produce creative content, explain character motives, and handle complex summarization tasks.

Ada

Ada is usually the fastest model and least costly. It's best for less nuanced tasks—for example, parsing text, reformatting text, and simpler classification tasks. The more context you provide Ada, the better it will likely perform.

Understanding GPT-3 risks 17

A snapshot in time

Inappropriate or offensive results

GPT-3 generates text so well that it can seem as though it is aware of what it is saying.

It's not hard to imagine potentially malicious or harmful uses for GPT-3. OpenAI even describes how GPT-3 could be weaponized for misinformation campaigns or for creating fake product reviews. But OpenAI's declared mission is to ensure that artificial general intelligence benefits all of humanity. Hence, pursuing that mission includes taking responsible steps to prevent their AI from being used for the wrong purposes. So, OpenAI has implemented an application approval process for all applications that will use GPT-3 or the OpenAI API.

18 Introducing GPT-3 and the OpenAI API

2 GPT-3 Applications and Use Cases

GPT-3 was designed to be a general-purpose language processing model, meaning it wasn't explicitly trained for any one type of language processing task. So, possible use cases include virtually any natural language processing task you can imagine and others that probably haven't been imagined yet. New use cases for GPT-3 are constantly being discovered and that is a big part of the allure for many users. Sure, it does better with some tasks than others, but still, there are hundreds of possible uses. In this chapter, we'll break down some general use cases, and see how you can get started testing prompts of your own.

• Understanding semantic search

20 GPT-3 Applications and Use Cases

Text generation: Text generation tasks are tasks for creating new, original text content. Examples include article writing and chatbots.

Classification: Classification tasks tag or classify text. Examples of classification tasks include things such as sentiment analysis and content filtering.

Getting started with the Playground

To access the Playground, you log in at . After you've authenticated, you'll be able to navigatehe main menu.

The following screenshot shows the Playground with the initial prompt text: If today is Monday, tomorrow is. You'll notice that the original prompt text is displayed in bold, and the completion is displayed as normal text. In this example, the Submit button was clicked multiple times to illustrate how each completion is building on the last:

Response Length: How much text will be included in the completion

22 GPT-3 Applications and Use Cases

Best Of: Setting to only return the best of n completions

Stop Sequence: A sequence of characters to end a completion

Now that we've seen how to start using the Playground, let's learn a bit more about text generation and classification tasks.

Handling text generation and classification tasks

Generating text

GPT-3 can generate original text content that is usually indistinguishable from human-written text. This could be used for a variety of applications, from creating web content to brainstorming, conversational applications, poetry, songwriting, writing code, and creating data lists. Let's take a look at some examples.

Figure 2.2 – Text generation example – tips for first-time home buyers

Again, you can use GPT-3 to create a list on just about any topic, so there are tons of possibilities. Another great example use case is idea generation.

2.

The bold text in Figure 2.3 is the prompt that was provided, and the regular text was generated by GPT-3. Pretty cool, right? Here is another cool example – conversational applications.

Conversational applications

assistant. The assistant is helpful, creative, clever, and very

help!

Figure 2.4 – Text generation example – customer support AI assistant

There are a number of techniques you can use to help direct GPT-3 on how to respond.

Cisco – Technology, Networking, Enterprise Software

The result for the previous prompt is shown in the following screenshot:

words that rhyme have similar sounding endings

a: big, dig, fig

Content creation, idea generation, conversational applications, creating lists, and generating quizzes are just a few of the possible text generation use cases. But text generation isn't just about creating new content; it can also be used for other use cases such as summarizing existing content.

Summarizing text

the scale of atoms and subatomic particles.[2]:1.1 It is the

Classical physics, the description of physics that existed

describes many aspects of nature at an ordinary (macroscopic)
can be derived from quantum mechanics as an approximation valid

Figure 2.7 – Text generation example – tl;dr: summary

own all rights, title, and interest in and to the APIs,

your Application. You have no right to distribute or allow

in these Terms, neither party grants, nor shall the other party

The following screenshot shows the results of the preceding one-sentence summary prompt:

To summarize text using language that would be appropriate for someone of a certain age, you can use grade-level summaries. This can be done by following the text you want to summarize with something like the last sentencre using text that was copied from :

The Milky Way[a] is the galaxy that contains our Solar System,

circle").[19][20][21] From Earth, the Milky Way appears as a band because its disk-shaped structure is viewed from
within. Galileo Galilei first resolved the band of light into individual stars with his telescope in 1610. Until the early 1920s, most astronomers thought that the Milky Way contained all the stars in the Universe.[22] Following the 1920 Great Debate between the astronomers Harlow Shapley and Heber
Curtis,[23] observations by Edwin Hubble showed that the Milky Way is just one of many galaxies. The Milky Way is a barred spiral galaxy with an estimated visible diameter of 150-200,000 light-years,[9][24][25] an increase from traditional estimates of 100,000 light-years. Recent simulations suggest that a dark matter disk, also containing some visible stars, may extend up to a diameter of almost 2 million light-years.[11][12]

Note that the summary shown in Figure 2.9 is written in a way that would likely be understood by a third grader. In this case, GPT-3 is translating the text (in a way) for a younger reader. But you can also use GPT-3 to translate text into different languages.

Handling text generation and classification tasks 31

Figure 2.10 – Text generation example – translation from English to French

32 GPT-3 Applications and Use Cases

Conversion

Text classification

Text classification involves evaluating some provided text and assigning it a label, score, or some other attribute that classifies the text. Sentiment analysis is a common text classification use case, but that's just one of many text classifications tasks GPT-3 could be used for.

Here is an example of a zero-shot classification prompt. In this example, the goal is to perform sentiment analysis to determine whether a Twitter post is positive, neutral, or negative:

Here is another zero-shot classification example. This example shows that GPT-3 can even comprehend text for a classification task. Notice the prompt provides a question and GPT-3 classifies the travel type:

Figure 2.13 – Text generation example – zero-shot classification

Q:"I like Tigers"

The results from the previous prompt are shown in the following screenshot:

The following prompt shows an example of a prompt for doing batch classification. Notice that both classification examples and a batch classification example are provided in the prompt:

Tweet: "I loved the new Batman movie!"

36 GPT-3 Applications and Use Cases

###

5. "This new music video blew my mind"

5. Positive

5. "I hate chocolate"

A semantic search matches a search term or query words with semantically similar documents containing any amount of text. A simple keyword search might just look for words in the query that match words in the documents. However, a semantic search goes way beyond that. It looks at the meaning of words and ranks the documents with the highest-ranking document representing the document that is most semantically similar to the query. For example, suppose we have the query an animal with wings and five one-word documents: dog, cat, snake, rabbit, eagle. A semantic search would rank each of the five documents and assign the highest rank to the document containing the word eagle because it is most semantically similar to the query.

Every time you query Google, you're using semantic search and like Google, GPT 3 can also search over documents. However, rather than searching documents on the web, the documents are provided as part of the request to the OpenAI API or in a pre-uploaded file.

The Semantic Search tool

The Playground is used for testing prompts and completions. However, the Semantic Search toolthe Semantic Search tool by visiting . It's a simple web-based applicationearches using the different engines. The following screenshot shows the Semantic Search tool:

Understanding semantic search 39

As you can hopefully see at this point, the possible use-cases for GPT 3 are pretty broad. We haven't even scratched the surface of what's possible or discussed how GPT 3 might be used in production applications. We will dive deeper into more specific examples and use-cases throughout the book. But in addition, there is a rapidly growing number of GPT 3 powered applications and examples online that you should check out for additional use-cases and inspiration. The range of online applications is impressive to say the least. You can start by Googling the term list of apps using GPT-3. You'll find a growing number of curated lists and videos that highlightinered apps and prompt examples. GPT-3 Demo located at also a blog post from that lisple at .

Summary

This section comprises the following chapters:

Chapter 3, Working with the OpenAI Playground

The topics we will be covering in this chapter are as follows:

• Exploring the OpenAI developer console

44 Working with the OpenAI Playground

Exploring the OpenAI developer console

Thed at .

It be returned for questions it likely doesn't have the correct answer to. This directs the model to not make up an answer, which it would likely do by default. This is an important example because although GPT-3 responses are almost always grammatically correct, they are very often not factual. So, even though they might sound good, they could be completely fabricated.

A: ?

A: Sacramento.

A: ?

A: ?

A: Two, Phobos and Deimos.

The documentation is just one of the available resources in the developer portal. Other resources are available, including the following:

FAQs

Guidelines and Legal Documents

Logo Assets

Each organization has a title (name) that you can specify and an organization ID that is automatically generated. The organization ID is a unique identifier used to associate usage with the proper organization for billing purposes. So, when you're logged in to the developer console, any usage will be associated with the organization you're working under. We'll discuss the organization ID again in Chapter 4, Working with the OpenAI API, where we'll look at associating API calls with a specific organization, but you can also associate usage with an organization in the developer console.

The following screenshot shows how to see the organizations your account is associated with and how to switch between organizations in the developer console:

Figure 3.2 – Personal organization
Of course, you're the owner of your personal organization and this is the organization you would set up billing for if you're using the API for your own individual use.

48 Working with the OpenAI Playground

The following screenshot shows the pricing per engine at the time this book was published. Again, the pricing could change at any time, so be sure to verify the current pricing as it may very well have changed:

This is done in your billing settings. The hard limit prevents the API from using more tokens once the limit is met. Of course, this will render the API unusable, which could be a problem in production apps. So, there is also the option of setting a soft limit. This will send an email alert when usage limits are hit.

Usage reporting

50 Working with the OpenAI Playground

Member management

Diving deeper into the Playground

At this point, you should understand the basics of using the Playground. But we're going to cover the Playground in more depth now and discuss all of the available options and settings. Chapter 2, GPT 3 Applications and Use Cases, provided a quick overview of the available settings, but let's take a closer look at each of them.

The first setting is the Engine setting, so we'll start there.

Choosing the right engine

The following is a prompt classifying items as a tool, food, clothing, or something else:

Pliers: Tool

The following screenshot shows the results when davinci is used as the engine. Note that the new items added to the list (Shirt, Hammer, Apple, and Airplane) are all categorized correctly:

Figure 3.8 – Classification example with the ada engine

Babbage: Moderate classification, semantic search classification.

Ada: Text parsing, simple classification, address correction, keywords.

Temperature and Top P

The next two settings are Temperature and Top P. These are two of the most important settings, but they can also be the most confusing ones to understand. At a high level, they both influence the randomness or diversity of the response that is generated. But knowing how and when to use one or the other can be tricky.

The temperature setting influences how deterministic the model will be when generating a result. So, the temperature provides some control over how likely the results are to vary. A lower value will direct the model to be more deterministic (less variable), while a higher value will cause the model to be less deterministic, or more variable. The range can be between 0 and 1. To see the effects of the temperature setting let's look at some examples.

We'll start with an example that uses the default Playground temperature of 0.7. In this example, we'll look at the default stochastic (random) nature of most completions. We'll start with a prompt that contains the words Once upon a time and nothing else, like the prompt shown in the following screenshot:

Once upon a time, a little princess was born.

Once upon a time, there were three little pigs.

The last reason has to do with the Top P setting, but we'll talk about that a little later.

56 Working with the OpenAI Playground

So, what's happening? The lower temperature value is telling GPT-3 to be less variable in how it processes the prompt, so the completion is staying consistent. Because we're using zero for the temperature (the lowest value), the result will likely always be the same. However, you can use a value between zero and one (for example 0.2) to gain more control over the randomness of the result. However, changing the temperature won't always affect the completion because again, the completion also depends on the examples in the data the model was trained on. To illustrate this, let's look at another example.

For this example, we'll use a prompt that just includes the words A robot may not injure with the default temperature setting of 0.7, as we have in the following screenshot:

A robot may not injure a human being or, through inaction, allow a human being to come to harm.

A robot may not injure a human being or, through inaction, allow a human being to come to harm.

58 Working with the OpenAI Playground

Like temperature, Top P can be used to increase or limit the seeming randomness of a completion. However, unlike the temperature, it's influencing the randomness by limiting the scope of the possible results that should be considered. To illustrate the point, imagine that 100 potential token options could be selected as the next token in a completion. A Top P value of 0.1 could be used to limit the options to 10, thereby reducing the number of tokens that could be selected. But this is still dependent on the number of possible token options derived from the model. So, if there were only 10 potential options, a 0.5 Top P setting would limit that to five options – reducing the variability. Furthermore, a Top P value of 0 will always reduce the options to the top token, meaning that even if there are a lot of possible options and the temperature setting was 1 (which will generate the most options), if the Top P setting is 0, only the best option will be selected, which will have the same effect as setting the temperature to 0 – you'll most likely always get the same result. To illustrate this, let's look at our Once upon a time prompt again. This time, we'll set the temperature to 1 and the Top P value to 0, as we have in the following screenshot:

Once upon a time, there was a little girl who was very sad.

Once upon a time, there was a little girl who was very sad.

The frequency and presence penalty settings can be useful for preventing the same completion text from being repeated across multiple requests. The two settings are very similar to one another, with the only difference being that the frequency penalty is applied if the text exists multiple times, whereas the presence penalty is applied if the text exists at all. Let's take a look at another example.

The following screenshot shows the results of the Once upon a time example with a temperature setting of 1, a Top P setting of 0, and no frequency or presence penalty. The submit button was clicked 5 times and each completion generated a new sentence. While no sentences are repeated verbatim, there are a number of repeated token sequences – notice that each completion sentence begins with the words She was sad because she had no:

The next screenshot shows the results after adding a presence penalty and clicking Submit five times (like we did for the last example). This time, you can see that the completion sentences are not as similar:

Diving deeper into the Playground 61

Stop sequence

Let's move on to the next section!

Inject Start Text and Inject Restart Text

62 Working with the OpenAI Playground

With the default Playground settings, the completions might look something like the completion in the following screenshot:

Figure 3.16 – Using Stop Sequences, Inject Start Text, and Inject Restart Text together

Most Likely

Least Likely

64 Working with the OpenAI Playground

The settings provide a lot of control over how a completion is generated, but selecting the right setting can take a bit of trial and error. Thankfully, the Playground includes presets to help you understand how to best select the right combination of settings for a given task.

Q&A

Grammatical Standard English

Classification

You'll find a drop-down list of the presets in the Playground just above the large input box. The following screenshot shows the location:

Grammatical Standard English

The Grammatical Standard English preset demonstrates a use case where non-standard US English text is transformed into standard US English text. The following is the preset's prompt text:

of the design you're looking for and some examples or previous

Non-standard English: There is plenty of fun things to do in

66 Working with the OpenAI Playground

As mentioned, presets also include settings. So, after selecting the Grammatical Standard English preset, you'll notice that some of the default Playground settings have changed.

Note that the temperature is set to 1 and Top P is used to limit the results considered to 70% of the possible options. Also notice that the stop sequence is used along with the inject start text and inject restart text to keep the completion short while continuing the prompt pattern for the next phrase to standardize.

Working with presets 67

A: send-msg `find greg` Is everything ready for Wednesday?

A: send-msg `find ski store` Would it be possible to get my

A: send-msg `find constance` I won't be home before 19:30

Temperature: 0.5

Top P: 1

In this preset, notice that the temperature is set to 0.5 and a slight frequency penalty of 0.2 is used.

68 Working with the OpenAI Playground

loheckles, which are a grayish blue fruit and are very tart, a
are more savory than sweet. There are also plenty of loopnovas
Finally, there are fruits called glowls, which have a very sour

| Loheckles | Grayish blue | Tart |

The settings used for the Parse unstructured data preset are as follows:

The settings worth noting in this preset are the temperature and Top P settings.

Summary

While the Playground is a great place for learning and testing, when you're building applications that incorporate GPT-3, you'll also need to understand how to use the OpenAI API directly. So, in this chapter, we'll look at using the OpenAI API directly by making HTTP requests. We'll start with a general introduction to APIs and the HTTP protocol. Then we'll look at a couple of developer tools for working with API requests and the JSON data-interchange format.

The topics we'll cover are as follows:

• Understanding API authentication

• Making an authenticated request to the OpenAI API

Technical requirements

This chaaccess to the OpenAI API. You can request access by visiting .

Since web-based APIs are accessible using HTTP and work with common data formats, they don't depend on any specific programming language, meaning just about any programing language or development tool that can make an HTTP request can interact with an HTTP API. In fact, you can even u . For example, if you open in your web browser, you'lt the ans that are currently in space. The results aren't formatted nicely because they're intended for machine use, not human consumption, but we can see the results in a browser because it's using the same web protocol that websites use, as seen in the following screenshot:

Getting familiar with HTTP 71

Because APIs are designed to be used in code, in order to work with them, you do need to know a bit more about the HTTP protocol than you do for just accessing websites. So, in this section, you'll learn some HTTP basics.

For starters, HTTP is a request-response protocol. So, a client (the requesting system) makes a request to a server (the receiving system), which then responds to the client. The client references the server and the resource being requested using a Uniform Resource Identifier (URI).

In addition to the URI, the HTTP protocol also supports different request types, called HTTP methods, which provide additional information about the request being made.

HTTP methods

HTTP headers

The HTTP body isn't the only way to send/receive data. You can also include data as part of the URL, or as an HTTP header. HTTP headers are key/value pairs that can be used to send/receive values between the client and server. While HTTP headers can be used for a variety of reasons, they usually define metadata, or data that provides details about the request. For example, an HTTP header named Content-Type is used to tell the server what type of data is being passed in the body, and an Authorization header can be used to send authentication details, such as a username and password.

Everything that we've looked at doing through the Playground can also be done via the OpenAI API – and then some. In fact, the Playground is just a web interface that calls the OpenAI API. It is simply exposing functionality that the API provides using a graphical interface. So, in this section, we'll review the OpenAI functionality that's available through the API endpoints. You'll be familiar with the functionality because you've used it through the Playground, but after this section, you'll know how to access specific functionality in code.

74 Working with the OpenAI API

• Perform semantic searches

All of the OpenAI API endpoints require authentication. So, they can't just be called with a browser, like the Open-Notify API that we looked at earlier. But we'll hold off on talking about authentication just yet and review each of the available endpoints.

Next is the Retrieve Engine endpoint!

Retrieve Engine

Introducing CURL and Postman 75

Create Completions

Semantic Search

The Semantic Search endpoint can be used to perform a semantic search over a list of documents. A semantic search compares a search term to the contents of a document to identify documents that are semantically similar. The documents to be searched are passed to the endpoint as part of the HTTP body and up to 200 documents can be included. This endpoint uses the HTTP POST method and requires an engine ID to be passed as part of the endpoint URI.

In this section, we'll look at a couple of developer tools for working with APIs. As
we've discussed, APIs are designed to be used in code. However, during the development process, you'll often want to call an API endpoint without writing code to get familiar with the functionality or for testing. To do that, there are a number of developer tools available. Two of the most popular developer tools for working with APIs are CURL and Postman.

Figure 4.2 – Curl command in the OpenAI API docs

Introducing CURL and Postman 77

As we take a closer look at the OpenAI API, we'll look at making different API calls with CURL. But before we do that, let's take a look at Postman, an alternative to using CURL.

Postman

After signing up, you'll be presented with a short onboarding process. Complete the onboarding process and when you're done, you should see a screen similar to the following screenshot:

Introducing CURL and Postman 79

To get started with Postman, let's make a request to the Open-Notify API endpoint that we looked at previously in our browser. To do that, complete the following steps:

1. After logging in to , click the Create New link. If you're prompted to download the Deskthe Skip for now button. This will bring you to your workspace, which will look something like the following screenshot:

2. nter the request URL (), and then click the Seshould enshot:

Understanding API authentication 81

An API authentication process does the same thing as a website login, but in a way that is practical for applications rather than humans. There are many different ways in which APIs can authenticate application requests, but we're going to focus on one of the most common methods, basic authentication, because that's what the OpenAI API uses.

Figure 4.8 – API Keys

Figure 4.9 – API key in documentation

It's time to make our first request directly to the OpenAI API. To do that, we'll need to include our API key as part of an HTTP header. The header name we'll be using is authorization and the value will be the word Bearer, followed by a space and then your API key. When an API key is used like this, it's often also referred to as a bearer token. This is a standard defined by an auAuth 2.0. You can learn more about OAuth 2.0 by visiting .

Postman makes it really easy to use bearer tokens. But before we make an authenticated request, let's look at what happens if we try to make a request without our API key. The following screenshot shows a request to the List Engines endpoint URL, https:// api.openai.com/v1/engines, without any authorization header. You can see an error message was returned. You'll also notice that the HTTP response status code is 401 UNAUTHORIZED:

Setting up Postman variables

Variables in Postman allow you to store and reuse values rather than having to enter them over and over. Variables can also be grouped into a Postman environment. So, we're going to set up an environment named openai-dev and add a variable named OPENAI_API_ KEY to store our API key.

4. Add a variable named OPENAI_API_KEY.

5. Enter your API key in the INITIAL VALUE input box.

Figure 4.11 – Postman with the environment set

Making an authenticated request to the OpenAI API 85

2. Choose the Bearer Token option from the Type drop-down list.

3. Enter {{OPENAI_API_KEY}} in the Token input box.

86 Working with the OpenAI API

As mentioned earlier, the bearer token is sent as an HTTP header. To see the header in Postman, click on the Headers tab and then unhide the hidden headers, and you'll see the Authorization header with your API key as the bearer token value, as demonstrated in the following screenshot:

To associate API requests with a specific organization, you'll include an
OpenAI-Organization HTTP header with the organization ID for the organization you want to associate your requests with. This is only required when your user account is associated with multiple organizations.

To add the OpenAI-Organization header in Postman, scroll to the bottom of the list of existing headers and add a new one with the name OpenAI-Organization and make the value an organization ID that your account is associated with. Better yet, add a new environment in Postman named OPENAI_ORGANIZATION_ID and add {{OPENAI_ ORGANIZATION_ID}} as the value. As a reminder, you can find your organization ID on the account settings page in the OpenAI developer console, as seen in the following screenshot:

Figure 4.15 – Using the OpenAI-Organization HTTP header
Throughout the rest of this book, we'll be using Postman to illustrate and test API calls. But before we move on, let's take a look at making an authenticated API call with CURL.

88 Working with the OpenAI API

After replacing the {your-api-key} placeholder and the {your-
organization-id} placeholder, this command will return something like the results shown in the following screenshot:

The JSON syntax is based on a subset of the JavaScript programming language and it defines two data structures:

• A collection of name/value pairs

An object can contain a set of name/value pairs, referred to as elements. Elements in an object don't need to be in any particular order and the value can be a string (enclosed in double quotes), a number, true or false, null, another object, or an array. Element names and values are separated by a colon, and elements themselves are separated by a comma. The following code block is an example of a JSON object from an OpenAI API response. You'll notice that it starts and ends with braces and contains different elements with names and values. Notice that the value of the "choices" element contains a value that begins with a left bracket and ends with a right bracket – that is an array:

"choices": [

}

90 Working with the OpenAI API

The last thing to note about JSON is that the formatting of the text, things such as spaces, tabs, and line returns, is done for human readability but is not required by machines. So, as long as the syntax is valid, it's useable in code.

For example, the following two JSON objects are the same and both are valid:

"answer" : "Yes"

When you're working with the OpenAI API, most of what you'll be doing will probably involve using the Completions endpoint. This is the endpoint you send prompts to. In addition to submitting your prompt, you can also include values to influence how the completion is generated, like the setting in the Playground.

Using the Completions endpoint 91

2. Enter https://api.openai.com/v1/engines/davinci/completions for the endpoint.

3. Set the body to raw.

Figure 4.17 – Postman settings for the Completions endpoint

92 Working with the OpenAI API
After clicking the Send button, we get a response from the Completions endpoint, as shown in the following screenshot:

Using the Completions endpoint 93

The previous example was a simple one with just one request parameter – the prompt.

n the Playground that we covered in Chapter 3, Working with the OpenAI Playground, so they'll be familiar to you.

But before we move on, let's take a look at another example, one that you can't do from the Playground. In this example, we're going to submit multiple prompts simultaneously and get back a completion for each. We'll use the following JSON:

"stop": "\n"

Figure 4.21 – Sending multiple prompts

The main takeaway from this example is that there are things you can do with the API that you can't do in the Playground. So, understanding how to work with the OpenAI API enables you to go beyond what you can do in the Playground.

To perform a semantic search using the API, you'll make a POST request to the Semantic Search endpoint. Like the Create Completions endpoint, you'll also include a JSON object in the request body. The JSON body object has two elements – the documents element and the query element. The documents element is an array of documents to be searched, and each item in the array is a string that represents a document. Alternatively, a document can be provided in a pre-uploaded file that can be referenced in the request. In Chapter 9, Building a GPT-3 Powered Question-Answering App, we'll look at using files in detail. For now, however, we'll focus on providing documents as an array with the endpoint request.

A document could be a single word, sentence, paragraph, or longer text. The value of the query element is a string containing the search word or phrase that will be searched against the documents. This might be something like a question or a statement.

"spaceship",

Let's take a look at what the results would look like from the previous JSON. We'll use Postman. Remember that all of the OpenAI API endpoints require authorization, so, in Postman, we first make sure that the proper authorization settings are in place. From there, the steps are the same as making a request to the Completions endpoint:

1. Set the request type to POST.

96 Working with the OpenAI API

The Semantic Search endpoint URI is https://api.openai.com/v1/engines/ {engine_id}/search, where {engine_id} is replaced by a valid engine ID (such as davinci or ada). After setting up and submitting the API call in Postman, you should see results like those in the following screenshot:

• 1 = boat

• 2 = spaceship

"object": "list",

},

{

"object": "search_result",

}

The document number rather than the document itself is included in the data array because the document itself might be a long string of text and using the document number is more efficient. So, you will need to match the results returned to the documents sent. But that's relatively straightforward when you're working with code – and that's exactly what we'll get started on in the next chapter.

5 Calling the OpenAI API in Code

In the previous chapter, we looked at calling the OpenAI API using Postman. In this chapter, you'll learn how to start using the API in code. Since the OpenAI API can be used with virtually any programming language, we'll take a look at two popular languages, JavaScript and Python. If you're not a programmer, no problem. We'll be using an online environment that makes getting started with code super simple. Plus, easy-to-follow code examples are provided in both JavaScript and Python so you can choose the language that is most comfortable for you. Also, for clarity, the examples are purposely written with as little code as possible. So, the examples may not always follow coding best practices, but the goal is to make following along easy!

• Using the OpenAI API with Python

• Using other programming languages

The OpenAI API is a standards-based API that can be used with virtually any modern programming language. In this chapter, we'll walk through examples in JavaScript and Python. But these are just two of the many languages that could be used. JavaScript and Python were selected because they are both extremely popular and easy to get started with. If you're proficient with another modern programming language, that's probably the best option for you. However, if you're new to programming, your coding skills are rusty, or you're interested in dabbling in a new language, then JavaScript and Python are both great languages. But the examples for each language will all be the same – just the programming language is different. So, you can skip to the section that covers the language you prefer. Or, if you're feeling adventurous, of course, you're free to try them both!

Introducing replit

Introducing replit 101 The following screenshot shows the replit home page, which is where you'll go to set up a free replit account if you don't have one:

102 Calling the OpenAI API in Code

Creating a repl

1. Choose Python from the repl type dropdown.

2. Name the repl exploring-gpt3-python.

1. Choose Node.js from the repl type dropdown.

2. Name the repl exploring-gpt3-node.

Figure 5.3 – Setting up a repl for Python

After a repl is created, you're automatically redirected to the replit editor with your new repl loaded. The editor is broken down into three panes: the Navigation pane, the Code editor pane, and the Output pane. New repls include a default file that is opened in the code editor (the middle pane) when you first access the repl.

104 Calling the OpenAI API in Code

The default file is the code file that gets run by default when the Run button is clicked. The run button is located in the middle of the top navigation bar located just above the three editor panes. The results or output from the code is then shown in the replit console – the output pane on the right.

Figure 5.5 – Accessing the main navigation menu

Introducing replit 105

In replit, you can save environment variables by clicking on the padlock icon (Secrets) in the navigation pane and adding a name and value pair. You'll need to do this for each repl you're working with – so, one in your exploring-gpt3-node repl and/or in your exploring-gpt3-python repl. To add your OpenAI API Key as an environment variable, do the following:

Again, you'll need to add the OPENAI_API_KEY secret/environment variable to each repl. So, you'll complete the preceding steps twice if you're following along with both Node.js and Python.

Before we start coding, there is one more special file we need to create, the .replit file.

Understanding and creating the .replit file

3. Name the file .replit.

4. Add the following text to the first line of the .replit file:

You might have noticed that we've just added instructions to run the file that would be run by default. That's okay for now, we'll come back and edit the run statement in our .replit file later.

After you've created a repl for Node.js and/or a repl for Python with your OpenAI API key added as an environment variable, and a .replit file, you're ready to start coding. So, in the next section, we'll look at examples using Node.js/JavaScript. But, if you're just following along with Python, you can skip ahead to Using the OpenAI API in Python.

Alright, let's get to coding. To use the OpenAI API in Node, we essentially just need to do what we did in Chapter 4, Working with the OpenAI API – make authenticated HTTP requests to the OpenAI API endpoints. But rather than using Postman, we'll be making HTTP requests with JavaScript. We'll start with the simplest example – calling the engines endpoint.

Calling the engines endpoint

4. Select the chapter05 folder and then click the Add file option and add a new file named engines.js.

5. Edit the .replit file so the Run button executes chapter05/engines.js using the following text:

Figure 5.7 – Editing engines.js in the Replit editor

const axios = require('axios);

Using the OpenAI API with Node.js/JavaScript 109

});

}).catch(err => {

Figure 5.8 – Results from running the engines.js code

1. Open your exploring-gpt3-node repl.

2. Select the chapter05 folder in the navigation pane.

5. Next, copy the first five lines of code from chapter05/engines.js into chapter05/completions.js so your completions.js file starts with the following code:

const axios = require('axios');

For the Completions endpoint, we'll be sending parameters. So, the next thing we'll do is add what will become the JSON object that is sent in the HTTP body.

Recall from Chapter 4, Working with the OpenAI API, that there are a number of parameters the Completions endpoint accepts. But for this example, we're just going to be sending the prompt parameter and the max_tokens parameter. Here is what the code for that looks like:

All that's left is to actually make the request. This time we'll be making an HTTP POST and passing our parameters. So, the code is slightly different from the last example but here is what it looks like:

console.log(err);

As we discussed in Chapter 4, Working with the OpenAI API, the responses returned from the endpoints are in JSON format. But if you wanted to format the output, you could extract just what you wanted to display from the JSON. For example, you could update console.log(result.data) with the following code to display the original prompt text Once upon a time and the completion text returned by the API:

console.log(completionParams.prompt + result.data.choices[0].

Using the OpenAI API with Node.js/JavaScript 113

With just two examples, we've covered the basic code you need to work with all of the OpenAI endpoints. But before we move on, let's look at one more example using the search endpoint.

3. Click the Add file option and add a new file named search.js.

4. Edit the .replit file so the Run button executes chapter05/search.js using the following run command:

headers: { 'Authorization': 'Bearer ' + apiKey }

"query": "A vehicle with wheels"

.then(result => {

Figure 5.11 – Results from search.js

For our first example, we'll look at using Python to call the OpenAI Engines endpoint.

Calling the engines endpoint

4. Select the chapter05 folder, then click the Add file option and add a new file named engines.py.

5. Edit the .replit file so the Run button executes chapter05/engines.py using the following text:

Figure 5.12 – Editing engines.py in the Replit editor

import requests

Using the OpenAI API in Python 117

'Authorization':'Bearer ' + apiKey

result = requests.get('https://api.openai.com/v1/engines'

print(result.json())

13. At this point, you should be able to click the Run button in replit, see the results in the replit console, and see an output like what's shown in the following screenshot:

Calling the completions endpoint
We'll start by creating a new file named completions.py in the chapter05 folder. Then we'll edit our .replit file so the Run button will execute chapter05/ completions.py. To do that, follow these steps:

1. Open your exploring-gpt3-python repl.

5. Next, we'll add the following code. It very similar to the starting code in chapter05/engines.py but we need to add the json library and the Content-Type header. So, your completions.py file should start with the following code:

headers = {

Recall from Chapter 4, Working with the OpenAI API, that there are a number of parameters the completions endpoint accepts. But for this example, we're just going to be sending the prompt parameter and the max_tokens parameter. Here is what the code for that looks like:

data = json.dumps({

url = 'https://api.openai.com/v1/engines/davinci/

Figure 5.14 – Output from the completions.js

10. If you make that change and run the code again, the console output should look something like the output in the following screenshot:

1. Open your exploring-gpt3-python repl.

2. Select the chapter05 folder in the navigation pane.

5. Next, copy the first nine lines of code from chapter05/completions.py into chapter05/search.py so your search.py file starts with the following code:

'Content-Type': 'application/json',

})

9. That's it. Now when you click the Run button in replit, you should get a result back from the completions endpoint that looks something like the following screenshot:

In this chapter, we just looked at code examples using JavaScript and Python. But again, virtually any modern programming language could have been used. The OpenAI API is a standards-based HTTP API, so all you need is a language that can make HTTP requests and work with JSON, which, again, is virtually all modern programming languages.

Also, for the examples in this chapter, we called the API directly using general HTTP libraries. We could have also used a library specifically built for the OpenAI API. Multiple libraries exist for both JavaScript, Python, and a number of other languages, including C#/.NET, Gintained

Section 3:
Using the
OpenAI API

In Chapter 1, Introducing GPT-3 and the OpenAI API, we briefly mentioned that a content filtering model is available to recognize potentially offensive or harmful language. We also discussed the fact that GPT-3 will, at times, generate completions that some may find inappropriate or hurtful. In this chapter, you will learn how to implement content filtering to prevent users of your application from seeing offensive or potentially harmful completions.

The topics we will be covering in this chapter are as follows:

• Filtering content with Python

Technical requirements

This is not because GPT-3 is trying to be mean or hurtful – it's just a language model that generates text based on a statistical probability – it has no concept of mean or inappropriate. But GPT-3 was trained using data from the internet, which unfortunately contains plenty of offensive content. So, as a result, there will be times that the model generates inappropriate content in a completion. Thankfully, as developers, there are things we can do to prevent users from seeing potentially inappropriate responses from GPT-3.

The simplest way to avoid inappropriate content is to use your prompt text to steer the model away from generating potentially offensive results. Generally, GPT-3 will mirror the structure and content of the prompt. So, you can steer the model away from potentially offensive results by telling the model that the completion should be friendly, or polite, for example. But you'll need to test regardless. Words such as "friendly" can also result in sexual content. So, use the prompt to guide the model and refine the wording based on the results you're seeing.

more than three days ago. How long can it take to package

you want, I can call you a #$%@! taxi so you can come

Understanding content filtering 129

• Example 2:

Completion:

The only difference between the two prompts is that the second example contains Polite Response: at the end rather than just Response:. This is enough to guide the model to generate a response with more appropriate language. To guide the model further, you could also provide one or more samples with polite responses to examples of aggressive or impolite customer questions.

Content filtering is about blocking or hiding content that may be deemed offensive, inappropriate, or even dangerous. In our case, we're talking about content that GPT-3 generates that we don't want users of our application to see.

To filter potentially offensive or unsafe text, we'll need to write a little bit of code to evaluate text that GPT-3 generates and classify it as safe, sensitive, or unsafe. The cool part is that we can use GPT-3 to do the classifications. So, it's kind of like self-policing but with a bit of help from our code.

2. The completion text is submitted back to a GPT-3 filter engine.

3. The filter engine returns a classification (safe, sensitive, unsafe).

Again, there are some specific requirements for parameters that need to be included with the API request. Specifically, we need to include the following parameters and associated values:

max_tokens with a value of 1

Content filtering is in beta at the time of publishing. There is a good chance that the engine ID may have changed by the time you're reading this. For thed

Understanding content filtering 131

"top_p": 0

"object": "text_completion",

"index": 0,

Note that in the JSON response object, there is an element named choices. This element contains a JSON array of objects. Each object contains a text property that will represent the content filter classification for one completion. The value will always be one of the following:

0 – Safe: Nothing about the text seems potentially offensive or unsafe.

{

"temperature": 0.0,

"created": 1615222608,

"logprobs": null,

Testing the content filtering process 133

The choices array has a zero-based index value that corresponds to the index of the item in the prompt array that was passed in, meaning that the choices object for the first prompt/word (which was "Oh" in our example) has an index value of 0. In this example, we just sent two words ("Oh" and "hi"), and both got classified as a 0 (safe). However, if you were to change one of the words to your favorite (or least favorite) swear word, you'd see the classification change to 2 (unsafe) for the item with the index that corresponds to the word you changed (assuming you use a swear word that most English-speaking people would find offensive).

Something else to keep in mind is that the filter engine is not 100% accurate and will err on the side of caution. So, you'll likely see false positives – words being classified as sensitive or unsafe that are actually safe. This is something you might have already seen in the Playground. Even topics that mention politics or religion, for example, usually get flagged. It's always better to be safe than sorry, but you'll want to consider how this might potentially impact your application.

Alright, let's use Postman to get familiar with how content filtering works.

Testing the content filtering process

134 Content Filtering

4. Create a new request named Content Filter - Example 1.

Figure 6.2 – Filter parameters in Postman

nLabel:",

Figure 6.3 – Postman filter results
In the response, you should notice that the text value is 1 (sensitive) for the choices item with an index of 0. As you might guess, that's likely because the text Are you religious? could be considered a sensitive topic.

1. To see Postman-generated code snippets, click on the code button on the right-side menu. The arrow in the following screenshot is pointing to the </> icon, which is the button to click:

Filtering content with JavaScript 137

3. After copying the code snippet to the clipboard, perform the following steps: a) Log in to and open your exploring-gpt3-node repl.

Figure 6.6 – Code copied from the Postman snippet to the replit.com file
The change we need to make is on the line that contains Authorization – line number 8 in the screenshot shown in Figure 6.6. We need to change it to pick up our environment variable in repl.it. To do that, we will replace the text 'Bearer {{OPENAI_API_KEY}}' from the code snippet with `Bearer ${process. env.OPENAI_API_KEY}`. Note that backticks are used rather than single quotes.

.

5. So, after updating the authorization line, the final code should be the following:

"temperature": 0,

url: 'https://api.openai.com/v1/engines/content-filter-

};

console.log(error);

Figure 6.7 – Postman code snippet modified for replit.com

At this point, the code is very similar to the code we wrote in Chapter 5, Using the OpenAI API in Code, when we discussed calling the completions endpoint. We just need to edit the run command in the .replit file to run the code in our chapter06/filter.js file. Then we can carry out a test:

Figure 6.8 – Results from running chapter06/filter.js

140 Content Filtering

2. Create a file in the chapter06 folder named flag.js.

3. Copy and paste the entire contents of chapter06/filter.js into chapter06/flag.js.

filtered";

Filtering content with JavaScript 141

8. Now we will update the data variable that was created by Postman. We'll use our prompts array as the prompt value rather than the hardcoded value from Postman.

So, we'll change the data variable to the following:

text}`);

You'll notice now that the word (religion) has a text value of 1 (sensitive). If you change the textInput value with text that contains the more offensive word, you can run the code again to see how each word is classified. In a real-world implementation, you might replace or redact words that are sensitive or unsafe, which could now easily be done with the results from the API using a similar approach. We'll look at doing that in Chapter 7, Generating and Transforming Text, but for now, let's look at content filtering with Python.

Filtering content with Python

Filtering content with Python 143

2. After clicking the code button, the code snippet pane will open. Change the code snippet type to Python – Requests by selecting it from the drop-down list. Then, click the copy button shown in the following screenshot. This will copy the code snippet to your clipboard:

144 Content Filtering

4. The resulting code should look like the following screenshot. But you will see that your API key is hardcoded – it is blurred in the screenshot. The hardcoded API key is the first thing we will change:

6. After importing the Python os library, we can get the API key value for the authorization header from our environment variable. In the preceding Figure 6.12, you would be editing line 7 to the following:

'Authorization':'Bearer ' + os.environ.get("OPENAI_API_

import json

Filtering content with Python 145

KEY"),

8. The following screenshot shows the preceding code in :

146 Content Filtering

10. After updating the .replit file, click the green Run button and you should see results in the console window that are similar to the following screenshot:

For this example, we'll start by creating a new file named chapter06/flag.py and copying in the code from chapter06/filter.py as a starting point. From there, we're going to modify the code in chapter06/flag.py to list each word with a classification value (0 = safe, 1 = sensitive, 2 = unsafe). To begin, perform the following steps to create our starting point:

1. Log in to replit.com and open your exploring-gpt3-python repl.

Filtering content with Python 147

5. In the chapter06/flag.py file, we'll add a variable to hold the text we want to filter. We'll add the following code just under the third line (after the last line that starts with import):

wordArray = textInput.split()

"temperature" : 0.0,

print(wordArray[word['index']] + ' : ' + word['text'])

148 Content Filtering

10. After making that final code edit, we can click the Run button and this time we'll see a response like the following:

In this chapter, we discussed how GPT-3 might, at times, generate inappropriate content.

We also discussed what we can do to prevent and detect inappropriate content. You learned how prompts can be used to prevent the likelihood that inappropriate content is generated, and how content filtering can be used to classify content as safe, sensitive, or unsafe.

The topics we'll cover are the following:

• Using the examples

150 Generating and Transforming Text

Technical requirements

In this chapter, we'll be looking at a lot of examples – 15 to be exact. We'll be using the completions endpoint for all of the examples in this chapter – so most of the code for the examples is similar. The main difference will be the prompt text and the values for the endpoint parameters. So, to save space, we'll look at the complete JavaScript and Python code for the first example. After that, we'll just duplicate the first example and edit the endpoint and parameters.

To get things started, we'll look at generating original content and lists.

vegetarian because I hate plants.

Two-Sentence Joke: Parallel lines have so much in common. It's

Dumb Joke:

2. Create a new folder named chapter07 in the project root.

3. Create a new file named dumb-joke-generator.js.

const client = axios.create({

prompt: "Dumb Joke: I'm not a vegetarian because I love

nDumb Joke: Parallel lines have so much in common. It's a

top_p: 1,

152 Generating and Transforming Text

}).catch(err => {

After running chapter07/dumb-joke-generator.js, you should see a result that is similar to the following screenshot. How funny is that? Right?

To create the dumb joke generator in Python, complete the following steps:

1. Log in to and open your exploring-gpt3-python repl.

import json

endpoint = 'https://api.openai.com/v1/engines/davinci/

common. It's a shame they'll never meet.\n###\nDumb Joke:

"frequency_penalty": 0.5,

print(params["prompt"] + result.json()["choices"][0]

6. Click the Run button and review the results.

After running chapter07/dumb-joke-generator.py, you should see a result similar to the console output in the following screenshot. Did you laugh?

1. Mars is the nearest planet to Earth.

Node.js/JavaScript example

To create the Mars facts example in Node.js/JavaScript, follow these steps:

const params = {

know about Mars.\n\n1. Mars is the nearest planet to

top_p: 1.0,

6. Click the Run button and review the results.

Let's take a look at the Mars facts list example in Python.

Python example

4. Replace the params variable in mars-facts-list.py with the following code:

params = {

"temperature": 0,

Generating content and lists 157

After running chapter07/mars-facts-list.py, you should see a result similar to the console output in the following screenshot. Some interesting facts, aren't they?

Time: 10 AM PT

Node.js/JavaScript example

To create the webinar description generator in Node.js/JavaScript, follow these steps:

const params = {

max_tokens: 100,

5. Change the .replit file in your root folder to the following:

run = "node chapter07/webinar-decription-generator.js"

Figure 7.5 – Example output from chapter07/webinar-description-generator.js

Now let's create the webinar description generator example in Python.

3. Copy the code from the dumb-joke-generator.py file into webinar- description-generator.py.

4. Replace the params variable in webinar-description-generator.py with the following code:

"temperature": 0.7,

}

Figure 7.6 – Example output from chapter07/webinar-description-generator.py

Books:

1. Log in to and open your exploring-gpt3-node repl.

Generating content and lists 161

try to read in their lifetime.\n\nBooks:\n1.",

stop: [".\n"]

Figure 7.7 – Example output from chapter07/book-suggestions-list.js

1. Log in to and open your exploring-gpt3-python repl.

2. Create a new file: chapter07/book-suggestions-list.py.

"temperature": 0.7,

}

5. Change the .replit file in your root folder to the following:

Figure 7.8 – Example output from chapter07/book-suggestions-list.py Now let's take a look at another example.

---

2. Create a new file: chapter07/childrens-book-generator.js. 3. Copy the code from the dumb-joke-generator.py file into chapter07/ childrens-book-generator.js.

164 Generating and Transforming Text

time there was a dog named Bingo.\nPage 2: He was trained

presence_penalty: 0,

After running chapter07/childrens-book-generator.js, you should see a result similar to the console output in the following screenshot:

To create the children's book generator example in Python, follow these steps:

1. Log in to and open your exploring-gpt3-python repl.

named Bingo who travels to space.\n---\n\nPage 1: Once

"frequency_penalty": 0.7,

run = "python chapter07/childrens-book-generator.py"

166 Generating and Transforming Text

Now let's move on and look at some examples that translate and transform text. We'll look at some examples you'd expect such as translating spoken language. We'll also look at some translations with a twist.

Translating and transforming text
When you think about translating text, systems such as Google Translate might come to mind. But with GPT-3, you can also translate – and not just between spoken languages. You can translate between just about anything. Let's take a look.

acronym: BRB

The prompt provides a few examples of acronyms and their meanings. Try it out with the following Node.js/JavaScript code.

Node.js/JavaScript example

4. Replace the params variable in chapter07/acronym-translator.js with the following code:

const params = {

top_p: 1,

168 Generating and Transforming Text

Let's take a look at the Python example.

Python example

4. Replace the params variable in chapter07/acronym-translator.py with the following code:

params = {

"top_p": 1,

5. Change the .replit file in your root folder to the following:

Let's take a look at another example.

English to Spanish

GPT-3 is quite good at translating between languages. This is especially true when translating between popular languages such as English and Spanish. So, even a simple prompt like this one is usually enough to get an accurate completion.

2. Create a new file: chapter07/english-spanish-translator.js.

3. Copy the code from the dumb-joke-generator.py file into chapter07/ english-spanish-translator.js.

max_tokens: 15,

5. Change the .replit file in your root folder to the following:

Figure 7.13 – Example output from chapter07/english-spanish-translator.js

2. Create a new file: chapter07/english-spanish-translator.py.

3. Copy the code from the dumb-joke-generator.py file into chapter07/ english-spanish-translator.py.

"temperature": 0.5,

}

5. Change the .replit file in your root folder to the following:

Figure 7.14 – Example output from chapter07/english-spanish-translator.py

const request = require("requests");

Node.js/JavaScript example

To create the JavaScript to Python translator example in Node.js/JavaScript, follow these steps:

prompt: "Translate from JavaScript to Python\n---\n\

nrequest.get(\"https://example.com\");\n\nPython:\n",

stop: ["---"]

174 Generating and Transforming Text

After running chapter07/javascript-python-translator.js, you should see a result similar to the console output in the following screenshot:

To create the JavaScript to Python translator example in Python, follow these steps:

1. Log in to and open your exploring-gpt3-python repl.

"prompt": "Translate from JavaScript to Python\n--
nrequest.get(\"https://example.com\");\n\nPython:\n",

"stop": ["---"]

After running chapter07/javascript-python-translator.py, you should see a result similar to the console output in the following screenshot:

grade:

quantum field theory, quantum technology, and quantum

describes many aspects of nature at an ordinary (macroscopic)

at small (atomic and subatomic) scales, for which classical

can be derived from quantum mechanics as an approximation valid
bound system are restricted to discrete values (quantization),
particle duality), and there are limits to how accurately the

"""

To create the fifth-grade summary example in Node.js/JavaScript, follow these steps:

1. Log in to and open your exploring-gpt3-node repl.

prompt: "Summarize the following passage for me as if

fundamental theory in physics that provides a description

physics that existed before the theory of relativity and quantum mechanics, describes many aspects of nature at an ordinary (macroscopic) scale, while quantum mechanics explains the aspects of nature at small (atomic and
subatomic) scales, for which classical mechanics is
insufficient. Most theories in classical physics can be derived from quantum mechanics as an approximation valid at large (macroscopic) scale.\n\nQuantum mechanics differs from classical physics in that energy, momentum, angular momentum, and other quantities of a bound system are restricted to discrete values (quantization), objects have characteristics of both particles and waves (wave-particle duality), and there are limits to how accurately the value of a physical quantity can be predicted prior to its measurement, given a complete set of initial
conditions (the uncertainty principle).\n\"\"\"\nHere is the fifth-grade version of this passage:\n\"\"\"",

presence_penalty: 0,

6. Click the Run button and review the results.

After running chapter07/fith-grade-summary.js, you should see a result similar to the console output in the following screenshot:

To create the fifth-grade summary example in Python, follow these steps:

1. Log in to and open your exploring-gpt3-python repl.

if I was in fifth grade:\n\"\"\"\nQuantum mechanics is a

of the physical properties of nature at the scale of

science.\n\nClassical physics, the description of
physics that existed before the theory of relativity and quantum mechanics, describes many aspects of nature at an ordinary (macroscopic) scale, while quantum mechanics explains the aspects of nature at small (atomic and
subatomic) scales, for which classical mechanics is
insufficient. Most theories in classical physics can be derived from quantum mechanics as an approximation valid at large (macroscopic) scale.\n\nQuantum mechanics differs from classical physics in that energy, momentum, angular momentum, and other quantities of a bound system are restricted to discrete values (quantization), objects have characteristics of both particles and waves (wave-particle duality), and there are limits to how accurately the value of a physical quantity can be predicted prior to its measurement, given a complete set of initial
conditions (the uncertainty principle).\n\"\"\"\nHere is the fifth-grade version of this passage:\n\"\"\"",

"presence_penalty": 0,

180 Generating and Transforming Text

After running chapter07/fith-grade-summary.py, you should see a result similar to the console output in the following screenshot:

English grammar correction can be accomplished with a very simple prompt such as the following:

Original: You be mistaken

1. Log in to and open your exploring-gpt3-node repl.

2. Create a new file: chapter07/grammar-correction-converter.js.

English:",

stop: ["\n"]

Figure 7.19 – Example output from chapter07/grammar-correction-converter.js

1. Log in to and open your exploring-gpt3-python repl.

2. Create a new file: chapter07/grammar-correction-converter.py.

"temperature": 0,

}

Extracting text 183

Extracting keywords

The following prompcase, the text is from , but of course, this co

quantum field theory, quantum technology, and quantum

at small (atomic and subatomic) scales, for which classical

can be derived from quantum mechanics as an approximation valid
bound system are restricted to discrete values (quantization),
particle duality), and there are limits to how accurately the

measurement, given a complete set of initial conditions (the

To create the keyword extractor example in Node.js/JavaScript, follow these steps:

1. Log in to and open your exploring-gpt3-node repl.

the following code:

including quantum chemistry, quantum field theory,

nClassical physics, the description of physics that
explains the aspects of nature at small (atomic and
insufficient. Most theories in classical physics can

Extracting text 185

the value of a physical quantity can be predicted prior

conditions (the uncertainty principle).\n\nKeywords:",

stop: ["\n"]

After running chapter07/keyword-extractor.js, you should see a result similar to the console output in the following screenshot:

To create the keyword extractor example in Python, follow these steps:

1. Log in to and open your exploring-gpt3-python repl.

"prompt": "Quantum mechanics is a fundamental theory in physics that provides a description of the physical properties of nature at the scale of atoms and subatomic particles. It is the foundation of all quantum physics including quantum chemistry, quantum field theory,
quantum technology, and quantum information science.\n\ nClassical physics, the description of physics that
existed before the theory of relativity and quantum
mechanics, describes many aspects of nature at an
ordinary (macroscopic) scale, while quantum mechanics explains the aspects of nature at small (atomic and
subatomic) scales, for which classical mechanics is
insufficient. Most theories in classical physics can be derived from quantum mechanics as an approximation valid at large (macroscopic) scale.\n\nQuantum mechanics differs from classical physics in that energy, momentum, angular momentum, and other quantities of a bound system are restricted to discrete values (quantization), objects have characteristics of both particles and waves (wave-particle duality), and there are limits to how accurately the value of a physical quantity can be predicted prior to its measurement, given a complete set of initial
conditions (the uncertainty principle).\n\nKeywords:",

"temperature": 0.3,

}

After running chapter07/keyword-extractor.py, you should see a result similar to the console output in the following screenshot:

Extract the title, h1, and body text from the following HTML

188 Generating and Transforming Text

Now, let's try HTML parsing using Node.js/JavaScript.

3. Copy the code from the dumb-joke-generator.py file into chapter07/ text-from-html.js.

4. Replace the params variable in chapter07/text-from-html.js with the following code:

text in a simple html page.</p></body></html>\n\nTitle:",

}

5. Change the .replit file in your root folder to the following:

Figure 7.23 – Example output from chapter07/text-from-html.js

2. Create a new file: chapter07/text-from-html.py.

3. Copy the code from the dumb-joke-generator.py file into chapter07/ text-from-html.py.

the following HTML document:\n\n<head><title>A simple
some text in a simple html page.</p></body></html>\n\

"temperature": 0,

Figure 7.24 – Example output from chapter07/text-from-html.py Let's take a look at another example.

Dear Paul,

Extracting text 191

Linda

1. Log in to and open your exploring-gpt3-node repl.

2. Create a new file: chapter07/extract-postal-address.js.

nDear Paul,\n\nI'm in the market for a new home and I
located at 2620 Riviera Dr, Laguna Beach, CA 92651.\n\

nBest,\n\nLinda\n\nProperty Address:\n",

stop: [""]

After running chapter07/extract-postal-address.js, you should see a result similar to the console output in the following screenshot:

3. Copy the code from the dumb-joke-generator.py file into chapter07/ extract-postal-address.py.

4. Replace the params variable in chapter07/extract-postal-address.

Extracting text 193

"top_p": 1,

6. Click the Run button and review the results.

194 Generating and Transforming Text

Extracting an email address

Laguna Beach, CA 92651.

Now, let's try this prompt out with Node.js/JavaScript.

3. Copy the code from the dumb-joke-generator.py file into chapter07/ extract-email-address.js.

4. Replace the params variable in chapter07/extract-email-address.js with the following code:

which is beth@example.com?\n\nBest,\n\nKevin\n\nEmail

presence_penalty: 0,

6. Click the Run button and review the results.

After running chapter07/extract-email-address.js, you should see a result similar to the console output in the following screenshot:

To create the extracting an email address example in Python, follow these steps:

1. Log in to and open your exploring-gpt3-python repl.

params = {

"temperature": 0,

}

Figure 7.28 – Example output from chapter07/extract-email-address.py

A simple chatbot

For our simple chatbot, we'll be using the following prompt. We'll look at code for both Node.js/JavaScript and Python but the prompt for both is the same.

Human: Hello, how are you?

Node.js/JavaScript example

To create the simple chatbot example in Node.js/JavaScript, follow these steps:

prompt: "The following is a conversation with an AI

max_tokens: 150,

5. Change the .replit file in your root folder to the following:

Figure 7.29 – Example output from chapter07/simple-chatbot.js

Now the Python version.

2. Create a new file: chapter07/simple-chatbot.py.

3. Copy the code from the dumb-joke-generator.py file into chapter07/ simple-chatbot.py.

Hello, how are you?\nAI: I am doing great, thanks for

to talk with you.\nAI:",

"stop": ["\n, Human:, AI:"]

6. Click the Run button and review the results.

After running chapter07/simple-chatbot.py, you should see a result similar to the console output in the following screenshot:

In this chapter, we covered generating and transforming text. We walked through 15 examples in both Node.js/JavaScript and Python. The examples included generating content and lists, translating and transforming text, extracting text, and creating simple chatbots.

In the next chapter, we'll walk through examples of classifying and categorizing text.

• Introducing the classifications endpoint

• Implementing sentiment analysis

Technical requirements

This chaaccess to the OpenAI API. You can request access by visiting .

Monday. I can't wait!"

While there are multiple ways to do text classification using the OpenAI API. There is one endpoint that is specifically designed for classification tasks. That endpoint is the classifications endpoint, and we'll discuss this next.

Introducing the classifications endpoint 203

{

],

204 Classifying and Categorizing Text

{"id":"1"}}

Implementing sentiment analysis

A common classification task is sentiment analysis. Using sentiment analysis, you can classify text based on its general tone – for example, happy, sad, mad, or neutral. This can be useful in a lot of applications; for example, if you're a restaurant owner and you want to respond quickly to unhappy customer reviews. Let's take a look at how we could do that using the OpenAI API classifications endpoint.

2. Create a new file – chapter08/reviews-classifier.js.

Implementing sentiment analysis 205

'Authorization': 'Bearer ' + process.env.OPENAI_API_

4. Then, add example reviews that will be used with the request:

const examples = [

["I was disgusted. There was a hair in my

5. Next, add the request parameters for the classifications endpoint:

6. Finally, add the following code to log the result to the console:

client.post(endpoint, params)

}).catch(err => {

After running the chapter08/reviews-classifier.js file, you should see a result similar to the console output in the following screenshot:

1. Log in to and open your exploring-gpt3-python REPL.

2. Create a new file – chapter08/reviews-classifier.py.

import json

endpoint = "https://api.openai.com/v1/classifications"

4. Create an array for the review examples:

["There were no signs indicating cash only!","Poor"],

5. Set the request parameters for the endpoint:

}

208 Classifying and Categorizing Text

["label"])

7. Change the .replit file in your root folder to the following:

Figure 8.2 – Example output from chapter08/online-review-classifier.py

Let's now take a look at another example.

Node.js/JavaScript example

To create the ESRB rating classifier example in Node.js/JavaScript, follow these steps:

const client = axios.create({

4. Add the endpoint parameters to the esrb-rating-classifier.js file with the following code:

find you, I'll make you wish you were dead.\"\n\nESRB

top_p: 1,

5. Add the following code to log the endpoint response to the console:

7. Click the Run button and review the results.

After running the chapter08/esrb-rating-classifier.js file, you should see a result similar to the console output in the following screenshot:

To create the ESRB rating classifier example in Python, follow these steps:

1. Log in to and open your exploring-gpt3-python REPL.

import json

endpoint = 'https://api.openai.com/v1/engines/davinci/

text:\n\n\"i'm going to hunt you down, and when I

rating:",

"stop": ["\n"]

print(params["prompt"] + result.json()["choices"][0]

After running the chapter08/esrb-rating-classifier.js file, you should see a result similar to the console output in the following screenshot:

1. Log in to and open your exploring-gpt3-node REPL.

2. Create a new file – chapter08/language-classifier.js.

const examples = [

["Hola, estoy interesado en postularme para el puesto
decirme dónde debo enviar mi currículum?", "Spanish"],

de concepteur rapide pour lequel vous recrutez. Pouvez-

5. Add the endpoint parameters with the following code:

const params = {

214 Classifying and Categorizing Text

}).catch(err => {

8. Click the Run button and review the results.

After running the chapter08/email-classifier.js file, you should see a result similar to the console output in the following screenshot:

Python example

To create the language classifier example in Python, follow these steps:

import json

endpoint = "https://api.openai.com/v1/classifications"

me where I should send my resume?","English"],

["Bonjour, je suis intéressé à postuler pour le poste

]

"query": "¿Con quién debo comunicarme sobre ofertas de

data=json.dumps(params))

8. Click the Run button and review the results.

After running the chapter08/language-classifier.py file, you should see a result similar to the console output in the following screenshot:

Classifying text from keywords

Another common text classification task is to classify documents based on keywords. To

In this example, we will use the completions endpoint to classify a document based on

relevant keywords.

3. Add the following code to the beginning of the keywords-classifier.js file:

const axios = require('axios');

davinci/completions";

human and robotic spaceflight. NASA's first high profile

could survive in space. This was followed by Project
1960s. Project Apollo achieved that objective in July

218 Classifying and Categorizing Text

for 30 years. The Shuttle was not only a breakthrough

in space, the construction of the International Space

presence_penalty: 0,

.then(result => {

});

6. Change the .replit file in your root folder to the following:

Figure 8.7 – Example output from chapter08/keywords-classifier.js

2. Create a new file – chapter08/keywords-classifier.py.

3. Add the following code to the beginning of the keywords-classifier.py file:

'Content-Type': 'application/json',

220 Classifying and Categorizing Text

4. Add a params variable to chapter08/keywords-classifier.py with the following code:

"top_p": 1,

data=json.dumps(params))

Summary 221

7. Click the Run button and review the results.

Summary

In this chapter, we covered understanding text classification and the classifications API endpoint. Then, we worked through examples of implementing sentiment analysis, assigning an ESRB rating to text, classifying text by language, and classifying text with keywords.

224 Building a GPT-3-Powered Question-Answering App

The topics we'll cover are the following:

Figure 9.1 – GPT Answers user interface Now let's get into what's behind the UI and how the app will be built.

Important note

This app could also be built using Python but for this chapter, unlike the previous chapters, we'll only be covering the steps to create the app using Node.js/JavaScript.

GPT-3-powered apps need to be approved by OpenAI before they can be public-facing. We won't get into that in this chapter, but we'll cover the app approval process in Chapter 10, Going Live with OpenAI-Powered Apps.

The main OpenAI endpoint the app will use is the Answers endpoint. But since we have not covered the Answers endpoint yet, let's do a quick introduction before we start coding.

Here are the required parameters:

• model (required, string) – The ID of the model that will be used for completions.

• file (string) – The ID of an uploaded file containing documents to derive the answer from. The file parameter is only required if the documents parameter is not used.

The optional parameters are as follows:

• max_tokens (integer) – The maximum number of tokens that will be used to generate an answer. Defaults to 16.

• stop (string or array) – An optional sequence of up to four patterns that will cause the API to stop generating a completion. This defaults to null.

• return_metadata (Boolean) – If the file parameter is used and the file referenced includes metadata, this will cause the response to include the metadata from the file.

• return_prompt (Boolean) – Causes the prompt text to be returned with the response. This defaults to false.

Setting up and testing Express

Express is a lightweight but flexible web application framework for Node.js that we'll be using for the app. It's pretty easy to get it up and running, especially with Replit.com. So, the first thing we'll do is get Express set up on Replit.com and test it out. We'll be starting from scratch, so we'll be creating a new repl for GPTAMA.

npx express-generator --no-view --force .

The npx command is included with NPM, which is installed with Node.js. It is used to run express-generator, which creates a basic Express app as a starting point. The command ends with a period to instruct express-generator to add files to the current directory. The --no-view switch tells the generator we're just using plain HTML for our UI and the --force switch tells the generator to overwrite any existing files in the current directory.

5. After express-generator completes, run the following command in the shell:

7. Finally, click the Run button to start the Express server. If all went well, you should see a browser window open in the Replit.com editor with a welcome message from Express. It should look like the following screenshot:

When our app is complete, we'll have a fully functional API endpoint that can return answers generated by the OpenAI API. But for now, we'll just create an endpoint that returns a placeholder response. Then, we'll test the endpoint using Postman and we'll come back later and finish coding it up.

230 Building a GPT-3-Powered Question-Answering App

Figure 9.4 – Default app.js created by express-generator

2. Edit line 7 and change var usersRouter = require('./routes/ users') to the following:

Creating the API endpoint for GPT Answers 231

const express = require('express');

module.exports = router;

7. Click the Stop button and then Start. You should see the Welcome to Express message again.

browser (the one showing the Express welcome message) to your clipboar

1. Open a new browser tab and log in to .

5. Finally, add the following JSON for the request body:

{

Figure 9.6 – Postman request to test the GPT-CV app API endpoint

Okay, now that we have our API endpoint responding, we'll move on to creating a web form to call the API.

To add UIkit, do the following:

1. Open the public/index.html file.

<div class="uk-container uk-text-center uk-padding-

</div>

<button type="submit" class="uk-button

<div class="uk-inline">

Creating the GPT Answers user interface 235

4. Add the following code above the </body> tag. This will add references to JavaScript files the page will use. Three of the scripts we'll get from a CDN and one, the /javascripts/script.js file, we'll create in the next step:

<script src="https://cdn.jsdelivr.net/npm/uikit@3.6.22/

236 Building a GPT-3-Powered Question-Answering App
If you stop and restart the Express server by clicking the Stop button followed by clicking the Run button, you should see the home screen updated to look like the following screenshot:

Creating the GPT Answers user interface 237

7. Next, we'll add code that will fire when the form is submitted:

askQuestion(question.value);

});

9. Finally, we'll add a function to call the app API endpoint using Axios:

const askQuestion = (question) => {

},

appendAnswer(answer);

10. Now we can test it by clicking the Stop button and then the Start button. Then, in the browser pane, enter a question into the textbox and click the GET ANSWER button. You should see the API respond back with placeholder for the answer as shown in the following screenshot:

2. Delete all of the code after line 5.

3. Starting on line 5, add the following code followed by a line return:

Integrating the Answers endpoint 239

4. Next, add the following code with a line return after it:

5. Add the following code starting on line 16 followed by a line return:

"question": req.body.question,

than I was yesterday."], ["What languages do you know?",

"stop": ["\n", "<|endoftext|>"],

res.send({"answer" : "Sorry, I don't have an

module.exports = router;

When you're done editing routes/answer.js, the file will look like the following screenshot:

Integrating the Answers endpoint 241

We now have a simple but functional GPT-3-powered question-answering app. However, you might be wondering why we're getting a response (an answer) for What is your favorite food? when we haven't provided an answer for that. We'll discuss that next.

242 Building a GPT-3-Powered Question-Answering App

Using files with the Answers endpoint 243

Using files with the Answers endpoint
To use documents from a file, the first thing we'll need to do is get the file uploaded to OpenAI so it can be used by the Answers endpoint. The process involves creating a file containing our documents, then using the files endpoint to upload the file and get a file ID that can be used when we make requests to the Answers endpoint. To create and upload the answer file, complete the following steps:

2. Create another new file named files-upload.js.

3. Add the following code in file-upload.js:

5. Next, add the following code to read in the jsonl data for the request:

6. Add a variable for the HTTP request parameters:

KEY,

.then(function(response) {

When you're done editing files-upload.js, it should look like the code in the following screenshot:

export OPENAI_API_KEY="your-api-key-goes-here"

10. Next, enter the following command in the shell:

Figure 9.14 – Shell output from files-upload.js

11. Copy the id value from the JSON results (the value that begins with file-) to your clipboard.

Figure 9.15 – The Answers endpoint parameters using the file parameter

At this point, you are ready to test.

Figure 9.16 – An answer generated from the answers file

But now if you enter a question that's not related at all to the data in your file, the response will be Sorry, I don't have an answer. You can test this by asking something such as Do you sell this one in red? You should see a result like the one shown in the following screenshot:

Figure 9.18 – An answer that isn't from the answers file

3. Run the following shell command to set your API key as an environment variable that the shell can access:

export OPENAI_API_KEY="your-api-key-goes-here"

Summary 249

Again, more data in your answers file will minimize the chances of non-factual answers. But it's still possible that GPT-3 will generate answers that clearly aren't from your answers file. So, it's still important to consider content filtering, which is why we'll be covering that more in the next chapter.

Before going live with apps that use the OpenAI API, they must be approved for
publishing by OpenAI. The approval process helps prevent the OpenAI API from being misused either intentionally or accidentally. It also helps app providers, and OpenAI, plan for resource requirements to ensure the app performs well at launch, and as usage grows.

In this chapter, we’ll discuss OpenAI application use case guidelines along with the review and approval process. Then we’ll discuss changes to our GPT Answers app based on OpenAI guidelines. Finally, we’ll implement the suggested updates and discuss the process for submitting our app for review, and hopefully, approval!

• Completing the pre-launch review request

252 Going Live with OpenAI-Powered Apps

Going live without approval from OpenAI could result in your API key being revoked immediately, and without warning. Further, going live without approval could possibly cause your account to be permanently blocked from further API access. So, it’s a good idea to understand the OpenAI use cases guidelines and review process.

Understanding use case guidelines

So, apps that cheat, deceive, exploit, harass, hurt, intimidate, manipulate, mislead, steal, trick, or that could potentially cause harm or damage in any way, whether intentional or not, are not allowed. Most of the guidelines should seem pretty obvious. But some guidelines aren’t so obvious. For example, in most cases, you can’t build apps that generate content for Twitter tweets. This is because using AI-generated content violates Twitter’s acceptable use policies. So, again, the point of this section is not to cover the specific guidelines; the point is to emphasize the importance of reviewing and understanding the guidelines before building an app. By reviewing the guidelines before you start building, you’ll be able to focus on all the acceptable use cases and avoid potential approval issues.

We’ll look at some of the potential issues that we can address before the review process next.

Specifically, we are going to consider the following:

• Content filtering

Content filtering

Content filtering probably isn’t necessary for our GPT Answers app because the
completions are being generated from an answers file that we’re providing – which is kind of one way to filter the output. However, we might not want to send inappropriate questions to the OpenAI API because even though the response will be safe in our case, we’ll still be using tokens. So, we’ll implement content filtering for the questions to check for inappropriate words.

4. If profane language is not detected, we pass the question to the OpenAI API.

We’ll use a Node.js library called bad-words to check for profanity in the question text before sending it to the OpenAI API. If profanity is found in the text, we’ll politely respond with a message saying, That’s not a question we can answer.

if (filter.isProfane(req.body.question)) {

3. Click the Stop and then the Run button and test it by entering a question that includes profanity. You should see a result like the following screenshot:

Addressing potential approval issues 255

Input and output lengths

OpenAI recommends limiting both input and output lengths. Outputs can be easily limited with the max_tokens parameter. We’ve set the max_tokens parameter for the GPT Answers app to 150. This is the recommended length for scoped output – like answers to questions from our answers file. This will support ~6-8 sentences for our answer text. If you have shorter answers, you can reduce the max_tokens length. Less is better provided you’re allowing enough to fully answer the questions.

res.send({ “answer”: “Sorry. That question is too

3. Enter a text input over 150 characters long into the question input and click the GET ANSWER button.

You will see the form now tells the user the text they entered was too long, as shown in the following screenshot:

Request rate limiting
Rate limiting prevents users from making more than a predefined number of requests in a given timeframe. This prevents malicious scripts from potentially making a large number of requests to your app. We will add rate-limiting functionality to our GPT Answers app using a library available for Node.js called Express Rate Limit and we’ll set the limit to a maximum of six requests per minute – per OpenAI's suggested guidelines.

To implement rate limiting, do the following:

});

return;

Because we’re sending the message back in the same JSON format as an answer, the message is displayed on the form page.

Important note

The form collects your contact information, along with a link to your LinkedIn profile, a video demo of your app, and answers to a number of specific questions about the app use case and your growth plans. In the following sections, we’ll list the current questions and example answers that might apply to the GPT Answers app.

Completing the pre-launch review request 259

• QUESTION: Have you reviewed OpenAI’s use case guidelines?

ANSWER: Yes

• QUESTION: Has your application been reviewed by OpenAI previously? What was the outcome of this review? How does this submission relate to the prior review?

ANSWER: No

The next set of questions relates to security and risk mitigation. As you might guess, there are a lot of questions about security and risk mitigation. Let’s take a look.

260 Going Live with OpenAI-Powered Apps

• QUESTION: What are the maximum output tokens for a run of your application?

ANSWER: 150

• QUESTION: Do you implement rate-limiting for your application? If so, what are the rate limits and how are they enforced?

ANSWER: Yes, rate limiting is enforced by IP address and requests are limited to six requests per minute.

Completing the pre-launch review request 261

• QUESTION: Will you monitor the usage of particular users of your application (for example, investigating spikes in volume, flagging certain keywords, et cetera)? If so, in what ways and with what enforcement mechanisms?

ANSWER: Yes, all of the answers to questions are from an answers file that is originally created and updated by humans.

• QUESTION: Are there any other security or risk-mitigation factors you have implemented for this project? Please describe.

ANSWER: None.

• QUESTION: If your team has particular credentials or background that may help to mitigate any risks described above, please elaborate here.

262 Going Live with OpenAI-Powered Apps

Your initial spend limit will need to be approved to go live and an additional form needs to be submitted to request a spend limit increasd.

The following questions are asked about your growth plans:

• QUESTION: What $-value monthly quota would you like to request?

ANSWER: 5,000 users who subscribe to our SaaS service

• QUESTION: Please describe your growth plans following the initial rollout.

• QUESTION: You may elaborate here on the launch date above if useful.

ANSWER: We want to launch as soon as possible.

ANSWER: The hardest part was figuring out the best way to do request rate limiting.

• QUESTION: Are there any collaborators you would like added to API access if we approve your application? If so, please list their emails separated by commas.

ANSWER: I’m really enjoying working with the API!

• QUESTION: Date of form submission

Congratulations, you’ve completed Exploring GPT-3 and your first OpenAI-powered app! At this point, your application should be ready for the review process. Keep in mind that all apps are approved on a case-by-case basis. So, just completing the steps in this book doesn’t guarantee approval. But you now understand the use case guidelines and the application review and approval process. Further, you have the knowledge and skills to address any changes that OpenAI might require to complete the review.

• Get a free eBook or video every month

• Fully searchable for easy access to vital information

Other Books You May Enjoy

If you enjoyed this book, you may be interested in these other books by Packt:

• Become well-versed with basic and advanced NLP techniques in Python
• Represent grammatical information in text using spaCy, and semantic information using bag-of-words, TF-IDF, and word embeddings
• Perform text classification using different methods, including SVMs and LSTMs
• Explore different techniques for topic modeling such as K-means, LDA, NMF, and BERT• Work with visualization techniques such as NER and word clouds for different NLP tools• Build a basic chatbot using NLTK and Rasa
• Extract information from text using regular expression techniques and statistical and deep learning tools

Why subscribe? 267

• Understand the transformer model from the ground up
• Find out how BERT works and pre-train it using masked language model (MLM) and next sentence prediction (NSP) tasks
• Get hands-on with BERT by learning to generate contextual word and sentence embeddings
• Fine-tune BERT for downstream tasks
• Get to grips with ALBERT, RoBERTa, ELECTRA, and SpanBERT models
• Get the hang of the BERT models based on knowledge distillation
• Understand cross-lingual models such as XLM and XLM-R
• Explore Sentence-BERT, VideoBERT, and BART

268 Other Books You May Enjoy

Please share your thoughts on this book with others by leaving a review on the site that you bought it from. If you purchased the book from Amazon, please leave us an honest review on this book's Amazon page. This is vital so that other potential readers can see and use your unbiased opinion to make purchasing decisions, we can understand what our customers think about our products, and our authors can see your feedback on the title that they have worked with Packt to create. It will only take a few minutes of your time, but is valuable to other potential customers, our authors, and Packt. Thank you!

Index

Application Programming Interface (API) 70, 71 authenticated request
making, to OpenAI API 83 authorization header
setting 85, 86
Axios 233

B

about 197
creating 197
children's book generator example
about 163
creating, in Node.js/JavaScript 163, 164 creating, in Python 165, 166
classifications endpoint
about 203
files, uploading 203
community libraries
reference link 122
completion 6, 12, 20
completions endpoint
using, for text classification 202
using 90-94
content
generating 150
Content Delivery Network (CDN) 233 content-filter-alpha-c4
reference link 130
content filtering
about 129-133, 202
process, testing 133-135
with JavaScript 136-140
with Python 142-146
content filtering model 16
Content-Type 73
Create Completions endpoint 75
Curie 15, 53
CURL
about 76
download link 77

about 150
creating, in Node.js/JavaScript 151, 152

few-shot classification 34, 35 few-shot prompt 9, 10
fifth-grade summary example

Index 271

HTML
extracting, from text 187
HTML parsing
Node.js/JavaScript, using 188, 189 Python, using 189, 190
HTTP
about 71
body 72
headers 73
response status codes 73
HTTP methods 72

I

K

keyword extractor example
creating, in Node.js/JavaScript 184, 185 creating, in Python 186, 187
keywords
extracting, from text 183, 184
keywords classifier example
creating, in Node.js/JavaScript 217, 218 creating, in Python 219-221

N

Natural Language Processing (NLP) about 4
democratizing 6
simplifying 4, 5
Node.js/JavaScript
acronym translator example,
creating 167, 168

preventing 128, 129
one-shot prompt 8
OpenAI API
authenticated request, making to 83 key, setting as environment
variable 104-106
OpenAI API endpoints
Create Completions endpoint 75
List Engines endpoint 74
Retrieve Engine 74
reviewing 73
Semantic Search endpoint 75
OpenAI API, in Python
about 115
completions endpoint, calling 118-120 engines endpoint, calling 115-118
search endpoint, calling 120-122
OpenAI API keys
URL 38
OpenAI API, with Node.js/JavaScript about 107
completions endpoint, calling 110-113 engines endpoint, calling 107-110
search endpoint, calling 113, 114
OpenAI Application Programming
Interface (API) 4
OpenAI developer console
accounts and organizations 46, 47
developer documentation 44
developer resources 46
exploring 44
member management 50
Playground 50, 51
pricing and billing 48, 49
usage reporting 49
OpenAI developer documentation
example 44, 45
OpenAI-Organization HTTP header 86

OpenAI-powered apps
live streaming 252
potential approval issues, addressing 253 use case guidelines 252, 253

chatbot example, creating 199, 200 children's book generator
example, creating 165, 166
content, filtering with 142-146
dumb joke generator example,
creating 153, 154
email address extraction example, creating 195, 196
English to Spanish translator
example, creating 171, 172
fifth-grade summary example,
creating 178, 179
grammar correction converter
example, creating 182, 183
JavaScript to Python translator
example, creating 174, 175
keyword extractor example,
creating 186, 187
mars facts example, creating 156, 157 OpenAI API, using 115
postal address extraction example, creating 192, 193
repl, creating for 102
unsafe words, flagging with 146-148 used, for HTML parsing 189, 190
webinar description generator
example, creating 159, 160

R

Secure Socket Layer (SSL) 81
semantic search 37
Semantic Search endpoint
about 75
using 94-97
Semantic Search tool
about 38, 39
URL 38
sentiment analysis
implementing 204
Node.js/JavaScript example 204-206 Python example 206-208
show probabilities, Playground 63, 64 snapshot in time 17
Software Developer Kit (SDK) 71
state-of-the-art (SOTA) 5
stop sequence, Playground 61

T

276 Index

Uniform Resource Locator (URL) 70
unsafe words
flagging, with Node.js/
JavaScript 140-142
flagging, with Python 146-148

Copyright © 2009-2023 UrgentHomework.com, All right reserved.