The world’s leading publication for data science, AI, and ML professionals.

Prompt Engineering 101: Zero, One, and Few-Shot Prompting

An introduction to a basic prompt engineering strategy

Image by Alexandra_Koch from Pixabay
Image by Alexandra_Koch from Pixabay

Introduction

Despite their seemingly supernatural capabilities, LLMs are ultimately predictive models that simply predict the next word in the sequence of words based on the provided context.

As such, their performances don’t just hinge on the vast volumes of data they are trained with; they also depend heavily on the context provided through the users’ inputs.

Frequent users of LLM-powered chatbots are aware of the importance of context. Without sufficient context, chatbots, whether they are publicly available services (e.g., ChatGPT) or custom-made LLM products, will struggle to carry out the more complex instructions.

Here, we delve into one of the most basic strategies used to guide LLMs to answer prompts correctly: providing context within user prompts. This is often carried out with 3 different methods: zero-shot prompting, one-shot prompting, and few-shot prompting.


Zero-Shot Prompting

If you’ve interacted with an LLM-powered chatbot before, you’ve likely already used zero-shot prompting unwittingly. Zero-shot prompting entails relying solely on an LLM’s pre-trained information to answer a given user prompt.

For instance, let’s suppose that we used ChatGPT as a sentiment classifier. An input with zero-shot prompting would look like this:

User Prompt: "Determine the sentiment of this sentence. Sentence: ‘This basketball has a lot of weight’."

ChatGPT: "The sentiment of the sentence ‘This basketball has a lot of weight’ is neutral. It does not convey a positive or negative sentiment but rather provides factual information about the weight of the basketball."

When provided the query, the LLM, with no additional context, evaluates this sentence to be neutral.

While this method can usually yield satisfactory responses, it may be insufficient when the complexity of the question is high or when the criteria for the answer are different from the norm.

For such cases, users can consider providing additional context in their prompts.


One-Shot Prompting

One-shot prompting entails providing an example in the user prompt to guide the LLM towards to desired generated response.

In the previous example, ChatGPT deemed that the sentence, "This basketball has a lot of weight" is neutral.

However, certain users (e.g., retailers selling basketballs) may deem heavy basketballs to be faulty products. Thus, they would need to ensure that their LLM gives the term "heavy" a negative connotation.

A simple way to do so would be to provide an example in the query itself.

Prompt:

"Sentence: "This basketball is easy to carry".

Answer: "The sentiment of this sentence is positive".

Determine the sentiment of the sentence.

Sentence: "This basketball has a lot of weight"

Answer:"

ChatGPT: ‘The sentiment of the sentence "This basketball has a lot of weight" is negative. It expresses that the basketball is heavy, which is typically seen as a negative attribute when it comes to ease of use or play.’

This time, ChatGPT deems the same sentence to be negative, since it uses the provided example as a reference.


Few-Shot Prompting

Users will not always be able to provide all of the context that the LLM will need to function properly with just one example.

For such cases, they can provide more context with few-shot prompting, which entails providing multiple examples in the user prompt.

In the previous examples, we explored the use of ChatGPT to classify sentence sentiments as "positive" and "negative".

However, what if we wanted a wider range of sentiment labels? For instance, we could use the labels "extremely positive", "positive", "somewhat positive", "neutral", "somewhat negative", "negative", and "extremely negative".

To enable ChatGPT (or another LLM product) to incorporate specific grading criteria, few-shot prompting can be an effective strategy.

User Prompt:

"Sentence: This basketball fits in my bag.

Sentiment: somewhat positive.

Sentence: This basketball deflates after just one game!

Sentiment: extremely negative.

Sentence: This basketball is orange.

Sentiment: neutral.

Sentence: This basketball’s feel is a bit better than I am used to.

Sentiment:"

ChatGPT: "Somewhat Positive"

By introducing ChatGPT to the new range of sentiment labels, ChatGPT can be "taught" to classify sentences with those same labels.


Why We Can’t Use Infinite Examples

At this point, it’s tempting to consider including as many examples as possible in user prompts to guarantee a successful outcome.

After all, the more context, the better, right?

While that could be the case, LLMs are constrained by a limited number of useable tokens. Every LLM has a context window, the maximum number of tokens that the model can take as input.

The number of tokens afforded to users depends on the underlying model itself. Oftentimes, the specific figures can be found in the documentation.

For instance, the token limits of OpenAI’s GPT-3.5 models are detailed in the OpenAI API documentation.

Token Limits for GPT-3.5 Models (Source: Models - OpenAI API)
Token Limits for GPT-3.5 Models (Source: Models – OpenAI API)

Users exhaust tokens when writing user prompts and, as a result, don’t have the bandwidth to bombard LLMs with countless examples.

Instead, they have to figure out how much context an LLM will need to perform a given task adequately, and then determine how they can provide that context in as few tokens as possible.


Key Takeaways

Photo by Prateek Katyal on Unsplash
Photo by Prateek Katyal on Unsplash

Overall, users can yield satisfactory results from their LLM without providing additional context within their prompts since these models are already trained with high volumes of data.

However, for cases where prompts are complex or where responses have to meet unique criteria, users can benefit from including one or more examples in their prompts (i.e., one-shot prompting, few-shot prompting), provided that they do so with the allotted number of tokens.

Whether you are using a ready-made chatbot or are building your own custom-made LLM product, using zero-shot, one-shot, and few-shot prompting is a simple yet effective way to obtain adequate results.

Thank you for reading!


Related Articles