Prompt engineering

Learn how to get the most out of your language model

Language models are great for generating text. However, we’d like to make sure the text adheres to our needs as developers. In many use cases, we’d like to feed the model with some input text and get an output text that satisfies the product need. To do so, you need to engineer the perfect prompt.

As an example, let's say you own an online retail platform. Your job requires you to write mass amounts of product descriptions, and you'd like to automate this process as much as possible. You can optimize the process by using AI21 Studio language models. Think of the models as smart autocompletion machines: you feed in text, and the model will generate text that seems natural. Your job is to ensure that the desired output (product descriptions, in this example) are the most natural continuation. In other words, you need to create the right input to the model, also known as a prompt. So, what are your tools to make that happen?

0️⃣ Prompt with instructions (aka Zero-shot prompts)

This is the simplest approach and the one that feels the most natural.

Why? This model was specifically trained to follow natural language instructions. It is the most natural way to interact with large language models: simply tell the model what you want it to do, and it will accordingly follow the instructions.

When? Drafting, seeking for inspiration, or when the format and guidelines are "work in progress".

How? Just provide an instruction to the model, like this:

PROMPT Write an engaging product description for a T-shirt for a clothing eCommerce site:

Feeding this text into the model, you’ll get a nice completion:

COMPLETION This T-shirt is perfect for any occasion. It's made of high-quality materials and it's comfortable to wear. It's also stylish and versatile, so you can wear it with anything.

The model can generate nice descriptions based only on instructions, but this is not a realistic business scenario. A more realistic scenario is when you have a product name and some features, and you want a description that highlights those features.

You can modify your prompt by including the specific features and highlights you wish to incorporate:

PROMPT Write an engaging product description for a clothing eCommerce site. Make sure to include the following features in the description. Product: Humor Men's Graphic T-Shirt. Features: - Soft cotton - Short sleeve - Have a print of Einstein's quote: "artificial intelligence is no match for natural stupidity” Description:

As you can see, the generated description includes all the features we wanted to highlight:

COMPLETION This Humor Men's Graphic T-Shirt is perfect for anyone who wants to make a statement. The soft cotton and short sleeve make it comfortable to wear, and the print of Einstein's quote is sure to get laughs. Whether you're wearing it to a party or just out and about, this shirt is sure to turn heads.

If you want, you can add more details and guidance to the model in your prompt. For example:

PROMPT Product: Humor Men's Graphic T-Shirt. Details: - Soft cotton - Short sleeve - Have a print of Einstein's quote: "artificial intelligence is no match for natural stupidity" ## Write an engaging product description for an eCommerce clothing site based on the properties which are listed above. Make sure to: - Focus on the ideal buyer - Entice with benefits - Appeal to the reader's imagination Product Description:

This description seems more aligned with the provided guidelines:

COMPLETION This soft cotton t-shirt is perfect for fans of the classic scientist, Albert Einstein. The quote "artificial intelligence is no match for natural stupidity" is a reminder that sometimes the most intelligent people are the most foolish. So whether you're a science nerd or just a fan of witty quotes, this t-shirt is for you.

🎵 Notes

  • Recency bias: In the third example, we placed the instruction at the end of the prompt, rather than at the beginning. It is due to LLMs' recency bias, where the ends of prompts affect completion more than the beginnings. In longer prompts, it is a good idea to place the instruction at the end.
  • Stop sequence: Notice the ## in the third example, which separates the instruction from the features to include? So did the model 😉. Adding a separator between prompt parts can help the model understand the task.

✍ Prompt with examples (aka Few-shot prompts)

Look again at the output we got earlier. Maybe the description isn’t exactly what you imagined, and you want a different one. This can be challenging to specify in the prompt.. or maybe it’s easy, but the model doesn't exactly meet your requirements. A powerful solution would be to use the few-shot approach.

Why? Examples are helpful in assisting the model to comprehend and generate responses that adhere to the intended format.

When? Examples are particularly useful when there are stringent format constraints, a well-defined objective, and an overall structure to be maintained.

How? To establish a pattern for the model to follow, present a few instances (“shots”) of input-output pairs in the prompt. This enables the model to mimic the pattern. Then, provide the input for a query example and allow the model to generate a suitable completion. This approach is commonly referred to as a "few-shot prompt".

In your e-commerce platform, you probably have existing descriptions you perfected over time and want to maintain the same spirit. You can take some of them and format them into a template of input-output pairs. This will create a prefix of a few examples, followed by a query (where all constitute the prompt). This is how the prompt will look:

PROMPT Write an engaging product description for a clothing eCommerce site. Make sure to include the following features in the description. Product: Camp Director Crossbody Bag Features: - Canvas purse - Rainbow space print - Leather trim - Two securely-zipped compartments Description: This Camp Director Crossbody Bag is the perfect accessory for carrying your essentials in style. The canvas purse features a colorful space print with leather trim, and two securely-zipped compartments to keep your belongings safe. The adjustable strap makes it easy to wear over the shoulder or crossbody, and the purse is the perfect size for holding your phone, wallet, and more. ## Write an engaging product description for a clothing eCommerce site. Make sure to include the following features in the description. Product: Women's Boho Beach Dress Features: - Midi dress - Swing hem - Slimming - Special for summer Description: Women's Boho Beach Dress The style of midi dress with swing hem is slimming, It will make your tummy look flatter. This dress for women could not only make you feel cool, but also perfectly present the curve of female waist. Even in hot summer, it still keeps your elegant. ## Write an engaging product description for a clothing eCommerce site. Make sure to include the following features in the description. Product: Humor Men's Graphic T-Shirt Features: - Soft cotton - Short sleeve - Have a print of Einstein's quote: "artificial intelligence is no match for natural stupidity" Description:

And here’s the generated result:

COMPLETION This Humor Men's Graphic T-Shirt is perfect for anyone who wants to make a statement. The soft cotton and short sleeve make it comfortable to wear, and the print of Einstein's quote is sure to get a chuckle. Whether you're a fan of science or just looking for a funny shirt, this shirt is a great choice.

Seems that the model has captured the tone from the examples we provided. You can try it yourself in the playground.

Another advantage of the few-shot approach is that we can tell the model when to stop. Did you notice the ## at the end of every description? Besides being a separator between the examples, which helps the model to distinguish between them, it also helps us in knowing when the model should stop generating more text. You can read more about stop sequences here.

🪄 Tips & Tricks

  • Check several phrasings: Small differences between prompts can have a large impact on the completion, even if the meaning is the same.
  • Change the temperature: This is an effective parameter that controls the creativity-accuracy tradeoff of the generation. You can read more about it here.
  • Make sure your examples are varied: With few-shot prompts, the model will be biased to follow the pattern in the examples. If you only provide examples of clothes descriptions and then ask the model to generate a car description, it won't be as effective.
  • Use the same structure/template: When using few-shot prompts, always use the same template for every example. Otherwise, the model will not recognize the pattern and you will get suboptimal results.