![Build an AI-Powered Fullstack Next.js App, v3](https://static.frontendmasters.com/assets/courses/2023-08-12-fullstack-app-next-v3/thumb.webp)
Check out a free preview of the full Build an AI-Powered Fullstack Next.js App, v3 course
The "Creating Consistent Prompts" 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 demonstrates how to provide a prompt that returns consistent results. Giving an example of the data to be returned from the LLM helps create more consistency and reduces the amount of parsing needed back in the application.
Transcript from the "Creating Consistent Prompts" Lesson
[00:00:00]
>> We're getting somewhere. So now let's experiment with like, let's give the AI a fake journal entry and then ask it to perform an analysis on it and see if we can. Because I mean, the goal here, right, is to be able to, we're trying to get back this from the AI.
[00:00:17]
We wanna give you a journal entry, give me back the mood, the summary, the color, and whether it's negative or not. So a couple things there. One, we got to figure out how to ask it that. Two, we got to figure out how to make sure it's consistent.
[00:00:29]
Because that's the thing about AIs. There's a little bit of randomness into it that keeps it from giving you the same thing every time. I can give it the same problem and get back. If I ask it again right now, I will get back something different. The code might be the same this might be different maybe this H1 is down here it's not always gonna be the same.
[00:00:46]
So how do we get structured output from something that's completely random what do we have to ask it to get it? And then how do we keep all that working so it's like It's very hard to get that to work and LinkedIn comes in. So let's see if we can try to do some of this stuff and then we'll bring in some of the Liang Cheng tools.
[00:01:06]
So one is the prompt. So let's say we have some fake entry here. Well, I guess this will be the entry will be the prompt., how can we try to get some of that back? So the first thing is prompt engineering. Prompt engineering is like, let's craft the best prompts, let's think about it.
[00:01:27]
And that way if we write a good enough prompt, it will give us back the results that we want, right? So what I can do in that case is I'll head back to where I'm writing in my prompts here and I'll say I'm going to give you a journal entry.
[00:01:50]
I want you to analyze it, For a few things. I need the mood, A summary, what the subject is, And a color representing, The mood. You need to respond back. With formatted JSON like so. And then I can give it an example of what that JSON should look like.
[00:02:34]
So I can say, yeah, it should look like, name, or I'm sorry mood, there's like the mood and then there's, like what else I say subject. And subject stacks, and then you have color. It is like trying to give a person instructions negative. And then finally I can say I'm just going to the next line and I'm gonna say, entry like this.
[00:03:17]
And then I'm gonna write the journal entry. Today was a really great day. I finally was able to grab that pair of shoes I have been dying to get. All right, so that's my entry, so we can save that. And let's see what it gives me back. And then we'll run it a few times to see, like, how it differs.
[00:03:47]
Maybe it'll give us that back exactly every time and we'll just get lucky. Maybe it won't, by the way. Either way, it won't be this is still missing some things like this doesn't even know what type these things are like negative supposed to be a Boolean. So like it's just so many things here.
[00:04:02]
So let's run this refresh this it's gonna take a minute. So you can see here I personally did get back an object here so it did work for me actually did it twice I got back mood happy subject getting new shoes. Color yellow, negative false. So, for me, it worked out pretty good twice.
[00:04:25]
And actually, when I look at both of them, it gave me back the same thing there. So that was actually really cool. The problem is you're not gonna get this every single time. Sometimes when I run this, it might be something else. The other problem is, it's not gonna give me, now it's giving me error at the same time.
[00:04:48]
It's like, yeah, watch, I'll give it to you and say watch. Check this out. The other thing is it doesn't know the types of things. This false is false, it's supposed to be like a Boolean and not a string. So then you might ask yourself, yeah, I'll just go in here and be like, I can like give this thing I mean, you're basically giving it a schema at this point.
[00:05:07]
You're like, if I give it a schema of what it should look like and I ask it that it should only respond with this, I should get that back every time. But sometimes you might see something like, it'll respond and instead of just giving you back this object that you want, it'll put a word before it's like, here's your answer.
[00:05:23]
And that word in front of it will mess it up because you can't parse that. So it's like little things like that you might not get actually wrote a pretty damn good prompt here. So it's like, yeah, I understand it. And that's just cuz [LAUGH] I do this so much.
[00:05:34]
But most people probably wouldn't know how to do this.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops