Prompt Engineering Series, Part One: Easy Wins in Prompt Crafting
Straightforward Approaches to Better Your Experience with LLM Prompts
Hello and welcome back to AIDriven.Dev. We continuously explore fascinating and innovative methods of integrating AI and Language Learning Models (LLMs) in software development.
I’m excited to present Part One of our ongoing series - Prompt Engineering. This series focuses on the art and science of crafting prompts for LLMs, specifically the GPT family. Our goal? To help you dramatically enhance your LLM experiences through effective prompt crafting.
In this part, we're focusing on some 'easy wins' - basic, yet highly effective tips that will serve as a solid foundation for more advanced techniques we'll explore in future parts of this series.
It's worth mentioning that while most of these strategies should be effective across various models, my testing and validation have been primarily focused on OpenAI’s GPT-3.5 and GPT-4 models. So, let's dive in and start boosting your prompt crafting skills!
NOTE: In this article, I'm sharing insights drawn from both my personal experiences with GPT models and valuable resources I've found along the way. Be sure to check out the list of references at the end of the article for more in-depth information.
Why Prompt Engineering? And What Does It Mean for You as a Developer?
When I first encountered the term "prompt engineering", I was slightly skeptical. The concept of "prompting" seemed very straightforward - after all, it's about guiding the LLM (like a GPT model) using natural language. However, as I dove deeper into its principles and guidelines, I realized it's not just about using natural language. Yes, you can still get decent results without adhering strictly to these guidelines, but if you're aiming for consistently good outcomes, these principles become indispensable.
You've probably seen lots of impressive AI applications that have sprung up in recent months. In most of these, prompt engineering plays a pivotal role. But what is prompt engineering, exactly?
Prompt engineering can be seen as the technique of carefully crafting instructions to get an ML model to do precisely what you want. Consider instruction-tuned LLMs like GPT. When you use an instruction-tuned LLM (such as GPT), think of giving instructions to another person, say someone that's smart that can do a lot of things but need careful instruction to do them well.
Imagine the potential of automating tasks that currently consume countless hours of your time, from drafting emails to generating code. It all boils down to formulating a well-structured prompt and letting the AI work its magic.
So, whether you're working with ChatGPT or using GPT APIs, it's vital to comprehend and master the art of writing effective prompts. If you're puzzled about why the model isn't producing the results or the output format you desire, it's probably the prompt that needs tweaking. Without further delay, let's delve into some easy wins - fundamental principles that will enhance your prompt writing skills.
Basic Prompt Engineering Principles
Principle 1: Write clear and specific instructions
This is the most fundamental and crucial principle of all. If you're aiming for good results, be detailed in your prompt. The more specifics you provide to the LLM about the task, the less likely you are to receive incorrect or irrelevant results. This principle really resonates with me. Consider your day-to-day job - if someone assigns you a task without clear and precise instructions, you'd probably do a lot of guesswork, potentially resulting in an outcome different from what was intended.
GPTs can’t read your mind. If outputs are too long, ask for brief replies. If outputs are too simple, ask for expert-level writing. If you dislike the format, demonstrate the format you’d like to see. The less GPTs have to guess at what you want, the more likely you’ll get it - OpenAI's GPT best practices
To ensure clarity and specificity in your instructions, consider employing these tactics:
Make your query detailed. For instance, "Who's president?" lacks clarity and specificity, while "Who was the president of Mexico in 2021, and how frequently are elections held?" provides a much clearer picture.
Use delimiters to distinctly indicate different parts of your input. Delimiters can be anything, such as: ```, """, < >, or <tag> </tag>. Here's an example:
Explain the code delimited by triple back-ticks: ``` Your code ```
Ask for a structured output such as JSON, HTML ...etc
Generate a list of three made-up book titles along \ with their authors and genres. Provide them in JSON format with the following keys: book_id, title, author, genre.
Use “Few-Shot” prompting which is basically providing examples of successful executions of the task you want to be performed and ask the model to follow your lead
Your task is to answer in a consistent style. <child>: Teach me about patience. <grandparent>: The river that carves the deepest \ valley flows from a modest spring; the \ grandest symphony originates from a single note; \ the most intricate tapestry begins with a solitary thread. <child>: Teach me about resilience.
Principle 2: Give the model time to think
In software engineering, it's common practice to break down a large task into smaller, manageable sub-tasks. This approach also works well with LLMs, reducing the likelihood of errors due to rushed conclusions - a mistake humans might also make when dealing with complex tasks.
Here are some tactics to help your model 'think' through tasks more effectively:
Specify the steps required to complete the task. For example, the following prompt would result in a better result than just giving a generic/short instruction
Perform the following actions: 1 - Summarize the following text delimited by triple backticks with 1 sentence. 2 - Translate the summary into French. 3 - List each name in the French summary. 4 - Output a json object that contains the following keys: french_summary, num_names. Separate your answers with line breaks. Text: ```{text}```
I find myself applying this method quite frequently. I picked it up through a process of trial and error, initially making generic requests and then refining these prompts after observing what the model overlooked. This approach is especially beneficial when you're proficient at a task but find it tedious and would rather have the AI take it on. You get to guide the model through the necessary steps. Here's an example I use almost daily when working on my side projects using Python/Django stack. Bear in mind that it's not a perfect prompt. I usually have to tweak it a bit or provide more context as needed. However, it serves as a good illustration of being clear, specific and outlining the steps to complete the task.
You are a very experienced Django developer AI. Your tech stack is Python 3.11, Django 4, PostgreSQL 13, TailwindCSS
You are working on project X whic does X. Your task is to build a new endpoint that should all user to do X. Follow th following steps:
1. Create the DB/Django model(s) and make sure to include fields: name, description...etc
2. Create the view function along with the URL new path/config and make sure authentication and authorization are considered
3. Create the view template (HTML template) and make sure to use tailwindCSS for styling
Instruct the model to work out its own solution before rushing to a conclusion. This is somewhat similar to the first tactic, but here you're not providing the steps. Instead, you're asking the model to think through the task and come up with its own solution. Essentially, we're asking the AI to 'think out loud' as it tackles the task. Here's what an instance of this looks like:
Your task is to determine if the student's solution \ is correct or not. To solve the problem do the following: - First, work out your own solution to the problem. - Then compare your solution to the student's solution \ and evaluate if the student's solution is correct or not. Don't decide if the student's solution is correct until you have done the problem yourself. Question: ``` {question} ``` Student's solution: ``` {Student's solution} ``` Actual solution:
Principle 3: Prompt engineering is an iterative process
This is not a technique to apply to a prompt but something to know and keep in mind, prompt engineering is an iterative process that mirrors the evolution of ideas and implementations in machine learning model development. It's rare to conceive the perfect prompt for an application on the first attempt.
In the course of prompt engineering, you may start with an idea for a task you want the LLM to complete. You would then attempt to articulate this task in a clear and specific prompt. After running the prompt and analyzing the model's output, you may realize that your instructions weren't clear enough or that you didn't give the model ample time to "think." This feedback then informs your revisions to the prompt and helps refine your idea, creating a loop of continuous improvement until the prompt works ideally for your application.
Principle 4: Be aware of the model’s limits
Understanding the limitations of large language models (LLMs) is crucial when developing applications that leverage these models. Even though LLMs, such as GPT-4, have been trained on extensive data sets, they don't perfectly retain the information they've encountered. Consequently, they may struggle to accurately define the extent of their knowledge, sometimes generating responses about obscure or complex topics based on guesswork rather than factual information. These inaccuracies, known as "hallucinations," can result in the creation of plausible but untrue statements.
There are, however, strategies to mitigate these limitations. One effective method is to guide the model to draw upon relevant quotations from its input text when generating answers. This approach can help ground the model's responses in the provided material, reducing the likelihood of hallucinations.
I also find it beneficial not to entirely trust that the LLM will always produce the output as instructed. For instance, if you've asked the model to output in JSON format, it's crucial to validate that the output is in a valid JSON format and contains the specific JSON "keys" you required.
Conclusion
As we bring this first article in our 'Prompt Engineering' series to a close, I'd like to remind you that we've merely skimmed the surface of this fascinating topic. Today, we've touched upon some easy-win strategies that can immediately improve your interaction with Language Learning Models, but the world of prompt engineering is vast and we've got a lot more ground to cover.
Remember, these principles are just the tip of the iceberg. We've focused on basic yet highly effective techniques, but there are more advanced strategies waiting to be unveiled. We'll dive deeper into these in the subsequent parts of this series, so stay tuned.
I encourage you to refer to the resources section below for a deeper dive into these concepts. Not only does it provide more details, but it also gives you a broader perspective on the subject.
If you found this article valuable, consider subscribing to our newsletter. You'll be the first to know when we release the next article in this series. Moreover, if you think this information could benefit others, do share it among your network. After all, the more we share, the more we learn and grow together.
Thank you for joining me in this exploration of prompt engineering. I look forward to continuing this journey with you in the subsequent parts of the series. Until next time!