Prompt design

Lesson 8 of 12

Lesson ยท 12 min

Templates and versions

A prompt is a part of the program

In the lessons before this one, each prompt was one long text. You made a new version with +. That works for a lesson. In a real program, it causes problems.

  • The same sentence is in many places. You change it in one place, and you forget the other places.
  • Nobody knows which text gave which result.
  • A small edit goes to the users with no test.

Treat a prompt as you treat code.

Build the prompt from parts

Keep each part in one place: the task, each rule, each example. One function builds the complete text from the parts.

RULE = "A comment that asks something is a question."

prompt = build_prompt([RULE], [])

Now a new version is a new list of parts. You can try a version with one rule less, or with the rules in another order, and you do not copy any text.

Give each version a name

Store the versions in a dictionary, with a name for each. Store the result of the test set under the same name.

Version What it has Accuracy on the dev set
v1 The task and the form of the answer. 66.7
v2 And the rule about questions. 75.0

A table of this kind is the history of the prompt. It tells you and your team why each part is there. When a part does not make the result better, take it out. A shorter prompt costs less.

A change to the prompt is a change to the program

Run the test set before a new version goes to the users, as you run the tests of your code. Many teams keep their prompts in the repository of the code. Then each change to a prompt has a date, an author, and a review.

Your turn

main.py has the parts of the prompt, the functions run and accuracy, and the function example_block of lesson 5. The results come from the simulated model, as in the lessons before. Do the tasks in order.

Tasks

4 to do

  1. Task 1, not checked yet. Write a function build_prompt(rules, examples). rules is a list of rule sentences, and examples is a list of pairs for example_block. The function returns the text of BASE, then each rule with one space in front of it. When examples is not empty, the text then gets a line break and the result of example_block(examples).

  2. Task 2, not checked yet. Make a dictionary VERSIONS with four prompts. Use build_prompt for each. v1 has no rule. v2 has RULE_QUESTION. v3 has RULE_QUESTION and RULE_MIXED. v4 has RULE_QUESTION, RULE_MIXED and RULE_NOT. No version has examples.

  3. Task 3, not checked yet. Make a dictionary results. It has the same keys as VERSIONS, and each value is the accuracy of that prompt on dev. Print it.

  4. Task 4, not checked yet. Make a variable best with the name of the version that has the highest accuracy in results.

Task 1 of 4

Write a function build_prompt(rules, examples). rules is a list of rule sentences, and examples is a list of pairs for example_block. The function returns the text of BASE, then each rule with one space in front of it. When examples is not empty, the text then gets a line break and the result of example_block(examples).

All tasks

Ctrl+Enter runs your code. Ctrl+Shift+Enter checks it. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.