Discover how to enhance your AI chat assistant by incorporating specific FAQ content directly into your system prompts. Learn effective prompt engineering techniques to enable your AI to accurately answer questions about products, services, and even job opportunities.
Key Insights
- Integrate FAQ content directly into your OpenAI API prompts by loading external text files using Python's
open()
function, significantly enhancing your AI assistant's response accuracy and relevance. - Utilize Markdown-formatted FAQs to improve readability and data parsing, allowing the AI to clearly interpret headers, bullet points, and formatting for effective response generation.
- Augmenting the AI's general knowledge with company-specific details, such as product offerings, physical locations, and available job openings, empowers it to provide precise and contextually accurate customer support.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
Hi, welcome back to Lesson 11. This is the final lesson in our AI chat assistant project using Flask, JavaScript, and the OpenAI API. My name is Brian McLean.
Let's get started. So in the last lesson, as you recall, we got a two-way search going with the AI, which is kind of neat. Why don't we take that for a spin? I'll take that for a spin, and we'll trash some of these.
Here we go. Okay, let's take the little chat for a spin. Okay, so we can type here, do you have fruit trees for sale? And it'll answer and say yes.
Sure. Now, the thing is, though, it doesn't really know beyond what it can glean. It actually doesn't know.
It doesn't even know this text. It's just inferring from the prompt, and the prompt is really only giving it a little bit of information. It's not really too specific.
You are a Customer Service Assistant for Greenleaf Tree Nursery, a tree nursery website that sells sapling seeds and gardening accessories. So how does it really know that it has fruit trees—apple, pear, cherry, peach, and plum? How does it know that? It kind of doesn't. It's just inferring that.
So it's not hallucinating, but it is making that up. It doesn't really know. And if we drill further—do you have any job openings or something?—it's not going to know everything that you might find on the website.
Do you have any job openings? It probably doesn't know that. It might make something up. I'm sorry, I don't have access.
See, it doesn't know about that. So what we will do in the next lesson here is we want to provide some context. So right now, the AI just has its vast, generalized LLM (large language model) knowledge base, which is considerable.
So it can talk to you all day about the best way to plant apple trees. Can I plant or can I grow apple trees in the shade? And it'll say, not really. Apple trees generally prefer full sun to thrive.
So that's generalized knowledge. We didn't tell it that. It just knows.
But if we wanted to provide more specific information that it could use to augment its generalized knowledge, that'd be great. So what we're going to do in this lesson is provide a full FAQ in our initial prompt. And that FAQ is a text file that we have that we're going to load up into Python and then save as a text variable—a string—and then CONCATENATE that string onto the prompt, the initial system prompt.
So here we are in Lesson 11: AI Prompt Engineering with Product FAQ. We're going to load into Python an FAQ file of Greenleaf-specific info. We're going to add the FAQ text to the conversation being sent to the OpenAI API.
We're going to add it to the initial prompt—the initial system prompt. We're going to add the FAQ. We're going to test the FAQ knowledge by asking the AI chat assistant about specific stuff.
Ask the AI chat assistant questions to test its FAQ knowledge—its FAQ awareness. So in this lesson, we will load FAQ content from an external file into our Python Flask app using Python's open function. It's actually this with open thing that's really neat to read the file.
So one, let's load the FAQ file into Python. We're going to open the FAQ.txt file and check it out first. The FAQ contains info about Greenleaf's products, services, and even lists several job openings with the company.
So that's a big chunk of text. If you're doing this for your own website, you can go into your website and grab any text. You can either scrape it—you know, you could use an automated Pythonic way of scraping a website, which is outside the scope of this course—or you can just get your hands-on text by copying it out of the website. Or if you're, you know, a Web Admin, you might just have the text files anyway.
So here's that file. Let's close this text, FAQ text. It's in Markdown.
It's.txt, but it's written in Markdown format. So it's easier for the AI to parse if it knows that this is a header. So in Markdown, this double hashtag is analogous to an H2 on the web.
If we render—let's actually change the file type to.md—we can render this there. So notice it's rendering now.
So one hashtag means extra large, like H1. Two hashtags is H2 size. Three hashtags is H3 size.
Double asterisks surrounding text bolds it. Single asterisks around text italicize it. A dash makes a bullet-pointed list.
So it's a real lean and effective way of providing formatting to just text. So this little Markdown syntax is standing in for HTML tags, basically. And ChatGPT likes it if text is submitted to it as Markdown.
It's easier for it to parse it and understand it and not just see it as a blob of text—to see it as bullet-pointed lists, to see it as big headers and smaller headers. We want ChatGPT, the OpenAI API model, to be able to consume our FAQ in this format. So here's careers—joining the team.
I added this recently. All kinds of job openings. We're going to ask, once this is uploaded, we're going to ask the AI if there are any job openings.
And right now it has no idea, right? I'm sorry, I don't have access to real-time information about job openings. Well, that's why we want to provide context. We want to feed this entire FAQ to the OpenAI API as part of its system prompt.
Okay. Let's go back. We're going to rename the file.txt, leaving it in this Markdown format.
And that's a README, right? If you load anything to GitHub—any repo—you're supposed to have a README. It just explains what your project is about. So this is an important format to know.
The README file format is.md. We're going to go with just.txt here, just to get the file loaded up. I think we just load it as a README, but I already changed it. That's fine.
So here's what we're going to do. We're going to make a Server 11. We're going to do a save-as of HTML10, of index10.html, script10.js, and server10.py. We've got version 10 of the HTML, the JS, and the PY.
We need to make versions 11 of all of these. So let's go ahead and do that. Here they are—starting with index, save as index11—and then we'll change the script we're importing to 11.
Script10, save as script11, and server10, save as server11. In server11, right about the line that checks the conversation length, we're going to add this line. With open—the path to the FAQ, a second argument 'r' for read—as file.
We're giving an alias name to what we open, and then in the resulting file object, call the read method, which is going to return the text of the file. So right above here, load the FAQ text. So I'm going to say, with open, and it'll take the path—takes two arguments, the path, which is inside our static folder, right? Our text is in the static folder, and inside text, we have FAQ.txt. Second argument is 'r', meaning read, colon—oh, sorry—as file alias, colon—we're going to call it, we're going to catch what we load as FAQ text, and that will be file.read. So the file.read method will load up and return the text—save loaded FAQ string.
So there it is. Next, we're going to CONCATENATE the FAQ text onto the system prompt, and we're also going to augment the prompt to say, hey, there's an FAQ. The FAQ text will enable the AI to discuss Greenleaf's product services and career opportunities.
We're going to keep the beginning of the system prompt. Remember, this is if the conversation length is zero. Let's get some context here.
I'm going to just type it. If conversation—if the length, len(conversation), equals zero—I'm going to say conversation.append, and we're going to append this object with a role of system and a content of: You are a Customer Service Assistant for Greenleaf Nursery, a tree nursery website that sells sapling seeds and gardening accessories. We already have that part, but then we have this:
Here's a full FAQ about the company's products, services, and policies. Use this to answer any customer questions. That's what we're adding next, and then we're going to CONCATENATE on the FAQ, and we can just use string concatenation like so, or we can get fancy and do the string formatting with the F content F. Actually, you don't do it here at all.
You would do it in the content value right here. Okay, so this whole string—we're going to use template literal strings with an F in front—and that means that anything else you want to add as far as a variable gets added inside curly braces, and we want to add, of course, the FAQ text. We'll even do a line break if that helps the AI understand the text structure better.
There it is. There's the role. There's the system.
There. Nice and easy to read here. Okay, that's good.
So, that's new. Here's a full FAQ about the company's products, services, and policies. Products, services, job openings—current job openings—and policies.
Use this to answer any customer questions, and there's the FAQ added on. Try the chat again. Now the AI chat assistant should be able to draw upon the FAQ specifics.
So, in addition to its vast general knowledge, the AI now knows not only which specific tree varieties the nursery sells and how much they cost, but also how much sunlight apple trees require and which species of trees grow best in a climate. Vast general knowledge such as how much sunlight apple trees require and which species of trees grow best in a climate. It also knows about specific Greenleaf products.
Products, services, and even job openings. Services and job openings. Yes, services.
There it is. The AI can even help you in your job search. All right.
So, there it is. There is no change to the JavaScript whatsoever. All the change is to the system prompt.
So, the JS stays the same. The system prompt we need to change. There it is.
That's the big change right there. That's the system prompt. That's the loading of the FAQ, and there's the new system prompt.
Everything else is the same. Yep. And that's it.
It's just those two changes. Really, just load the file and augment the prompt. That is called prompt engineering.
I mean, it's not a lot of code to do it, but you do have to get the text together and understand that that's a thing that you would want and do. So, what we're now going to do is test and make sure the AI is aware of the website—services, products, and services.
Like, knows all this stuff. You know, does it know all this FAQ content? So, let us stop the server. It's fine.
Be that way. Back down to 11. Now, ask a question.
Do you have any job openings? Server error. Not a valid JSON. All right.
Okay. Gotcha, gotcha, gotcha. Why is it not a valid JSON? All right.
Fine. Let's make sure 10 is still working. Now, 11 is a save-as of 10.
That's why. Right? Do you have any job openings? Okay. It's working.
What is it about 11 that messed up? Let's go look. Static FAQ.txt. FAQ.txt.
Static text. Open. Why don't we, you know, let's do something.
Let's, as we go along here, let's print FAQ text. Not the whole thing. We'll print the first 100 characters.
Let's try that. Why, what are we getting here? Oh, there's a—wow, what's up? Penalized. Penalized.
Try help. What's up with this? Why is it printing all this stuff? I don't think I asked it to. Oh.
Sure. I just want to print the first 100 words of the FAQ. 100 characters, really.
I don't know why it's printing all that stuff, though. All right. Let's see.
The keyboard interrupted. There's something up. Something is up.
And let's go see. Do I have any job openings? It's not working, but, oh, error. Okay.
It's breaking here. What's it failing on? These line numbers, like 900, this means it's inside the modules. Okay.
Line 20. That's more like it. No such file.
Oh, wow. Is that right? There's no such file. Oh, we're in Python.
We don't need to escape this path. That's why. We're in the—servers are not inside a subfolder, so they don't need the dot slash.
Just one dot slash. That means current directory. All right.
Dot slash means current directory. You don't even need that. You could just say static, like so.
But, okay. That ought to fix it. It was going in the wrong directory.
It wasn't finding the file. It should run off this same server. We don't need to redo it.
Job openings. Okay. Boom.
There. Look. Yes.
We have tons of job openings. Look at all that. Basically told you every single job opening and gave all kinds of—system daily care plans, maintain greenhouse environment.
It—let me see if it—it basically just copy-pasted everything. Must be able to lift 50 pounds. Does this say all that stuff? No, it doesn't say everything.
It uses its discretion. Doesn't give you every little detail. It doesn't output it very well, but it does give you an email.
I mean, yeah. An email to send your info to. Where do you have any physical locations which I can visit? Yep.
We have two. You can visit one in Oneonta, New York, and the other in Lancaster, PA. Both locations feature climate-controlled greenhouses that are open year-round.
Now I don't know if it says that in the FAQ. Climate control might be embellishing a bit. Yeah.
Oh no, no, no. There. It did not.
That's actually in the FAQ. So it has context awareness now. That is a complete chat assistant.
You can make these for your website. Alrighty. That concludes Part One of this AI for Python class—building apps with the OpenAI API.
We did a full project called an AI chat assistant. In Part Two, we're going to turn our attention to making apps that consume images—that analyze images. So rather than provide text to the OpenAI API, you don't say anything.
You just provide an image, and it tells you what it is. And we have a prompt. I mean, you don't just upload anything.
Very specific prompt having to do with analyzing food. We're going to send pictures of meals—plates of food, whole meals—to the OpenAI API with instructions to give us a full description and nutritional breakdown of the meal. All right.
All that in the next and subsequent lessons. See you then.