Prompt-Engineering-Guide/guides/prompts-intro.md

8.6 KiB
Raw Blame History

Prompting Introduction

This guide covers the basics of standard prompts to provide a rough idea on how to use prompts to interact and instruct large language models (LLMs).

All examples are tested with text-davinci-003 (using OpenAIs playground) unless otherwise specified. It uses the default configurations, e.g., temperature=0.7 and top-p=1.

Topic: - Basic Prompts - A Word on LLM Settings - Standard Prompts - Prompt Elements - General Tips for Designing Prompts


Basic Prompts

You can already achieve a lot with prompts, but the quality of results depends on how much information you provide it. A prompt can contain information like the instruction or question you are passing to the model and including other details such as inputs or examples.

Here is a basic example of a simple prompt:

Prompt

The sky is

Output:

blue

The sky is blue on a clear day. On a cloudy day, the sky may be gray or white.

As you can see, the language model outputs a continuation of strings that make sense give the context "The sky is". The output might be unexpected or far from the task we want to accomplish.

This basic example also highlights the necessity to provide more context or instructions on what specifically we want to achieve.

Lets try to improve it a bit:

Prompt:

Complete the sentence: 

The sky is

Output:

 so  beautiful today.

Is that better? Well, we told the model to complete the sentence so the result looks a lot better as it follows exactly what we told it to do (“complete the sentence”). This approach of designing optimal prompts to instruct the model to perform a task is whats referred to as prompt engineering.

The example above is a basic illustration of whats possible with LLMs today. Todays LLMs are able to perform all kinds of advanced tasks that range from text summarization to mathematical reasoning to code generation.

Standard Prompts

We have tried a very simple prompt above. A standard prompt has the following format:

<Question>?

This can be formatted into a QA format, which is standard in a lot of QA dataset, as follows:

Q: <Question>?
A: 

Given the standard format above, one popular and effective technique to prompting is referred to as few-shot prompting where we provide exemplars. Few-shot prompts can be formatted as follows:

<Question>?
<Answer>

<Question>?
<Answer>

<Question>?
<Answer>

<Question>?

And you can already guess that its QA format version would look like this:

Q: <Question>?
A: <Answer>

Q: <Question>?
A: <Answer>

Q: <Question>?
A: <Answer>

Q: <Question>?
A:

Keep in mind that its not required to use QA format. The format depends on the task at hand. For instance, you can perform a simple classification task and give exemplars that demonstrate the task as follows:

Prompt:

This is awesome! // Positive
This is bad! // Negative
Wow that movie was rad! // Positive
What a horrible show! //

Output:

Negative

Few-shot prompts enable in-context learning which is the ability of language models to learn tasks given only a few examples. We will see more of this in action in the upcoming guides.

General Tips for Designing Prompts

Here are few some tips to keep in mind while you are designing your prompts:

The Instruction

You can design effective prompts for various simple tasks by using commands to instruct the model what you want to achieve such as “Write”, “Classify”, “Summarize”, “Translate”, “Order”, etc.

Keep in mind that you also need to experiment a lot so see what works best. Trying different instructions with different keywords, context, and data and see what works best for your particular use case and task. Usually, the more specific and relevant the context is to the task you are trying to perform, the better. We will touch on the importance of sampling and adding more context in the upcoming guides.

Others recommend that instructions are placed at the beginning of the prompt. Its also recommended that some clear separator like “###” is used to separate the instruction and context.

For instance:

Prompt:

### Instruction ###
Translate the text below to Spanish:

Text: "hello!"

Output:

¡Hola!

Specificity

Be very specific about the instruction and task you want the model to perform. The more descriptive and detailed the prompt is, the better the results. This is particularly important when you have a desired outcome or style of generation you are seeking. There arent specific tokens or keywords that lead to better results. Its more important to have a good format and descriptive prompt. In fact, providing examples in the prompt is very effective to get desired output in specific formats.

As an example, lets try a simple prompt to extract specific information from a piece of text.

Prompt:

Extract the name of places in the following text. 

Desired format:
Place: <comma_separated_list_of_company_names>

Input: "Although these developments are encouraging to researchers, much is still a mystery. “We often have a black box between the brain and the effect we see in the periphery,” says Henrique Veiga-Fernandes, a neuroimmunologist at the Champalimaud Centre for the Unknown in Lisbon. “If we want to use it in the therapeutic context, we actually need to understand the mechanism.""

Output:

Place: Champalimaud Centre for the Unknown, Lisbon

Input text is obtained from this Nature article.

Avoid Impreciseness

Given the tips above about being detailed and improving format, its easy to fall into the trap of wanting to be too clever about prompts and potentially creating imprecise descriptions. Its often better to be specific and direct. The analogy here is very similar to effective communication the more direct, the more effective the message gets across.

For example, you might be interested in generating a list of products to buy to prepare a BBQ. You might try something like:

Explain the concept prompt engineering. Keep the explanation short, only a few sentences, and don't be too descriptive.

Its not clear from the prompt above how many sentences to use and what style. You might still somewhat get good responses with the above prompts but the better prompt would be one that is very specific, concise, and to the point. Something like:

Use 2-3 sentences to explain the concept of prompt engineering to a high school student.

Next Section (Basic Prompting)