How AI Language Models Work ยท Building with Models

Prompting: Shaping the Answer

A prompt is just text, but its shape matters: an instruction, a few worked examples, and an ending that invites exactly the answer you want.

prompting.py

# A prompt is just text, but its shape changes the answer a lot.
system = "You label reviews as positive or negative. Answer with one word."
examples = [("I loved it", "positive"), ("Waste of money", "negative")]
question = "Works great, would buy again"

prompt = system + "\n"
for review, label in examples:
    prompt += "Review: " + review + "\nLabel: " + label + "\n"
prompt += "Review: " + question + "\nLabel:"
print(prompt)

Step by step

  1. The instruction (system prompt): the job and the exact answer format.
  2. Two worked examples, one of each label.
  3. The review we actually want labelled.
  4. Start the prompt with the instruction.
  5. review is created with the value 'I loved it'. label is created with the value 'positive'.
  6. Add the first example as a Review/Label pair.
  7. review changes from 'I loved it' to 'Waste of money'. label changes from 'positive' to 'negative'.
  8. And the second.
  9. Python moves to the next item of the loop: for review, label in examples.
  10. Finally the new review, ending with "Label:" and nothing after it.
  11. The finished prompt. A model reading it will almost certainly continue with " positive". Same model, same question: a better prompt gives a more reliable answer.