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
Task 1, not checked yet. Write a function
build_prompt(rules, examples).rulesis a list of rule sentences, andexamplesis a list of pairs forexample_block. The function returns the text ofBASE, then each rule with one space in front of it. Whenexamplesis not empty, the text then gets a line break and the result ofexample_block(examples).Task 2, not checked yet. Make a dictionary
VERSIONSwith four prompts. Usebuild_promptfor each.v1has no rule.v2hasRULE_QUESTION.v3hasRULE_QUESTIONandRULE_MIXED.v4hasRULE_QUESTION,RULE_MIXEDandRULE_NOT. No version has examples.Task 3, not checked yet. Make a dictionary
results. It has the same keys asVERSIONS, and each value is the accuracy of that prompt ondev. Print it.Task 4, not checked yet. Make a variable
bestwith the name of the version that has the highest accuracy inresults.
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).