Writing Better Prompts

In a world where everyone can be a programmer through natural language, the art of effective communication with Large Language Models (LLMs) becomes crucial. While machines comprehend plain English, nuances exist in crafting prompts tailored to the model’s interpretative abilities. This blog explores the emerging field of “Prompt Engineering,” delving into key methods for designing prompts that enhance understanding and task execution by LLMs. 1

Start Simple

While writing prompts, your goal should be to explain the task in simple statements. Like machine learning, prompt engineering is also an iterative process. You write, execute, refine and repeat, until you get a prompt that fulfills your requirements. For example: if you need to ask LLM to write letter, or you need it to generate one paragraph for each of your bullet points, the first step is to write an initial prompt that conveys model the task in minimum words:

TaskPrompt
Blog on SEOWrite a blog on SEO
Review of pizza placeWrite a review of [pizza place]

Prompt I/O

LLMs can be used to solve both simple and complex tasks. Some tasks require multiple inputs and outputs in a specific format. In addition to task instructions, the prompt also needs to define the format of these inputs and outputs. If this is not defined, the model may generate inconsistent outputs in different executions. Inputs should be defined within delimiters, and the prompt should specify how the input context is defined within these delimiters. You can instruct the model to generate structured outputs such as HTML or JSON.

PromptUpdated Prompt
What is the capital of India?Q. What is the capital of India?
A.
Convert the text into haiku. This is text for haikuConvert the text delimited by triple quotes into haiku. This is text for haiku
Rate the following text by the quality of grammar.Rate the following text by the quality of grammar. Rating should between 1 to 9. Generate output in JSON with key “rating”

Role Assignment

LLMs are trained with a huge amount of web data containing information from various domains. Thus, they are capable of mimicking various personas, styles, tones, and depths. You can use this to your advantage when performing complex tasks that only experts in the domain may know. Additionally, role assignment triggers the model to behave in a certain way that would otherwise require a large amount of instructions.

TaskPromptBetter Prompt
Generate blog on how to perform SEO for your site.Write a blog on SEO for my site.You are a SEO expert. Write a blog explaining what are the steps to improve the discovery of a site on search engine
Write a review of pizza place.Write a review of [pizza place].You are a food critic. Write a review of [random pizza place].
You are a food critic writing for the Michelin Guide. Write a review of [random pizza place].

Be Specific

Explain your requirements as clear as possible. Don’t keep anything vague.

PromptBetter Prompt
Explain the concept prompt engineering. Keep the explanation short, only a few sentences, and don’t be too descriptive.Use 2-3 sentences to explain the concept of prompt engineering to a high school student.
You’re a market analyst. Where should we sell our products?You’re a market analyst. Where in the U.S. should my company sell our camping gear products?
You’re a market analyst. What U.S. cities should we sell camping gear in?

Ask model to explain

LLMs are black boxes internally i.e. it cannot explain why a certain output is generated. This becomes troublesome specially when model generates incorrect answers. Imagine solving a quadratic questions or a complex physics problems. LLMs have a property of hallucination i.e. it can generate some content which is completely out of the context. Although these errors have reduced since initial models, you can still learn few techniques to reduce these errors. One method to solve this issue is to ask model to explain their thoughts or their work and generate the final answer. Research has suggested that adding statements like “Let’s think step by step” or “First explain the reason and then generate final answer” improves model’s accuracy.

PromptBetter Prompt
Solve x^2+34x+34=0Solve x^2+34x+34=0. Let’s think step by step
Classify the sentiment of the text into three classes positive, neutral or negative.Classify the sentiment of the text into three classes positive, neutral or negative. First explain the reason and then generate final result.

Multi-turn Prompting

For complex and hierarchical problem, ask the model to first retrieve all the relevant context and then focus on answering the main question in subsequent turns. This helps model to access their relevant information via information in prompt and reduces hallucinations. This is specially useful when the information for your prompt requires information from multiple domains

PromptBetter Prompt
Explain the impact of greenhouse effect on global warming/Turn 1: What is greenhouse effect?
Turn 2: What is global warming?
Turn 3: Now, what is the impact of greenhouse effect on global warming?
How does L1 regularization help in feature selection?Turn 1: What is feature selection in machine learning?
Turn 2: How does L1 regularization help in feature selection?

Other Techniques

Generate Shorter Text: In my personal experience, LLMs work better with shorter texts. With long text, generated text contains more hallucinations. Models have constraint with their input context window.

Don’t Don’t: You instructions should contains more of affirmative statements than negative statements. For example: statement like “Do no include incomplete lists” can be written as “Only include complete lists”

Using these in action:

Let’s apply these lessons to an example.

Task: Generate a review for pizza place. You need to generate this review to add into Google Maps.

Prompt V1: write a review of pizza place

Output:

Title: A Slice of Heaven: A Stellar Experience at Pizzarella

Rating: ★★★★★

