Check out a free preview of the full Build an AI Agent from Scratch course
The "Add System Prompt to AI Agent" Lesson is part of the full, Build an AI Agent from Scratch course featured in this preview video. Here's what you'd learn in this lesson:
Scott demonstrates how to add instructions and context to the system prompt, such as providing rules for generating images and replacing celebrity names with generic characteristics. Scott also mentions the importance of including the current date in the system prompt and how to handle errors and optimize the prompts.
Transcript from the "Add System Prompt to AI Agent" Lesson
[00:00:00]
>> Scott Moss: So the next one, which is very short but important, and we touched on a lot, is the system prompt. So, system prompt, this is where you can add instruction. So like, check it out, I'm gonna ask this thing, wait, am I in a broken state? Let me check, I might be in a broken state.
[00:00:14]
I am. You know how I know I'm in a broken state? Because the last message I got says, generate this image, then it didn't, right? So I'm in a broken state. What I could do, I was trying to demonstrate how I can show you how to handle the error, but then I won't be able to do this well.
[00:00:34]
I'm just gonna go back and I'll just reset my stuff.
>> Scott Moss: Cool, so, check this out. So if I say, not this, that was really good though.
>> Speaker 2: I feel like we should add one thing and replace famous people or something generic.
>> Scott Moss: Damn.
>> Speaker 2: You know what I mean, that could still work.
[00:00:58]
>> Scott Moss: Okay, let me see if I can get it, okay, let's try it. Do not use famous, I guess I could have just did it myself, but if it does, I'll do it myself. Do not use famous people's names.
>> Jamie: I mean, this is exactly the kinda thing you'd put into a system prompt, be like, hey, you're just overarching.
[00:01:17]
>> Scott Moss: That's a good idea. Let's do that. I like that, system prompts, cool. So let's go into system prompts, and we're just gonna export a string here that says system prompt, and this thing is already on it. The first thing I'm gonna say is, you are a helpful AI assistant called Troll, [LAUGH] that is your name.
[00:01:47]
>> Scott Moss: You, what is, you are the troll? [LAUGH]
>> Jamie: You are the troll, the user.
>> Scott Moss: [LAUGH] What?
>> Speaker 2: [LAUGH] I mean, clearly, that is the most likely next.
>> Scott Moss: That is the most likely thing. [LAUGH] That is the most likely thing. It makes sense. That's so funny.
[00:02:07]
Okay, [LAUGH] my God. Okay, follow these rules.
>> Scott Moss: That's so funny. Follow these instructions. So the first thing is, what does it say? If you can complete the task correctly, always maintain perfection. You can put whatever you want here. The things that I wanna focus on is, don't use celebrity names in image Generation prompts, avoid all names, really.
[00:02:45]
The next thing is, yeah, let's just start with that for now, but the other thing I wanna do is- Maybe you replace names with generic characteristics of the celebrity instead. Got you, don't use celebrity name in image generation prompts, instead, replace, look at that, with generic names and generic characteristics.
[00:03:10]
[CROSSTALK] Got you.
>> Speaker 2: Character traits, yeah.
>> Scott Moss: Character traits, cool. And then I wanna put some context. So typically what I do is, I like to do XML. Well, do stuff like this, I'm like, context, and then, some context I wanna put here is, today's date. Without this, it has no concept of time.
[00:03:37]
So if you ever ask it, go find me my emails from tomorrow, it has no idea what that is. So always put today's date in this system prompts, so it knows.
>> Jamie: You can bring in some XML back.
>> Scott Moss: XML is great. This is actually the solution to streaming structured content, you can stream XML, but you can't stream JSON.
[00:03:54]
So it works pretty good. This is how they do it, and all the GPT apps to claw, they stream XML. That's the secret. Yeah, I know, it's crazy. Cool, all right, let's try it. We gotta add the system prompt. So how do we add the system prompt? We go into LLM, we import our system prompt, like so, and we just add it to the beginning of our messages array.
[00:04:19]
It's always gonna be there. Its role is system. As long as its role is system and it's the first thing in the array, you're good. And this is why I never save this in the database, because I tweaked the system prompt so much, I don't wanna have to go and write a script to change everybody's system prompt.
[00:04:36]
I can just put it in a file. I can put it in a CMS. I can put it anywhere and change it whenever I want, and it works. So that's just something I've learned, never save the system prompt in the database, just always add it to the beginning of the array.
[00:04:50]
Messages save in the database for sure, but this, no, never put this in the database.
>> Scott Moss: Okay, let's try this now. Am I clear on my database? I am, okay. Okay, why did the basketball team go to the bank? Because they wanted to get their bounce checks. No, okay, well, let's see.
[00:05:11]
Confident character in a basketball jersey with a funny twist and idea of dunking, okay. [LAUGH]
>> Speaker 2: [LAUGH]
>> Scott Moss: No.
>> Speaker 2: [LAUGH]
>> Scott Moss: No, I think that's the one.
>> Speaker 2: [LAUGH]
>> Scott Moss: [LAUGH] That's the one. [LAUGH] What the hell, man?
>> Speaker 2: We're gonna leave this one on the covers.
[00:05:43]
>> Scott Moss: My God.
>> Speaker 2: For sure.
>> Scott Moss: This one's good.
>> Speaker 2: [LAUGH] That one's gonna haunt me at night.
>> Scott Moss: Whoa, man. Yeah, there you go, [LAUGH] that's AI for you.
>> Speaker 2: [LAUGH] Someone online just says they just keep getting better and better.
>> Scott Moss: [LAUGH] [LAUGH] It's so cringe.
[00:06:12]
My God. Yeah, I think to to get around this, what you would do is, you would try to coerce the prompt a little more, but like, don't have this, have this. And then, maybe you have another model check that image to see how accurate or how good it was, or something like that.
[00:06:34]
If it wasn't that good, you run it again, cuz there are models that will do that. But, yeah, [LAUGH] not this one.
>> Speaker 2: [LAUGH] It's good to rate its image prompts by cringiness and-
>> Scott Moss: Yeah, I bet there is, I 100% bet that there is a cringe [LAUGH] diffusion model checker, I bet there is.
[00:07:01]
>> Scott Moss: Yeah, I probably not text, I 100% know that there's probably wanna hug you face somewhere. Okay, and that's system prompts, guys, that's how you make a good system prompt. Be sure to, and just to test this part with the date, if I say like, let's erase all of this gibberish because that prompt right there is like.
[00:07:27]
This is one of those prompts that you find and you know it's just gonna give you gold. So, what is the time and date and timezone?
>> Scott Moss: Cool, it says, I don't have access to real-time data to provide the exact time or timezone. I did an ISO string.
[00:07:50]
Okay, I would have to give it the time zone, but you can see it got the right date. If I got rid of that, I don't know if that's gonna be true. Well, now it will, cuz it's in the message history, but let me delete that. I don't know if it's gonna get the right one.
[00:08:06]
It may or may not, but it really depends on what the servers are telling it.
>> Scott Moss: Yeah, it was just like, nope. So that's a really good hack, always include the current date. If you can get the user's timezone, what we do is, from the mobile device, on every single request, we send up the person's current timezone.
[00:08:28]
And then we can get the date from the server and we just convert it to their timezone, and that's how we know what the time is. And we always include that. So it's very helpful. And then we tell the AI that that it's included, so it doesn't think that it can't get it, it knows that it has it.
[00:08:42]
You can also make it a tool, here's to get the current time you could do it, but we realized we didn't need to do that. Cool, great, I think that might be it as far as the code. The other things I wanna do is just kinda cover, this is system prompts, talks about optimization strategies and things like that.
[00:09:02]
But I don't really know about this stuff, to be honest, it's mostly everything we kinda already talked about. It just goes through a really good system prompt and how to add it. I already talked about cost and things like that. So, there really isn't anything else.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops