Effective Prompt Engineering
Parts of A Prompt, Tips for Designing Effective Prompts and Must-Know Prompting Techniques
Prompt Engineering is a new term that is used to describe developing and optimizing prompts to efficiently use language models (LMs). But this term does not just indicate providing better prompts to get better results. Instead, it’s a wide set of practices and important skills to interface, build with, and understand the capabilities of LLMs.
In this article, I will discuss the basics of prompting, highlight important points for building effective prompts, and explain some important prompting techniques.
Parts of A Prompt
To build an effective prompt, it’s crucial to include key details. A well-structured prompt typically consists of the following components:
Instruction / Task
(Clearly describe what you want the AI to do)
Context / Background
(Provide background information for the task)
Input Data
(Input or question that we are interested to find a response for)
Output Indicator (Style, Format, etc.)
(Style or format of the output)
Persona
(Providing a story / who you are?)
and more…
You can use this list as a checklist, ensuring that you include all relevant information under each of these topics.
Below is an example that demonstrates a response both with and without these components
Without ❌:
and with the related prompt parts ✅:
Even with a short sentence, the second example clearly provides more detail and is closer to what you have in mind. By including the relevant components, you can expect AI tools to handle more complex tasks. This is just a simple illustration of a well-crafted prompt.
You should approach the AI tool like a normal person and always keep in mind that, it’s been pre-trained on human data., If you struggle to understand a task based on the background information provided, it's unlikely that the AI will execute it as intended.
In the next section, I’ll discuss key factors to consider for improving your prompts, in addition to providing the important details of these parts.
Tips for Designing Effective Prompts
Understanding the components of a prompt is crucial. Even for small and straightforward tasks, like the one in the previous section, it can significantly improve the quality of the response.
In addition to specifying these parts with your prompts, you can check the following tips to improve your prompts:
Write clear, small, and do one thing purpose prompts.
Remember, designing prompts is an iterative process. Start with simple prompts and ask the AI to focus on one task at a time.
For example, if you ask an AI tool to “Generate a react application. Then create a shopping cart application, covering the following: …” Then it will probably fail at some point or at least does not generate output as you wish.
Instead, break down your tasks into single executable units and keep adding more elements with the subsequent prompts. With this approach, the AI tool can create a context for itself and better evaluate your tasks.Provide sample texts, reference links, and all other required information.
Language models are designed to provide you with an answer no matter whether they know the answer or not. So, they are intended to give fake answers (if the instructions are not clear for the tool). To prevent this, make sure to provide all relevant context and include reference links to guide the AI in delivering accurate, reliable answers.Use a clear separator to split sections.
When designing a prompt, it’s important to break down the related sections with a clear separator. For example, you can use “##” (heading 2 in markdown) as a separator:## Task:
I want you to refine the provided text.
## Warnings:
…
## Text:
”…”
or you can follow a tag/placeholder-based separation as follows:<Task>
I want you to refine the provided text.
</Task>
<Warnings>…</Warnings>
<Text>…</Text>
It’s important to break down each part into individual contexts. This way you are providing a clear boundary for each section to AI tools. The way you structure your prompt directly impacts its efficiency and the quality of the response.Use step-by-step instructions for multi-step tasks.
Some tasks are best specified as a sequence of steps. Writing the steps out explicitly can make it easier for the model to follow them.
You should follow this approach in case, it’s not possible to break down the tasks into individual prompts and they need to be processed in the same context. Because, generally it’s better to break down multi-step tasks into separate prompts.Include examples if possible.
If it’s possible you can include sample input and output to the AI tool to help set expectations for the output:Classify the text into neutral, negative, or positive.
Example 1:
- Text: I really like here.
- Sentiment: Positive
…Text: I think the food was okay.
Sentiment:?
Including examples ensures that the AI has a better understanding of the desired format and content.Give models time to think.
One of the most effective ways to improve output is to allow the model to reason through tasks step-by-step. This technique, often referred to as "chain-of-thought prompting," enables more accurate and coherent responses, especially for complex or multi-step problems.
Here is a good example from OpenAI’s prompt engineering document:As demonstrated above, the prompt encourages the AI tool to take time and break down the problem into step-by-step processes. If it does not follow this kind of a generation process then probably the AI tool will give a wrong answer by specifying the student’s solution as true. (This technique is known as 'Chain-of-Thought,' which I will discuss in more detail in the next section)
Reduce imprecise descriptions.
*
Bad ❌:Generate a sub-heading title for my article: “HybridCache”, it should not be too long or too short.
Good ✅:
*Generate a sub-heading title for my article: “HybridCache”.
Use a 3 to 5 sentence paragraph to describe the article. …
The first prompt does not explicitly specify the length of the expecting output instead it gives an imprecise description as “not too long or too short”. On the other hand, the second one explicitly specifies the length, so it’s likely that the AI tool will generate a better sub-heading title for the second prompt.
These are some common practices you can use to design effective prompts, in addition to specifying the key components discussed in the previous section. These techniques work best when used together, so pay attention to these tips and include the relevant sections in your prompts to achieve better results.
Must-Know Prompting Techniques
After covering some common tips for designing better prompts, let's now explore some essential prompting techniques in simple terms:
1. Zero-shot Prompting
Zero-shot prompting is when you ask an AI to perform a task without giving it any prior examples. You rely entirely on the AI's pre-existing knowledge. This works well for simple and small tasks.
2. Few-shot Prompting
As the name implies, few-shot prompting involves giving the AI a few examples before asking it to perform the task. This helps guide the AI’s response.
3. Chain-of-thought Prompting
This technique encourages the AI to break down its thought process step by step to arrive at an answer. It’s particularly helpful for solving problems or answering questions that require reasoning. It’s all about giving AI time to think:
This technique is good for breaking down a complex question into manageable steps and it forces AI to solve the problem how a human might normally solve the problem.
4. Self-consistency Prompting
Self-consistency prompting is a technique where the AI generates multiple responses to the same prompt and then selects the most consistent or common answer.
The self-consistency technique ensures that the final output is the most accurate and relevant among the other options/answers.
5. Retrieval Augmented Generation (RAG)
Retrieval-Augmented Generation (RAG) is a prompting technique where a model combines information from external sources (like databases or documents) with its own internal knowledge to generate more accurate and relevant responses. This method improves the model’s output by augmenting its generation process with real-time retrieval of relevant data.
Conclusion
In this article, I’ve covered the basics of prompting, highlighted key points for building effective prompts, and explained several important prompting techniques.
There are many more prompting techniques, and I’ve only briefly covered five of them with examples. It’s beneficial to familiarize yourself with these techniques and apply them when appropriate.
It’s also really important to structure your prompt by understanding its components (context, task, instruction etc.), providing clear boundaries, and following best practices to achieve better results.