How to prompt engineer

Aditya Challapally
Data Science at Microsoft
8 min readFeb 6, 2024

Prompt engineering is the art and science of crafting natural language prompts that guide Large Language Models (LLMs) to produce the best possible outputs for your application. A prompt is a piece of text that you give to an LLM as input, and the output is the text that the LLM generates as a response. For example, if you want to use an LLM to write a greeting message, you can give it a prompt like “Hello, my name is Alice and I am a” and the LLM might generate an output like “Hello, my name is Alice and I am a software engineer.”

AI-generated image using DALL·E 2 for prompt engineering.

What are the steps of prompt engineering?

Prompt engineering involves five steps that you can follow to create effective prompts for your LLM-based applications. These steps consist of gathering context, snippeting, dressing up the context, prioritizing, and generating output.

Step 1: Gathering context

This step is about collecting relevant information that can help the LLM understand your task and goal. For example, if you want to use an LLM to create a document, you can gather context such as the title, the subtitle, the main points, the audience, and the tone. You can also gather context from other sources, such as previous documents, templates, or examples.

Gathering context is important because it can help the LLM generate more accurate and relevant outputs. For example, if you want to use an LLM to create a document that summarizes a research paper, you can gather context such as the abstract, the introduction, the results, and the conclusion of the paper.

Let’s go with this example and suppose that you want to use an LLM to create a document that summarizes a research paper. You can gather context such as:

  • The title: How to use Generative AI and prompt engineering to create amazing applications
  • The subtitle: A beginner’s guide to building software with Large Language Models
  • The main points: What is Generative AI and why is it important? How can you build software with LLMs? What are the steps of prompt engineering? How can you learn more about prompt engineering?
  • The audience: People who are interested in Generative AI and prompt engineering but have no prior experience or knowledge.
  • The tone: Informal, friendly, and engaging.
  • The abstract, the introduction, the results, and the conclusion of the paper.

Step 2: Snippeting

This step is about selecting the most important and useful pieces of context that you want to include in your prompt. You don’t want to include too much or too little context, because that can confuse or distract the LLM. You can use techniques such as cutting, scoring, and filtering to choose the best snippets of context.

Cutting is the technique of removing the irrelevant or redundant parts of the context. For example, if you have gathered a lot of context, you can cut out the parts that are not related to your task, such as the details or references that are not essential for the summary.

Scoring is the technique of assigning a numerical value to each context snippet based on its importance and usefulness. For example, if you have gathered a lot of context, you can score each snippet based on how much it helps the LLM understand your task and goal, such as by using a scale from 1 to 10.

Filtering is the technique of keeping only the context snippets that meet a certain criterion or threshold. For example, if you have scored your context snippets, you can filter out the ones that have a low score, such as below 5.

Example: If you have gathered the following context snippets for your task of creating a document that summarizes a research paper, you can use cutting, scoring, and filtering to select the best ones, as shown:

Table: Example actions to do on context snipppets.

Step 3: Dressing up the context

This step is about formatting and presenting the context snippets in a natural and coherent way in your prompt. You can use techniques such as comments, headers, footers, and separators to organize and label your context snippets.

Comments consist of the technique of adding text that explains the purpose and goal of your prompt, akin to using the # symbol in Markdown. For example, you can add a comment that describes what you want the LLM to do, such as # Create a document that summarizes a research paper.

Headers and footers represent the technique of adding text that marks the beginning and end of your prompt, such as by using ### symbols. For example, you can add a header that says ### BEGIN PROMPT ### and a footer that says ### END PROMPT ### to indicate where your prompt starts and ends.

Separators involve the technique of adding text that divides your context snippets into different sections, such as by using # symbols. For example, you can add separators that label your context snippets, such as # Title, # Subtitle, # Main points, and so on.

Example: If you have selected the following context snippets for your task of creating a document that summarizes a research paper, you can use comments, headers, footers, and separators to format and present them in your prompt:

### BEGIN PROMPT ###

# Create a document that summarizes a research paper

# Title: How to use Generative AI and prompt engineering to create amazing applications

# Subtitle: A beginner’s guide to building software with Large Language Models

