Build a few-shot prompt

A few-shot prompt shows the model worked examples in exactly the layout of the real question.

What to build

A program that prints a few-shot prompt from an instruction, some examples and a query.

Requirements

  • Print the instruction, then an empty line.
  • For each example print Input: X, Label: Y and an empty line.
  • End with Input: QUERY and a last line that is just Label:.

Input

Line 1: the instruction. Line 2: k. Then k lines of the form X => Y. The last line is the query.

Output

The prompt as lines of text.

Constraints

0 ≤ k ≤ 20. No input or label contains the text =>.

Example

Input:

Label the review as positive or negative.
2
great phone => positive
it broke => negative
works well

Output:

Label the review as positive or negative.

Input: great phone
Label: positive

Input: it broke
Label: negative

Input: works well
Label:

Why it works

The model sees the same layout again and again, so its most likely continuation after the final Label: is a label in the same style.

Examples

Example 1 · Input
Label the review as positive or negative.
2
great phone => positive
it broke => negative
works well
Expected output
Label the review as positive or negative.

Input: great phone
Label: positive

Input: it broke
Label: negative

Input: works well
Label:
Example 2 · Input
Answer yes or no.
1
is ice cold => yes
is fire cold
Expected output
Answer yes or no.

Input: is ice cold
Label: yes

Input: is fire cold
Label:

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Start a list of lines with the instruction and an empty line.

Hint 2

Each example adds an `Input:` line, a `Label:` line and an empty line. The query adds `Input:` and a bare `Label:`.

Solution

Show one correct solution (try it yourself first)
instruction = input()
k = int(input())
lines = [instruction, ""]
for _ in range(k):
    x, y = input().split(" => ")
    lines += [f"Input: {x}", f"Label: {y}", ""]
query = input()
lines += [f"Input: {query}", "Label:"]
print("\n".join(lines))

Lesson: Prompt engineering →