The Beginner's Guide to LLM Prompting
How to engineer your natural language prompts to get the best answers from a large language model
June 15, 2023Large language models (LLMs) have made it possible for everyone to interact with an AI â not through code but using prompts in natural language. The fact that language now acts as an interface to complex models makes it necessary to investigate the prompts we use more closely.
When used correctly, generative models can produce highly valuable results for businesses. Getting your prompts right is therefore necessary to harness LLMsâ enormous potential, especially when youâre looking to incorporate it into your product. That is why an entire industry has formed around the topic of âprompt engineering.â In this post, weâll explain our approach to this technique and share the dos and don’ts of prompting.
What is a prompt?
A prompt is an instruction to an LLM. If you have interacted with an LLM like ChatGPT, you have used prompts. Ideally, a prompt elicits an answer that is correct, adequate in form and content, and has the right length.
Essentially, prompting is about packaging your intent in a natural-language query that will cause the model to return the desired response.
How to come up with good prompts
Good prompts follow two basic principles: clarity and specificity. Clarity describes the use of simple, unambiguous language that avoids jargon and overly complex vocabulary. So rather than keeping your queries short and snappy, better take the long-winded way to make your point sufficiently clear to the LLM.
Example of an unclear prompt:
Who won the election?
Example of a clear prompt:
Which party won the 2023 general election in Paraguay?
Specificity, on the other hand, refers to the need for context. Tell your model as much as it needs to know to answer your question. Sometimes, this can veer almost into storytelling territory.
Example of an unspecific prompt:
Generate a list of titles for my autobiography.
Example of a specific prompt:
Generate a list of ten titles for my autobiography. The book is about my journey as an adventurer who has lived an unconventional life, meeting many different personalities and finally finding peace in gardening.
Letâs look at a few tricks to make our prompts even better.
Prompt engineering hacks
Prompting is not a science, and calling it âengineeringâ may be a slight stretch. But researchers and users of LLMs have identified some tricks that consistently improve the modelsâ responses.
Do say âdo,â donât say âdonâtâ
This point actually continues our specificity theme because âdoâ instructions are by nature more specific than âdonâtâ. So rather than telling the model what not to do, itâs usually better to specify exactly what we want it to do â if we know what that is.
Letâs imagine that, in our previous example, we want to make sure that the LLM doesnât produce overly long titles (as those models tend to do). Rather than saying:
Don't make the titles too long
it is better to be specific:
Each title should be between two and five words long.
Use few-shot prompting
In our example, we asked for a particular item (the title for an autobiography) of a certain length (two to five words). This type of instruction without any concrete examples is known as âzero-shot prompting.â
Most models, however, benefit from âfew-shot prompting,â where you feed your model some examples to learn the desired pattern. In our prompt, we can add a few existing titles as examples to help the model get a feel for what weâre looking for.
Here are some examples: Long walk to freedom, Wishful drinking, I know why the caged bird sings.
Structure your prompt in a meaningful way
Elements like quotation marks, bullet points, line breaks, etc. make it easier for humans to parse a text. And guess what? The same is true for LLMs. Letâs apply that insight to our previous example and spell out the full prompt once more.
Generate a list of ten titles for my autobiography. The book is about my journey as an adventurer who has lived an unconventional life, meeting many different personalities and finally finding peace in gardening. Each title should be between two and five words long.
### Examples of great titles ###
- âLong walk to freedomâ
- âWishful drinkingâ
- âI know why the caged bird singsâ
Now that weâve perfected our prompt, what does the LLMâs output look like? Hereâs what ChatGPT came up with in response:
âSeeking Skies, Finding Rootsâ is hilarious and I would absolutely consider reading that book.
Use leading words
Sometimes, the simplest techniques are the most effective. Researchers from Tokyo University have shown that telling a model to âthink step by stepâ before its response can generate more accurate results and help the model correct its own mistakes. This method is known as using âleading words,â because we gently guide the model towards a more effective plan for problem-solving.
The model is forced to break down its solution into multiple, more manageable steps rather than being allowed to just hazard a guess. Seeing how this simple trick improves the modelâs performance is pretty striking. Here is an example of a math puzzle that requires just a bit of recursive thinking:
The LLM confidently responds â with the wrong answer. Hereâs what happens when we ask it to âthink step by step:â
And this time, the final answer is correct.
Prompting pitfalls
In addition to these techniques for improvement, it helps to know which elements in an instruction can throw the model off. Some people, for example, try to incorporate requests for short, one-word answers into their prompts to prevent the models from rambling.
However, as the last example has shown, wordier answers might not be a bad thing when it comes to LLMs â but rather, a technique these models use to arrive at a correct response. LLMs may soon learn to keep their thought processes to themselves. In the meantime, we should let them ramble and refrain from requests for one-word answers.
Another problem happens when you exceed the modelâs context length. Due to the nature of these large Transformer models, an LLMâs âcontext windowâ (which is the portion of text it can process at once) comprises both the prompt and its response. If you exceed a modelâs context length, its output will deteriorate.
While this is a fascinating topic, it is also a fairly complex one â context lengths vary widely between models, and it isnât even trivial to measure the length of your prompt. Just remember that if the length of your prompt becomes an issue, you should look into breaking the problem down into smaller steps.
Become a better prompter
This article leans heavily on Vladimir Blagojevicâs webinar on âPrompting LLMs Using Haystack.â In it, Vladimir, who is a member of the core engineering team here at deepset, takes a deep dive into the art of prompting and shows how Haystack, our open source framework for NLP, offers just the right tools to get prompting right.
For even more tips on how to come up with great prompts, have a look at our guidelines for prompt engineering.
Prompts also play a huge role in Agents: the latest fascinating trend that lets you harness the power of LLMs. Agents receive a sort of âsuper-promptâ that instructs the LLM to break its reasoning into manageable steps and delegate those to the tools it deems most capable of solving the tasks. Have a look at our blog post on Agents to learn more.
Finally, you donât always need to write your own prompts. Head over to our PromptHub, where you find prompts for all kinds of applications, which you can use out of the box or tune to your liking, to get the best answers from your LLM.