# Main points: What is Generative AI and why is it important? How can you build software with LLMs? What are the steps of prompt engineering? How can you learn more about prompt engineering?

# Audience: People who are interested in Generative AI and prompt engineering but have no prior experience or knowledge.

# Tone: Informal, friendly, and engaging.

# Abstract of the paper:

Generative AI is a type of artificial intelligence that can create new content, such as text, images, music, or code, based on some input or context. Generative AI is powered by Large Language Models (LLMs), which are neural networks that learn from huge amounts of data, such as all the documents on the internet. LLMs can understand natural language and produce coherent and relevant outputs for different tasks. In this paper, we show how to use Generative AI and prompt engineering to create amazing applications with LLMs. Prompt engineering is the art and science of crafting natural language prompts that guide LLMs to produce the best possible outputs for your application. We explain what prompt engineering is, why it is important, and how to do it. We also provide some resources and examples of prompt engineering for various tasks and domains.

### END PROMPT ###

Step 4: Prioritization

This step is about deciding which context snippets to include or exclude in your prompt, based on their priority and relevance. It’s important not to exceed the maximum length of the prompt, which is usually limited by the LLM’s capacity. You can use techniques such as sorting, deleting, and ordering to prioritize your context snippets.

Sorting is the technique of arranging your context snippets in a certain order based on their priority, such as by using alphabetical, numerical, or logical order. For example, you can sort your context snippets by putting the most important ones at the top, such as the title and the subtitle.

Deleting is the technique of removing the context snippets that are not essential or redundant, such as by using the cutting technique outlined above in step 2. For example, you can delete the context snippets that are already implied or provided by other snippets, such as the tone, which is already given by the audience.

Ordering is the technique of placing your context snippets in a logical and natural way, such as by following the order of the paper. For example, you can order your context snippets by putting the abstract, the introduction, the results, and the conclusion of the paper in that order.

Example: If you have formatted and presented context snippets for your task of creating a document that summarizes a research paper, you can use sorting, deleting, and ordering to prioritize them in your prompt.

Step 5: The AI does its thing

This step is about letting the LLM generate an output based on your prompt. You can use different LLMs that have different capabilities and speeds, depending on your application and preference. You can also use parameters such as temperature, top-k, and top-p to control the randomness and diversity of the output.

LLMs are the models that power Generative AI, such as Codex, GPT-3, or BERT. They have different capabilities and speeds, such as how well they can generate code, text, or images, and how fast they can generate them. For example, Codex specializes in generating code, GPT-3 is good at generating text, and BERT is good at understanding text. You can choose the LLM that suits your application and preference, such as by using the OpenAI Playground, which lets you experiment with different LLMs and prompts.

Temperature, top-k, and top-p are parameters that control the randomness and diversity of the output that the LLM generates. For example, a low temperature makes the LLM more likely to choose the most probable option, while a high temperature makes it more likely to choose a less probable option. A high top-k makes the LLM choose from a larger set of options, while a low top-k makes it choose from a smaller set of options. A high top-p makes the LLM choose from a set of options that have a cumulative probability above a certain threshold, while a low top-p makes it choose from a set of options that have a lower cumulative probability. You can adjust these parameters to generate different outputs, such as by using the OpenAI Playground, which lets you change these parameters and see how they affect the output.

Example: If you have created a prompt for your task of creating a document that summarizes a research paper, you can use an LLM such as GPT-3, which is good at generating text, to produce an output for your prompt. You can also use a low temperature, a high top-k, and a low top-p to generate a more deterministic and consistent output.

Summary

Prompt engineering with Generative AI represents powerful and promising tools that can help create amazing applications with Large Language Models (LLMs). In this article, I’ve shown how to use Generative AI and prompt engineering to produce useful output. I’ve explained what Generative AI and prompt engineering are, why they are important, and how to do them. I’ve also provided some resources and examples of prompt engineering for various tasks and domains. I hope that this paper inspires you to explore the world of Generative AI and prompt engineering, and to start building your own amazing applications with LLMs.

Aditya Challapally is on LinkedIn.

--

--