From the course: Building Apps with AI Tools: ChatGPT, Semantic Kernel, and Langchain

Sentiment analysis

- [Instructor] Let's build our first ChatGPT-4 app. A sentiment classification bot. I'm going to check out the code for my branch for this video. Note that we have a start and end code for this one. So, let's check out Branch: 02_01b. Now, let's open up sentiment_classifier and go to line 11. Let's add a prompt to our system. "You are a sentiment classification bot, print out if the user is happy or sad." So, this is the prompt that we'll be using to classify our responses. Let's fix this typo. Now, let's scroll back. Now, the user prompt, I'm going to enter what the user says. So let's say, "I'm really enjoying my day today." Great. Now, let's go ahead and hit Run. And here we go. We got the response and the classification. The user is happy. Now, we're going to add two things to our app. The first one is the ability to read input from the terminal. And the second is the ability to ask multiple questions. So, starting off with the ability to capture input. I'm going to create a new variable called user_input and I'm going to read from the console using the input method. Let's go ahead and tell the user what they should enter. So let's say, "Enter a phrase and we'll tell you if it's happy or sad." Okay. Now, we're going to take this user_input and put it into our user prompt. So, let's go ahead and run that. So, here we go, we see, "Enter a phrase and we'll tell you if it's happy or sad." And let's enter, "I'm doing great." There we go. We got a response. The user is happy. Now, let's add the second part. The ability to ask multiple questions. So, let's go ahead and add a while loop here at the top. Going to type in while True. Now, let's go ahead and tab everything that we had before. Now, let's go ahead and run it. "I'm happy." Get a response. "I'm sad." "I'm feeling great." And here we go. We're asking the user multiple questions and outputting the GPT-4 response. Now, let's add one more thing where we'll add an if statement here to check if the user said, quit or exit, just so they'll be able to exit our app. Now, let's type below the user input, if user_input == "exit" or user_input == "quit". We're going to break the loop. Okay, now I'll add a \n at the end, so it's a little easier to read and we'll run it one more time. Okay. I'm sad. I'm happy. And let's hit quit. And there we have it. We built a simple app. A sentiment bot that uses our terminal to connect to GPT-4 and provide the user with a response.

Contents