Prompt engineering 101 for developers
How you can get generative AI (like ChatGPT and Google Bard) to be better at helping you debug and translate code, generate tests, write functions, and more.
Jul 11, 2023 • 7 Minute Read
Prompt engineering is the latest term thrown around when talking about generative AI systems. Far from a buzzword — like, say, the metaverse — prompt engineering is a crucial method for you to fine tune and optimize the responses you get from language models.
Prompt engineering can really be summed up as this: if you want better results, you need to ask better questions. This article will help you do just that.
Before we get started, the usual coding caveat applies: don't use code without validating it! You can and will get incorrect results, so make sure you understand what is being generated. Think of these tools as something to help your efficiency, rather than write all the code for you.
A bit more on Prompt Engineering
Have you ever got a vague brief from a client or coworker, with no scope or details (like the screenshot above)? It’s a recipe for disaster, right? You don’t know what they want, and if you proceed anyway, you’ll likely get it wrong.
Normally, you’d ask some follow up questions and get some clarity around the brief. However, AI models aren’t that intuitive. Like any machine, they do exactly what you tell them to: garbage in, garbage out. So the onus is on the user to make sure they provide the right ‘brief’.
That’s what prompt engineering is in a nutshell: providing effective prompts or instructions to an AI model to get accurate, effective responses. As a developer, this allows you to get output suitable for specific applications and tasks.
That said, no matter how good your prompt engineering skills are, you are still limited by the capabilities of the model itself. The quality and diversity of the model’s training data is crucial.
Wait, is prompt engineering just another word for Google-Fu?
For those familiar with the earlier days of the internet, this might sound an awful lot like “Google-Fu”, a term for your skill at creating a successful search engine query. These days, that’s pretty much second nature to everyone who uses the internet.
There are a lot of similarities between the two: you’re putting in a prompt, and trying to get a certain result. However, one of the key differences between prompt engineering and Google-Fu is that an AI model will not give the same response every time, whereas a search engine will — it’s a bit of a lottery. Engineers can guide the response from the model with prompt engineering, and trial and error is required.
Just like with Google-Fu, it’s likely one day we’ll all intuitively be prompt engineers. However, since generative AI is in its early stages, the level of technical literacy isn’t there yet.
The basics of prompt engineering
Right, now let’s get down to actually creating a great prompt. To maximize the effectiveness of prompt engineering, you should focus on the structure of the prompt, your phrasing, and context. We'll show examples of these principles in this article.
What your starting prompt should include
Introductions are important. While you don’t need to say “Hi, I’m Joe McHuman, nice to meet you”, your first prompt with the AI should contain the following structure:
- Introduction: Set up the context for which you're chatting in. It helps to give the AI an imaginary ‘role’ to think of themselves in. e.g. “Act as a software engineer. You're an expert in Python and …”
- Task: e.g. “I want you to develop software to manage my record collection.”
- Contextual Information: e.g. “I want it to be a web based application written in Python.”
- Instructions: e.g. “I want you to generate the code to write the program.”
- Closing: e.g. “I want to host it as an AWS Lambda function.”
So, putting all of the above together, our example prompt would look like this:
"Act as a software engineer. You're an expert in Python and AWS technologies. I want you to develop software to manage my record collection. Make it a web based application written in Python. Generate the code for the program, and I want to host it as a Lambda function."
And with a tool like ChatGPT, it immediately gets to work:
Now you have a conversation started about how to develop your application (and you didn’t even need to make any small talk!).
Now, while it’s important to structure your prompts, remember you don’t need to stick to the above structure faithfully. If you don’t need a portion of information, don’t fill it in just to tick a box. Sometimes, less is more!
General prompt engineering guidelines:
Whether you’re crafting an opening prompt or five exchanges into a conversation, consider the following guidelines to get the best results.
1. Keep your prompts clear and concise
Don’t confuse the poor AI. Clear and concise prompts help the AI model understand the intended task more easily. Avoid ambiguity and unnecessary complexity so it’s easily able to figure out what you’re after.
2. Consider context and specificity
Context is important. If you ask your friend if saying “I’m sorry” and “My bad” mean the same thing, they might say yes. However, they might say no if you add the context of being at a funeral.
By giving an AI model the right contextual information, it’s able to give you a more relevant and accurate answer (e.g. “Actually, Joe McHuman, I wouldn’t recommend saying that at the wake.”) Incorporate relevant contextual cues within your prompt, and make sure the prompt is specific to the intended task, avoiding generic or vague instructions.
3. Use a conversational style
This might sound a bit odd when you’re talking to a robot, but keep it natural and conversational. Pretend you’re talking to a really smart person who’s helping you out, and keep the language simple and accessible.
Why? Remember that what you put in is what you get out. This conversational approach helps create a more human-like interaction and makes the AI’s output easier to understand.
4. Leverage active voice and rhetorical questions
For those not familiar with the term, active voice is not giving an AI an instruction while you go for a jog. It’s structuring a sentence like this:
Active Voice: Can you write a persuasive essay on the benefits of exercise? (Subject + Verb + Object)
Passive Voice: An essay on the benefits of exercise is to be written by you. (Object + Verb + Subject)
In the above example, “You” is the subject, “Write” is the verb, and “A persuasive essay” is the object.
Why is any of this important? Active voice in prompts imparts a sense of action and clarity to the AI model.It emphasizes what you're asking for and encourages a focused response.
Additionally, incorporating rhetorical questions within prompts can stimulate the AI model's "thinking process" and guide it towards generating more insightful and meaningful outputs.
Example prompts for coders
Here are some great example prompts to use when you’re writing code. These will help you in the development process to speed up development time and help find errors.
1. Debug Code
"Scan the following code for potential problems"
If you give the AI this problem and insert the source code afterwards, you can find potential problems with the code, even if the code executes successfully. This works in ChatGPT, Bard, GitHub Copilot, and TabNine.
2. Improve Performance
"Evaluate the following code and look for performance issues" followed by the code in question.
3. Generate Tests
"Write a test for the following {language} code" followed by the code in question.
4. Explain This Code
"Explain how {something} works in {language}."
5. Translate Code
"Translate the following {first language} code into {second language}."
6. Correct Syntax
"What is the correct syntax to {do something} in {language}?"
7. Write a function
"Write a function to {do something} in {language}."
Conclusion
These are a few examples of prompt engineering. It's an art that takes a lot of trial and error, but it's worth it in the long run! Continue to experiment and test until you get the best results.
To see some more examples, or learn more about generative AI, check out the following articles:
- How to use ChatGPT to write code
- ChatGPT Vs Bard: Which is better for coding?
- ChatGPT plugins: How to make ChatGPT smarter
- Self-Fixing and Auto-Code with AI: Debating the Pros and Cons
- Pigeon VS AI: Is a wild bird smarter than artificial intelligence?