-: One obvious thing you might like to do when using ChatGPT
is to get it to classify data for you
into specific different types of labels.
For example, if you're looking at reviews,
maybe that's whether the review was positive
or whether the review was negative.
Let's have a look at how we can do this
using LangChain and the ChatGPT API.
First, you're gonna have to install LangChain
and Langchain OpenAI.
And then what we're gonna do is import the chat models,
ChatOpenAI, SystemMessage, and HumanMessage.
After running that as well,
you'll also need to import your OPENAI_API_KEY
if you haven't done so already.
As you'll see here,
we've got a list of different types of reviews
where each review is based on a movie.
It says things like I love or I hate this movie,
or this movie's a waste of time.
And so what we wanna do is build a classifier using ChatGPT
that allows us to get structured labeled data
from this review text.
So we've got our Python list,
which is a list of dictionaries.
We can instantiate our ChatOpenAI model,
and then we can store our classifications in a Python list.
We'll loop over each individual review
and then we'll just print
that we're gonna classify that review.
And then inside each individual review,
we're gonna have a system prompt which says,
"You're responsible for the classification of movie reviews.
Please classify the following review
as positive or negative.
You must use only the following words:
negative or positive."
And then we've got the HumanMessage
which contains the content of specifically what that review.
We're also saying if the content
that we get back from the LM is not negative or positive,
then we're throwing an error
because we've got an invalid classification.
And that's something we'd look at fixing
in terms of on an individual basis.
And so I can run this
and you'll see that we're actually generating
different types of classifications
for all of these types of reviews.
And then we can go and have a look
at the response of this afterwards.
So it's gonna print this out so you can see
it's looping through each individual classification.
And then what we're gonna have at the end of this
is a way for us to classify different reviews.
So we finally finished
and we've got all of our different reviews.
One thing we could also do is we could import pandas
and then we could have a date frame
and we could zip up the reviews with the classifications.
And so what you'll see here now
is we are just gonna import pandas
and this is gonna make a data frame
where for each of the reviews,
we're then gonna zip that up with the classifications
that came back.
And now you can see we've got an easy
nice pandas data frame that we can save,
so I'm gonna save that,
and then we've got this data frame
that we can then manipulate.
And then we can say, this is the review
and this is the classification.
And we could do things like count the number of,
count the number of these classifications,
so we can see that we had a total of 32 classifications.
We could do various different types of operation in pandas.
But the point I want to stress across to you is
think about the way that you can use ChatGPT
to label some of your data.
And you can use this on text, you can use this on JSON data,
anything that you want in terms of that.
And it's also possible to use the Vision API
with this type of information as well.
So think about using ChatGPT as a classification engine,
which you can use to get structured labeled data.