Check out a free preview of the full Build an AI-Powered Fullstack Next.js App, v3 course
The "Prompt Template" 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 uses the PromptTemplate object from the Langchain library to create the payload sent to the OpenAI API. The prompt template includes the structured output from the Zod library along with the instructions for the LLM.
Transcript from the "Prompt Template" Lesson
[00:00:00]
>> Now that we have the parser, we basically are going to create a prompt template. We're gonna do some interpolation and things like that, but we're not gonna do it ourself because, come on now, we're not gonna do that. LangChain has a bunch of stuff that we can use to create that, specifically something called a prompt template.
[00:00:21]
So we're gonna use a prompt template and we're gonna give it a guiding a prompt and then we're gonna mix that in with the schema that we just created. And then that way we'll get back the full prompt that we wanna use combined with the actual journal entry as well.
[00:00:38]
So that's what we're gonna do. So we just need to bring in the PromptTemplate from LangChain and we can get started with that. So I'm just gonna make a function down here that says getPrompt, like this. And yeah, all getPrompt is gonna do is just take in some content from a journal entry.
[00:01:00]
And then we can import a prompt from, let me see if I can figure this import out without looking, prompts. Maybe it's just prompts. It is. Yeah, so it's just prompts. So then we wanna get PromptTemplate, From langchain/prompts. So the first thing we wanna do is we kinda wanna parse this Zod schema that we created.
[00:01:31]
Or I should say format it. And I'm just gonna say, const formatted_instructions = the parser that we just created, and then you can say getFormatInstructions. If you were to log this, it's basically taking this entire schema and doing the equivalent of what I did here. I will log it so you can see.
[00:02:00]
And you'll see just how much better it is at doing it than what I did. And you're like, okay, that's cool, cuz that's all it's doing is just creating that for us so we don't have to do it. Okay, we got that. And then the next thing we wanna do here, as you can see we just want to make this PromptTemplate.
[00:02:17]
There's three things that go into a PromptTemplate, the actual template. So this is like the steering instructions of like this is what you are, these are the instructions. And then we have input variables for interpolation. This will be the entry, and then any partial variables which will be our format instructions.
[00:02:34]
So, let's do that. So we'll say prompts = new PromptTemplate, like this, Template. So in this template, I'm just gonna copy the one I had cuz I actually had to do a lot of trial and error to get this template to be good. So I highly recommend you copying this one too.
[00:03:00]
And I'll copy and then I'll read it, so you can see why I did it this way. So this one is basically what I'm saying here. I think it's just freaking out because of TypeScript, yeah, okay. So the template that I have here is I'm saying analyze the following journal entry, follow the instructions and format your response to match the format instructions, no matter what.
[00:03:26]
And I'm screaming at it. And then on the new line, I'm going to interpolate the formatted instructions, which are gonna be this, actually I'll just change the name of that to format_instructions, and then the actual content, the entry itself, the journal entry. So we're gonna put that in there, yes.
[00:03:43]
>> Two things, like 30, the word instructions, there's a typo there kinda at the end of line 30. Sorry the first part of line 30, [LAUGH] follow the instructions and format.
>> Yeah, right, yeah. Yeah, so this is a good time to talk about that. So yes, probably wanna fix that, also the AI.
[00:04:07]
AI does a really good job of like correcting your grammar for you. So you could do typos, it'll probably understand it. But if you do too many typos, it probably will affect the output. But yes, thank you.
>> And then line 20, kind of the same deal. There's a hexadecimal color code that-
[00:04:24]
>> That-
>> Represent, but as you said, probably would have been fixed.
>> Yep, thank you. Cool, any questions on this so far?
>> The no matter what, in your background, have you played around with alternates and that is the best way to get it to do the thing or is that?
[00:04:48]
>> Yeah, that's the one?
>> Yeah, that's the one.
>> Nice.
>> That's a pro tip right there. Remember that, put at the end of all your prompts, no matter what, or you're fired, right? And then it'd be like, I'm sorry. And then you feel bad, [LAUGH] cool.
[00:05:06]
And then we have input variables. In this case it'll just be entry. And we will interpolate that later. And then we have partial variables, which in this case will be, it will be an object with the format instruction. So it just needs to be the same name as you have here.
[00:05:27]
So I have format_instructions, so I'm gonna put format_instructions. So we're interpolating this, we're interpolating the partial value variables now, we'll interpolate the entry variable later. Okay, and then from there, now we're actually gonna call the format, which is asynchronous. And this is where we actually interpolate the entry.
[00:05:57]
And then we'll get back our actual prompt. And then we'll log it in. And then we'll check it out. So I'm just gonna call this the input, and I'm just gonna await the prompt.format, like this. Make this async at the top. And from here I'm just gonna say entry, cuz that's what we have for our interpolation.
[00:06:24]
If I can type, there we go. And then contents go here. And always return the input, but I'll log it first. There we go. And then for analyze, instead of taking in a prompt, we'll actually take in some content from an entry. No, we'll just take in the entry itself, and then we'll create a new prompt.
[00:06:56]
So we'll say input = await getPrompt, and it just takes in that content, like so. And then I'll pass in the input here.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops