logo
P
Prompt Master

Prompt 大师

掌握和 AI 对话的艺术

Examples

Prompt examples for summarization, extraction, Q&A, and more

The previous section covered the basics of prompting LLMs.

This section provides more examples of using prompts for different tasks and introduces key concepts along the way. The best way to learn is through examples. The following demonstrate how well-crafted prompts can handle different types of tasks.

Topics:


Text Summarization

Text summarization is a standard NLG task that spans many styles and domains. One of the most promising LLM use cases is summarizing articles and concepts into quick, readable summaries. Here is a basic summarization task.

Prompt:

Explain antibiotics

A:

Output:

Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body's immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

The A: is an explicit Q&A prompt format -- it signals the model that you expect an answer.

Now say you feel the model gave too much info and you want to distill it further. You can instruct the model to summarize in one sentence:

Prompt:

Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body's immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

Explain the above in one sentence:

Output:

Antibiotics are medications used to treat bacterial infections by either killing the bacteria or stopping them from reproducing, but they are not effective against viruses and overuse can lead to antibiotic resistance.

Without worrying too much about accuracy here (we will cover that later), the model compressed the paragraph into one sentence. You can fine-tune the instruction further, or experiment on your own to see if you can get better results.


Information Extraction

LLMs are primarily trained for text generation and related tasks, but they are also excellent at classification and other NLP tasks.

Here is a prompt that extracts information from a given paragraph:

Prompt:

Author-contribution statements and acknowledgements in research papers should state clearly and specifically whether, and to what extent, the authors used AI technologies such as ChatGPT in the preparation of their manuscript and analysis. They should also indicate which LLMs were used. This will alert editors and reviewers to scrutinize manuscripts more carefully for potential biases, inaccuracies and improper source crediting. Likewise, scientific journals should be transparent about their use of LLMs, for example when selecting submitted manuscripts.

Mention the large language model based product mentioned in the paragraph above:

Output:

The large language model based product mentioned in the paragraph above is ChatGPT.

By now it should be clear: you can get the model to do different tasks just by telling it what to do. AI product builders are already leveraging this to create powerful products and experiences.

Paragraph source: https://www.nature.com/articles/d41586-023-00288-7


Question Answering

One of the best ways to get specific answers from the model is to improve the prompt format. As discussed earlier, a prompt can combine instruction, context, input data, and output indicators for better results. These components are not mandatory, but the more specific your instructions, the better the results. Here is what a more structured prompt looks like:

Prompt:

Answer the question based on the context below. Keep the answer short and concise. Respond "Unsure about answer" if not sure about the answer.

Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.

Question: What was OKT3 originally sourced from?

Answer:

Output:

Mice.

Context source: https://www.nature.com/articles/d41586-023-00400-x


Text Classification

So far you have used simple instructions to perform tasks. As a prompt engineer, you will need to get better at providing instructions. But that is not everything -- for harder use cases, instructions alone will not cut it. You will need to think more about context and other elements like input data and examples.

Here is a text classification example:

Prompt:

Classify the text into neutral, negative or positive.

Text: I think the food was okay.
Sentiment:

Output:

Neutral

You gave a classification instruction, and the model responded with 'Neutral' -- correct. But say you need the label in a specific format: you want neutral (lowercase), not Neutral. How do you get that?

There are several approaches. Here, specificity is what matters -- the more info you give the prompt, the more controlled your results. Try providing examples to demonstrate the correct behavior:

Prompt:

Classify the text into neutral, negative or positive.

Text: I think the vacation is okay.
Sentiment: neutral

Text: I think the food was okay.
Sentiment:

Output:

neutral

This time the model returned neutral -- exactly what we needed. The example in the prompt helped the model stay specific in its output.

To show why specificity matters, check out this example and spot the issue:

Prompt:

Classify the text into nutral, negative or positive.

Text: I think the vacation is okay.
Sentiment:

Output:

Neutral

What is the problem? The model completely ignored the made-up label nutral and output Neutral instead -- it has a bias toward the correct spelling. But if you actually wanted nutral, you would need to add descriptions to your labels or include more examples in the prompt.


Conversation

You can do more interesting things with prompt engineering -- like telling the LLM how to behave, defining its intent and identity. This is especially useful when building conversational systems like customer support chatbots.

For example, here is a conversation system that gives technical, scientific answers. Notice how the instruction explicitly tells the model how to behave. This is sometimes called role prompting.

Prompt:

The following is a conversation with an AI research assistant. The assistant tone is technical and scientific.

Human: Hello, who are you?
AI: Greeting! I am an AI research assistant. How can I help you today?
Human: Can you tell me about the creation of blackholes?
AI:

Output:

Sure! Black holes are regions of spacetime where the gravitational force is so strong that nothing, not even light, can escape from it. They are created when a very massive star dies and its core collapses in on itself, forming a singularity of infinite density. The intense gravity of the singularity pulls in all the matter and radiation around it, creating the black hole.

Our AI research assistant sounds a bit too technical. Let us change that behavior and instruct the system to give more accessible answers:

Prompt:

The following is a conversation with an AI research assistant. The assistant answers should be easy to understand even by primary school students.

Human: Hello, who are you?
AI: Greeting! I am an AI research assistant. How can I help you today?
Human: Can you tell me about the creation of black holes?
AI:

Output:

Sure! Black holes are created when a very massive star runs out of fuel and collapses in on itself. The gravity of the star is so strong that nothing, not even light, can escape its pull. This creates a point in space-time with an intense gravitational force called a black hole.

Adding more examples could yield even better results.


Code Generation

LLMs are remarkably effective at code generation. You can pull off all sorts of code generation tasks with clever prompts. Here are a few examples.

First, a simple user greeting program:

Prompt:

/*
Ask the user for their name and say "Hello"
*/

Output:

let name = prompt("What is your name?");
console.log(`Hello, !`);

We did not even need to specify the programming language.

Let us level up. This next example shows how powerful LLMs can be with just a bit more prompt design:

Prompt:

"""
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
Create a MySQL query for all students in the Computer Science Department
"""

Output:

SELECT StudentId, StudentName
FROM students
WHERE DepartmentId IN (SELECT DepartmentId FROM departments WHERE DepartmentName = 'Computer Science');

Here you provided the database schema as data and asked it to generate a valid MySQL query.


Reasoning

Reasoning tasks are arguably the hardest thing for today is LLMs. This is also one of the most exciting areas, because reasoning unlocks all sorts of complex applications.

There has been solid progress on math-related reasoning tasks. But current LLMs can still struggle, so you often need more advanced prompt engineering techniques. Here are a few basic examples showing arithmetic ability.

Prompt:

What is 9,000 * 9,000?

Output:

81,000,000

Let us try something harder:

Prompt:

The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.

A:

Output:

No, the odd numbers in this group add up to an odd number: 119.

That is wrong. Let us improve it with a more explicit instruction:

Prompt:

The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.

Solve by breaking the problem into steps. First, identify the odd numbers, add them, and indicate whether the result is odd or even.

Output:

Odd numbers: 15, 5, 13, 7, 1
Sum: 41
41 is an odd number.

Much better. Quick note: we tried this task a few times and the model still fails occasionally. Better instructions combined with examples can help get more accurate results.


Notebook

If you want to practice the prompts above using Python, check out this notebook:

📚 相关资源