Build an AI-Powered Fullstack Next.js App, v3

Computing a Sentiment Score

Scott Moss

Scott Moss

Superfilter AI
Build an AI-Powered Fullstack Next.js App, v3

Check out a free preview of the full Build an AI-Powered Fullstack Next.js App, v3 course

The "Computing a Sentiment Score" Lesson is part of the full, Build an AI-Powered Fullstack Next.js App, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Scott adds a sentiment score to each entry to plot the user's mood over time. The score is added to the StructuredOutputParser, and the Prisma schema is updated. The question branch can be used as a starting point for this lesson.

Preview
Close

Transcript from the "Computing a Sentiment Score" Lesson

[00:00:00]
>> Now what we wanna do is we wanna create a page that shows your history plotted over time on the sentiment, the score of your sentiment. So a few things that we have to do in order for that to happen. First, we need to figure out what your sentiment score is on a certain entry, which we're not, we're not doing that right now.

[00:00:17]
So we can go back and do that. The next thing we need to do is figure out, we gotta save that somewhere. So we gotta update the database so we can save that. And then the last thing is creating a UI where we can plot that. And then we also wanna calculate your average sentiment, so you can understand how you've been.

[00:00:36]
Eventually, if you want it, you could put that on a sliding time range. Pick a date range, see how you felt within this date surface. You can even go further and be like, okay, within this range show me certain words that I use surface, common words that I use or understand what I was saying in that moment in time.

[00:00:55]
You can get really deep [LAUGH] to understand yourself, as long as you're honest with the stuff that you write. Okay, let's do that. So first thing first, let's make a new page inside of our dashboard, sibling to journal, and that will be called history. So I'll make a new folder called history and inside that folder, make a page.tsx.

[00:01:23]
And then history is just going to be a page, a server function. So we'll do that, default History like that. Okay, that's it, nothing crazy. Just a div. I'm just gonna add that to my links in my sidebar so I can actually click on it. There we go.

[00:02:07]
Okay, so now we should be able to go to history and it shows us history. Okay, first things first is we're gonna figure out how everybody knows what they are. So let's think about it. We could go down into AI and make some new functionality, just to get the sentiment.

[00:02:27]
But we know that we're using this analyze function every time an entry is saved and updated. We can just add another property on our schema to ask for a sentiment score. And then we can save that. So I think that's what I'll do. So I'll go to our schema in this parser here, and I'll just say, sentimentScore.

[00:02:54]
And I'll say this is a z.number And describe I do remember, I think this one was a little struggling, so I'm probably gonna use the one I used before. Which for this one, I described it as sentiment of the text and rated on a scale from -10 to 10, where -10 is extremely negative, 0 is neutral, and 10 is extremely positive.

[00:03:28]
It actually took me, or actually I asked GPT to write this for me. Because everyone that I wrote would not give me what I was looking for. It was like sometimes and not sometimes. It was just, this is not what I'm looking for. So I was like, I told you, it's like, this is what I'm trying to do, write me a prompt for this.

[00:03:45]
And it was like, cool, here you go. And I just paste it in and it works. And I was like, dang, that's Meta [LAUGH]. Cool, all right, so I'll paste that in. Now we need to go update everywhere we're doing this to account for it. We were mostly doing spread in most of the places, so we probably should be good in most places.

[00:04:11]
But let's just go double check. We definitely got to update the schema for sure. But let's go check in the API calls. Those two ones on the update and create. Yep, we're spreading here. So if it gets created we should be good. If you want to update it, it should be good.

[00:04:27]
And then on this one, we're also doing spread. So we should be good. We shouldn't have to change anything here cuz we're not manually adding any properties, everything's spread. Are we returning it though, from the parse? So let's see. Yeah, we're just returning the whole thing, so that should be good too.

[00:04:45]
That's all we really gotta do is just update our schema. So we'll come down here, make a new one called sentimentScore and this will be a Float. If you do an integer it'll break cuz you're gonna get back decimals. It's not gonna be whole numbers from -10 to 10, it'll be floats from -10 to 10.

[00:05:16]
And I'm gonna add a default here so our database doesn't freak out. So default, this is going to be 0, as in neutral. And then npx prisma db push should be good cuz we added a default here. Good to go, I'm gonna stop my server, start it again.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now