Nestled in the heart of the city, Pizzarella is a hidden gem that promises a gastronomic journey through the world of delectable pizzas. From the moment you step inside, the inviting aroma of freshly baked dough and savory toppings envelops you, setting the stage for an unforgettable dining experience.

Ambiance (5/5): Pizzarella boasts a cozy and welcoming atmosphere, with warm lighting and tasteful decor that create the perfect setting for a casual meal or a special celebration. The vibrant murals depicting Italy’s picturesque landscapes add a touch of authenticity, transporting diners to the heart of pizza’s birthplace.

Service (4.5/5): The attentive and friendly staff at Pizzarella go above and beyond to ensure a pleasant dining experience. Our server was knowledgeable about the menu, offering helpful recommendations and accommodating special requests. The only minor hiccup was a slight delay in getting our orders taken during peak hours, but the staff’s professionalism and courtesy made up for it.

Menu Variety (5/5): Pizzarella takes pride in its diverse menu, offering a wide array of pizzas to cater to every palate. Whether you’re a traditionalist who favors a classic Margherita or an adventurous soul seeking unique flavor combinations, Pizzarella has something to satisfy every pizza craving. The menu also features a variety of appetizers, salads, and desserts for those looking to complement their pizza experience.

Food Quality (5/5): The star of the show, of course, is the pizza, and Pizzarella does not disappoint. The crust strikes the perfect balance between crispy and chewy, providing a delightful foundation for the fresh and high-quality toppings. The cheese is gooey and indulgent, while the tomato sauce bursts with a rich, savory flavor. We tried the Pizzarella Special, a signature creation featuring a harmonious blend of Italian sausage, roasted vegetables, and truffle-infused olive oil – a true masterpiece.

Value for Money (4.5/5): While Pizzarella may be slightly on the upscale side, the quality of ingredients and the overall dining experience justify the price. The generous portions and the option to customize your pizza make it a worthwhile investment for pizza enthusiasts seeking a premium culinary experience. In conclusion, Pizzarella is a pizza lover’s paradise, offering a perfect fusion of ambiance, service, and, most importantly, mouthwatering pizzas. Whether you’re celebrating a special occasion or simply craving a top-notch slice, Pizzarella is undoubtedly worth a visit. Prepare to embark on a culinary journey that will leave your taste buds dancing with delight.

Thought: The output is too big for Google Maps. You need to make it shorter. The name of pizza place is Domino’s, not Pizzarella

Bad Prompt: Generate a short review for pizza place for Domino’s

Prompt V2: Generate a review for a pizza place named Domino’s. The review should contain a title (10 words) and review (5 sentences)

Output:

Title: “Domino’s Delivers Delicious Delights: A Pizza Lover’s Paradise!”

Review: Domino’s has truly won my heart with their delectable pizzas. The crust is perfectly crisp, and the toppings are generously piled on, creating a symphony of flavors with each bite. The delivery service is swift, ensuring that the pizzas arrive piping hot and fresh at my doorstep. The menu offers a variety of options, from classic Margherita to innovative specialty pizzas, satisfying every pizza craving imaginable. The affordability of their pizzas, along with the quality and convenience, makes Domino’s my go-to choice for a satisfying pizza experience.

Thought: Let’s see can be improve this review by assigning a role.

Prompt V3: You are an expert food critic. Generate a review for a pizza place named Domino’s. The review should contain a title (10 words) and review (5 sentences)

Output Title: “Domino’s Delight: A Satisfying Symphony of Flavorful Crust and Toppings” Review: Domino’s continues to impress with its consistently delicious pizzas. The crust, a perfect blend of crispy and chewy, provides a delightful foundation for their imaginative toppings. The menu offers a wide array of choices, from classic pepperoni to innovative specialty pizzas, catering to diverse tastes. The quality of ingredients is commendable, with fresh and flavorful toppings that elevate each bite. With reliable delivery and a commitment to customer satisfaction, Domino’s remains a go-to choice for a reliably enjoyable pizza experience.

Thought: Assigning a role did not change a lot here. Let’s ask the model to generate output in a fixed format. To consume this information into an API, you can ask the model to generate this in json format.

Prompt V4: You are an expert food critic. Generate a review for a pizza place named Domino’s. The review should contain a title (10 words) and review (5 sentences). Generate output in JSON format with keys title and review.

Output:

{
"title": "Domino's Delight: A Slice of Satisfaction in Every Bite",
"review": "Domino's impresses with consistently delicious pizzas. The crust is perfectly crispy, providing a satisfying crunch with each bite. The variety of toppings is extensive, ensuring there's something for every palate. The delivery service is prompt, maintaining the pizza's freshness. With affordable prices and reliable quality, Domino's remains a top choice for pizza lovers."
}


Update 1: 31st July 2024, Added section on Multi-turn Prompting

References:

  1. Introduction is generated by ChatGPT https://chat.openai.com/share/045cfb54-31ad-42b9-be06-ce2e8b1799c0 ↩︎

Subscribe

Please enable JavaScript in your browser to complete this form.
Name