From the course: Hands-On AI: Building LLM-Powered Apps
Prompt engineering basics
- [Instructor] At the end of the previous chapter, we have identified a deficiency in our chat with PDF application. It said that the PDF file does not contain enough information for one of the key questions we want to ask, despite there is enough evidence in the documents to answer. To address this issue, we can solve this by prompt engineering. We define prompt engineering as an engineered approach to communicate with LLMs to steer its behavior for the desired outcomes without updating model weights. It is a highly empirical method. First and foremost, we have to be very specific and explicit, leaving very little room for the model to misinterpret. This is similar to providing instructions to children. So to make our discussion more structured, we are introducing a prompt engineering framework, called RASCEF. It's R-A-S-C-E-F. It stands for roles, actions, steps, context, examples, and format. R stands for role playing. We can instruct the large language model to assume a role, so it will try to behave like one. As prompt engineers, we have the liberty to expand and define what the role does, and even the potential character to undertake if necessary. An example would be, "Please act as an expert product manager, like Steve Jobs, when you respond to user requests." This is effective for common job titles and roles, but less effective for the less common roles. Next, the A stands for action. It's the action that we want the large language model to take. For example, summarize, answer, extract, copyright, revise, et cetera. Again, we have to be very specific with the action required for the intended results. Next S in the RASCEF framework stand for steps. This is where we instruct the model to either explicitly follow certain steps or for the model to think step by step before it acts. The C in the framework stands for context. Sometimes the model might not have the right context or knowledge to answer our questions. As we have done in the previous lab, we use the RAG, retrieval augmented generation architecture to provide relevant context for the model to respond to our request. The E in the RACSEF framework stands for examples. For school or homework problems, it is very common for the teacher to provide a few examples and answers to the students so the students can mimic and learn from those examples. Language model behaves very similarly. We provide it with examples on how to answer questions to allow it to better answer to our request. Finally, the F in RACSEF stands for format. In our previous lab, you see that we put the format in a stream pass through. This way we can better analyze the model output for downstream applications. Sometimes we want the model to output a structure JSON format to better connect with the next step. And sometimes we just want the model to output a string to tell a story. So as an example, on the left here, we let the model output a un-structure, free form generation, and on the right, we ask the model to provide the answer in a JSON schema format, so it produces a JSON output, and this JSON output can be later parsed for downstream usage. In summary, prompt engineering is an engineered approach to guide the model to generate the desired output. We can utilize the RACSEF framework to instruct the model. Before we can engineer the prompt for our application, we have to try to first extract the prompt template used in our chat with PDF application and put them in a easily editable location. So next, we will go through a lab to extract a prompt template to enable prompt engineering in our application.