Prompt Engineering Guidelines
Prompts are instructions for large language models to generate a response or complete a task. The way you construct your prompt impacts the accuracy and format of the answer the model returns. That's why it's important to get your prompts right. This guide contains guidelines and best practices for writing prompts in deepset Cloud.
General Tips
- Prompts can contain instructions or questions to pass to the model. They can also contain additional information, such as context or examples.
- Prompt engineering is an iterative process, and it's rare to craft the perfect prompt on your first attempt. Be prepared to experiment and refine as you go. Writing effective prompts comes down to practice and learning through trial and error.
- Version your prompts to compare the performance of different versions and choose the one that worked best.
- Start with simple, zero-shot prompts and then keep making them more complex to reach the required accuracy of answers. You can add more context and examples to each iteration of your prompt. If you're not seeing any improvements, fine-tune the model.
- Always use the latest models available.
- The way you write prompts is often specific to the model you're using. Always check the instructions to prompt that particular model.
Prompt Structure
Remember that the prompt's structure depends on the task you want the model to perform. Experiment and test what works best.
Prompts with a structure that follows these guidelines seem to achieve better results than prompts with a random structure:
-
The prompt has instructions, context, input data, and output format. Not all these components are necessary, and they may vary depending on the model's task.
-
You can try experimenting with specific prompt structures, for example:
Question: Context: Answer:
or for question answering without a given context:
Q: A:
Instructions
- Put them at the beginning of the prompt.
- Use commands such as "write," "summarize," and "translate." Try using different keywords and see which yields the best results.
- Separate instructions from the context with a delimiter, such as quotation marks ("") or hashes (###).
- Separate the instructions, examples, context, and input data with one or more line breaks.
Examples:
### Instruction ###
Give a title to the article. The title must have 5 words.
Article: Online retail giant Amazon has said it plans to shutdown three warehouses in the UK putting 1200 jobs at risk.
Title:
Instructions: Detect the language of the text. Answer with the name of the language.
Text: Professionelle Beratung und ein Top-Service sind für uns selbstverständlich. Daher bringen unsere Mitarbeiter eine große Portion Kompetenz und Erfahrung mit.
Context
- Specify any information you want the model to use to generate the answer. The context should help the model arrive at better responses.
- In RAG pipelines, you can pass documents as context for the model.
Example:
Answer the question using the context. If you are not sure about the answer, answer with "I don't know".
Context: Contrails are a manmade type of cirrus cloud formed when water vapor from the exhaust of a jet engine condenses on particles, which come from either the surrounding air or the exhaust itself, and freezes, leaving behind a visible trail. The exhaust can also trigger the formation of cirrus by providing ice nuclei when there is an insufficient naturally-occurring supply in the atmosphere. One of the environmental impacts of aviation is that persistent contrails can form into large mats of cirrus, and increased air traffic has been implicated as one possible cause of the increasing frequency and amount of cirrus in Earth's atmosphere.
Question: Why do airplanes leave contrails in the sky?
In deepset Cloud, you can include the documents in the prompt:
You are a technical expert.
You answer questions truthfully based on provided documents.
Ignore typing errors in the question.
For each document check whether it is related to the question.
Only use documents that are related to the question to answer it.
Ignore documents that are not related to the question.
If the answer exists in several documents, summarize them.
Only answer based on the documents provided. Don't make things up.
Just output the structured, informative and precise answer and nothing else.
If the documents can't answer the question, say so.
Always use references in the form [NUMBER OF DOCUMENT] when using information from a document, e.g. [3] for Document[3].
Never name the documents, only enter a number in square brackets as a reference.
The reference must only refer to the number that comes in square brackets after the document.
Otherwise, do not use brackets in your answer and reference ONLY the number of the document without mentioning the word document.
These are the documents:
{% for document in documents %}
Document[{{ loop.index }}]:
{{ document.content }}
{% endfor %}
Question: {{ question }}
Answer:
Output Format
- Be specific about the format you want the model to use for the answer. If needed, specify the length, style, and so on.
- It may help to give the model a couple of concrete examples.
Example:
Classify the text into neutral, negative, or positive.
Text: I love Berlin!
Answer:
By giving the model examples, you're showing exactly what answer you expect.
Language
- Be direct, specific, and detailed.
- Be concise, as there's usually a limit on the number of tokens in a prompt and longer prompts are computationally more expensive than shorter ones.
- Focus on what you want the model to do rather than what you don't want it to do.
Example:
Explain the concept nuclear sampling in 3 to 5 sentences to a 10-year old.
Explain nuclear sampling. Be short and don't use too complicated language.
Role Prompting
Role prompting is a technique for instructing the AI to assume a specific role or persona or act in a certain way. It's helpful to control the style, tone, and even the level of detail of the model's answers.
Example:
In this example, we want the AI to sound professional and generate objective, to-the-point answers:
You are a technical expert.
You answer questions truthfully based on provided documents.
When asking the AI to write an email, prompting it to assume the role of a customer service representative may result in a more relational and solution-oriented answer:
You are a customer service representative.
Write an email to a client advising them about a delay in the delivery schedule due to logistical problems.
Few-Shot Prompts
Few-shot prompts contain a couple of examples for the model. By giving the model examples, you demonstrate what you expect it to do and enable it to perform in-context learning to improve its performance. Few-shot prompting helps to express ideas that may be difficult to explain through instruction. It can also significantly improve the accuracy of the model's answers.
Use few-shot prompts if you require the model to return an answer in a specific format or if a simple prompt didn't yield the results you needed.
- Use consistent formatting.
- Provide examples in random order. For example, don't put all negative examples first and positive ones second. Mix them up as the order might bias the model.
- Make sure you use labels.
Usually, the more examples you provide, the better, but be aware of the model's context window size limitations.
Examples:
This example is from the Brown et al. 2020 paper:
A "whatpu" is a small, furry animal native to Tanzania. An example of a sentence that uses
the word whatpu is:
We were traveling in Africa and we saw these very cute whatpus.
To do a "farduddle" means to jump up and down really fast. An example of a sentence that uses
the word farduddle is:
Model answer:
One day when I was playing tag with my little sister, she got really excited and she
started doing these crazy farduddles.
Or for sentiment analysis:
Classify the text into neutral, negative, or positive.
Text: I love Berlin!
Answer: positive
Text: I hate Paris.
Answer: negative
Text: I've never been to Bangalore.
Answer: neutral
Text: I don't like Tokio.
Answer:
Updated 4 months ago