The Stop Sequence GPT-3

The Stop Sequence GPT-3 [Full Code With Examples]

The Stop sequence within a GPT-3 call is one of the most critical pieces of the query, as it can help you save tokens due to more efficient use.

If you need 100 benefits back from your query, why would you pay for GPT-3 to generate 101 benefits? 

While this is a substantial benefit to efficiently using GPT-3, it’s not even my top reason.

The main reason I use them is that stop sequences help clean up the text before receiving it back, ensuring that you don’t have any unnecessary words or phrases in your response.

Stop sequences are essential in getting the most out of gpt-3, allowing you to refine your queries and get the best results possible. 

By using stop sequences, you can quickly ensure that your queries only return relevant results instead of extraneous information that may not be useful.

This post will explain the basics of the stop sequence and provide examples of applying them for maximum efficiency and accuracy (I hope you like Python)

Below are examples of some stop sequences.

stoplight

Stop Sequence Examples in GPT-3 (Python Code)

For this section, I’m going to assume you’re writing code in Python, but these stop sequences will work in whatever language you choose


Stop Sequence For List Prompts

A common way that you’ll use GPT-3 is with lists.

In the example below, we create a hard stop for GPT-3 using the “6.” command.

This means once GPT-3 generates the sixth benefit in our list, it will return us the result.

This is a great way to ensure you’re not paying for extra benefits.

We use six here, but any number can work; remember, you’ll get one less than the number you put.

import openai

openai.api_key = 'key-here'

p = f'''Write me a 6 benefits of dogs in a list format'''

# generate the response
response = openai.Completion.create(
      engine="davinci-instruct-beta-v3",
      prompt=p,
      temperature=.7,
      max_tokens=500,
      top_p=1,
      frequency_penalty=0,
      presence_penalty=0,
      stop=["6."]
    )

# grab our text from the repsonse
text = response['choices'][0]['text']


print(text+'\n')

example ai prompt 1


Stop Sequence For Question and Answer

There is nothing worse than when GPT-3 goes off on a tangent.

You just wanted a simple response to your query and received a book of random information, and what’s even worse is you paid for that response.

So, when it comes to question and answer, I always put in a stop sequence to keep GPT-3 from going way off on the answer.

In the example below, we use “Human:” and “Pirate:” to keep GPT-3 from continuing the dialog.

This is perfect as it means GPT-3 will simply answer the question and not continue crafting a made-up scenario outside the one we established.

import openai

openai.api_key = 'key-here'

p = f'''
The following is a discussion between a pirate and a human

Pirate: Give me your gold!
Human: Never! You'll have to come take it!

Pirate:
'''

# generate the response
response = openai.Completion.create(
      engine="davinci-instruct-beta-v3",
      prompt=p,
      temperature=.7,
      max_tokens=500,
      top_p=1,
      frequency_penalty=0,
      presence_penalty=0,
      stop=["Human:", "Pirate:"]
    )

# grab our text from the repsonse
text = response['choices'][0]['text']


print(text+'\n')

example ai prompt 2

Stop Sequence For Sentences and Paragraphs

Last but not least, we can hone down our responses to being one line or one block of information.

Let’s say you do not want four lines of text for a response; we can provide 

GPT-3 “.” to keep it from continuing in sentences.

This works for other slices of information.

Let’s say you want a good paragraph of information but don’t want to continue for multiple paragraphs.

We can provide GPT-3 the stop sequence “\n” to keep it from starting another line.

import openai

openai.api_key = 'key-here'

p = f'''Write a paragraph about the benefits of dogs, speak in-depth as if you are a genius'''

# generate the response
response = openai.Completion.create(
      engine="davinci-instruct-beta-v3",
      prompt=p,
      temperature=.7,
      max_tokens=500,
      top_p=1,
      frequency_penalty=0,
      presence_penalty=0,
      stop=['.']
    )

# grab our text from the repsonse
text = response['choices'][0]['text']


print(text+'\n')

example ai prompt 3

Advantages Of Using A Stop Sequence

As we’ve seen above, the stop sequence has many advantages.  

The advantages of using the stop sequence include greater accuracy in understanding what you receive back from OpenAi, increased clarity on your response, greater control over text formatting, and faster response times. 

This means that users can have more confidence in their interactions with GPT-3 since they know that the conversation is being conducted correctly and efficiently.

Finally, and probably most importantly, it saves you money.

saving money in jar

 

Other Articles In Our GPT-3 Series:

GPT-3 is pretty confusing.

To combat this, we have a full-fledged series that will help you understand it deeper.

Those articles can be found here:

Stewart Kaplan

Leave a Reply

Your email address will not be published. Required fields are marked *