So we've seen that the text completions models are pretty flexible, and can do much more than answer questions.
In this lesson, we'll take this a step further to discuss classification tasks.
Classification tasks involve assigning a label to a piece of information.
This can be identification, such as identifying the language used in a piece of text,
categorization, such as sorting geographical locations into countries and US states, or
even classifying a statement's sentiment, that is, whether it sounds positive or negative.
The Completions endpoint can be used for all of these tasks, providing the model has
sufficient knowledge of the subject area and that the prompt contains the necessary context.
Let's use the Completions endpoint to categorize these animals.
Printing the response, we see that the model categorized the animals into mammals, fish, and reptiles.
This might be what we were looking for, but there's an almost infinite number of ways
to categorize something, so it's best practice to state the desired categories in the prompt.
We can update the prompt to categorize animals into those with
and without fur,
and the model responds with the desired categories.
Let's look at an example of classifying sentiment.
Let's say we received a number of online reviews to our restaurant, and we
want to extract the sentiment, as numbers one to five, for further analysis.
We can write a prompt with the instruction to classify sentiment, and a
list of statements,
pass it to the Completions endpoint,
and print the response.
The model does a good job; however, because we didn't specify how to
classify the categories, the model didn't realize we actually wanted a numerical output.
We can update our prompt so the model has the context on what the categories should be.
The updated response contains a reasonable numerical representation of the sentiment of each statement.
The prompts we've used so far are examples of a so-called zero-shot prompts,
where we haven't provided any examples in the prompt for the model to learn from.
For many use cases, this may be fine, but for more complex cases where the model may not have a
good understanding of the subject matter or task, we may need to provide examples for it to learn from.
If one example is provided in the
prompt, it's called one-shot prompting, and for more than one, it's called few-shot prompting.
Providing examples in the prompt for the model to learn from is called
in-context learning, as the model is learning from the context we're providing.
Let's return to sentiment analysis to give these prompting techniques a go.
We're again dealing with restaurant reviews, but this time, we want the sentiment to be more specific.
To do this, we provide one example in the prompt, clearly separating the input and output.
Sending this to the Completions endpoint and printing the result
shows that the model accurately
analyzed the statement, but that it didn't provide any more detail than when we used a zero-shot prompt.
In more complex cases, one example is not enough, and we'll need to
experiment to determine the number of shots the model needs for the use case.
Let's extend the prompt to provide three examples.
Running the same code again,
we see that the model was much more specific.
Hopefully you're starting to grasp just how many problems can be solved
using the OpenAI API, and how important prompts are to obtaining good results.
Time to practice!