Write a short story about Q: Let's think step by step >_ PROMPT ENGINEERING LARGE LANGUAGE MODELS · BEST PRACTICES · MAY 2023

Article · Prompt Engineering · May 2023

Crafting Irresistible Prompts for Large Language Models

Learn how to write excellent prompts for large language models like GPT-4 and Bard — covering what prompts are, why they matter, and the best practices for engineering them effectively.

Prompt Engineering LLMs NLP Deep Learning · Sumit Kant  ·  May 7, 2023  ·  5 min read

01 What are Large Language Models (LLMs)?

Large Language Models are AI models that can understand and generate human-like language. These models are trained on massive amounts of text data and can be used for a variety of tasks, including language translation, text completion, and question-answering. In essence, these models are trained to predict the next word given a sequence of words.

Large Language Models like OpenAI's GPT-4 and Google's Bard are increasingly becoming popular owing to their success in generating smart responses. However, these smart responses are elicited only when the instructions are clear and contain keywords that trigger the model to respond in that way. This is what prompt engineering aims to achieve — writing good prompts for eliciting good responses.

02 Four Paradigms of NLP Progress

Natural Language Processing has gone through four phases of development. None of these are obsolete — each paradigm built on the last.

~2015
1. Feature Engineering
Hand-crafting features for embedding contextual information to improve model performance. Mostly for non-Neural-Network models.
2013–2018
2. Architecture Engineering
Fully supervised neural network models that took care of feature engineering automatically. Better network architectures took precedence.
2017–now
3. Objective Engineering
Pre-trained models like BERT can be fine-tuned on different objectives. Finding the right training objective became the key challenge.
2019–now
4. Prompt Engineering
Large Language Models can be used for many tasks but need good input text to elicit desired outcomes. Prompt engineering is now taking over.

03 What is a Prompt?

A prompt is a Natural Language input that is given to a Large Language Model to perform a task. The prompt can be a question, a statement, or a partial sentence. The quality of the prompt greatly affects the quality of the generated output. A well-crafted prompt can improve the accuracy and relevance of the generated text.

Basic Elements of a Prompt

📋
Instructions
How to achieve the task — the method, constraints, and format you want the model to follow. Required.
Question
What to do — the specific query or task. Required. Either a question or instructions must be present.
📄
Input Data
Data to be used while answering the question and performing the task. Optional.
💡
Examples
What to expect in a task — few-shot examples that show the model the desired output format. Optional.

04 What is Prompt Engineering?

Prompt engineering is the process of crafting a well-defined and structured prompt to get the desired output given a Large Language Model and a goal.

Prompt engineering is needed because Large Language Models generate text based on the input they receive. If the input is vague, unclear, or incomplete, the generated output will also be vague, unclear, or incomplete. By engineering a good prompt, you can improve the quality of the generated output.

Prompt engineering requires:

🧠
Understanding of the model
Different models will react differently to the same prompt.
🎯
Understanding of the domain
To incorporate the goal into the prompt — e.g. what good and bad outcomes should look like.
⚙️
A programmatic approach
Like generating prompt templates that can be modified according to some dataset or context.
🔄
Exploration
The process is iterative — you test, observe, and refine.
We need prompt engineering because as smart as these models are, they have quirks and are only trained to predict the next word. A bad prompt can make stuff up — or Hallucinate.

05 Best Practices for Writing Prompts

Be clear and specific

A clear and specific prompt helps the model understand what is required. Avoid vague or ambiguous prompts.

Use context

Use the context of the task to craft the prompt — including the topic, target audience, and purpose.

Use affordances

Functions defined in the prompt that the model is explicitly instructed to use — for example, a calc() function to compute results inline.

Use formatting

Bullet points, headings, and subheadings help the model understand the structure of the prompt.

Use examples

Provide few-shot examples to illustrate the desired output format and help the model understand the requirements.

Use constraints

Limit the scope of output — specify length, language type, or format to keep responses on target.

Test and refine

Test the prompt with the model and refine based on the output. Identify issues and improve iteratively — prompt engineering is not a one-shot task.

06 Prompt Examples

1
Chain of Thought Prompting

Encourage AI models to be factually correct by following a series of steps in their reasoning.

prompt
What European soccer team won the Champions League the year Barcelona hosted the Olympic games? Use this format: Q: <repeat question> A: Let's think step by step <give_reasoning>. Therefore, the answer is <final_answer> Avoid Hallucination
2
Cite Sources to Avoid Hallucination

Ask the model to ground answers in reliable sources and make them cite those sources explicitly.

prompt
What are the top 3 most important discoveries that the Hubble Space Telescope has enabled? Answer only using reliable sources and cite those sources.
3
Using <|endofprompt|> to Continue the Conversation

Ask the model to continue from a specific point using the special token that signals the end of the instruction block.

prompt
Write a scary short story. <|endofprompt|> It was a beautiful winter day
4
Forceful Language

Language models do not always react well to nice, friendly language. If you really want them to follow some instructions, you might want to use forceful language. All caps and exclamation marks work.

If your model is giving you a hard time following instructions, use:

prompt
NO! That's 11! WRITE A SENTENCE WITH EXACTLY 12 WORDS! NOW!
5
Checking Factual Correctness

Ask the model to audit an article or piece of text for factual inaccuracies.

prompt
Is there any factually incorrect information in this article: <article>
6
Having a Conversation as a Person

Make sure to include the person's name in ALL CAPS to ask the model to remember the character they are supposed to be.

prompt
I will ask you questions and from now on you respond as if you were Buzz Lightyear from the movie Toy Story. It is REALLY IMPORTANT that you answer all questions as if you were Buzz, ok?

07 Conclusion

Prompt engineering is a critical component of using Large Language Models effectively. By crafting clear, specific, and well-structured prompts, you can improve the quality and relevance of the generated output.

By following the best practices for writing prompts, you can ensure that your prompts are effective and efficient in generating the desired output.

The key insight: models are only trained to predict the next word. The quality of the prompt is the quality of the output. Invest in the input.

08 References