Lesson ยท 16 min
Prompts for text
A prompt for text
The lessons before this one asked for one word. Many programs ask a model for text: a reply to a customer, a summary, a description. The method is the same. The rules are different.
A model that gets only Write a reply to the comment. must choose the tone, the length, and the content. It writes a friendly and long text, and it can promise things that your team did not allow. The simulated model promises a free ride.
Four kinds of rules for text
| Kind | Example |
|---|---|
| Tone | Use a friendly tone. |
| Length | Use at most 30 words. |
| Content that must be there | Name the station. |
| Content that must not be there | Do not promise money or a free ride. |
Write each rule as one short sentence. Say what the reader of the text needs, and what your team does not allow.
Check the text in code
A label is right or wrong. A text has no single right form, so you cannot compare it with one expected text. You can still check it.
- The number of words:
len(reply.split()). - Words that must not occur. Compare in lower case, so that
Freeandfreeare the same. - Names that must occur.
Some things code cannot check: whether the reply is true, and whether it is polite. A person reads samples for that, or a second call to a model gives a grade. The course Evaluating AI apps shows both ways.
A safe text for the bad case
Decide what the program sends when a reply breaks a rule. A short fixed text that promises nothing is a common choice. The lesson calls it FALLBACK. Lesson 8 of the course Model APIs used the same idea for labels.
The simulated model and text
The simulated model writes a reply from fixed parts, and it reads the four rules by their key words. A real model writes new text each time, and it follows your rules most of the time. So the check in code is more important with a real model, not less.
Your turn
main.py has a comment, the text FALLBACK, and the function ask. Do the tasks in order.
Tasks
4 to do
Task 1, not checked yet. Make a variable
draftwithask("Write a reply to the comment.", comment). Print it, and read it.Task 2, not checked yet. The team has three rules for a reply. Write a function
problems(reply, station)that returns a list of the rules that the reply breaks, in this order. The texttoo long: the reply has more than 30 words. The textpromise: the reply in lower case has the textfreeor the textrefund. The textno station: the text ofstationis not in the reply.Task 3, not checked yet. Put the rules into the prompt. Make a variable
RULESwith this text:Write a reply to the comment. Use a friendly tone. Name the station. Do not promise money or a free ride. Use at most 30 words.Make a variablereplywithask(RULES, comment), and a variablefoundwithproblems(reply, "Harbor Gate"). Printreplyandfound.Task 4, not checked yet. Write a function
safe_reply(comment, station). It asks the model withRULESand the comment. Whenproblemsfinds no problem for the reply and the station, the function returns the reply. When it finds a problem, the function returnsFALLBACK.
Task 1 of 4
Make a variable draft with ask("Write a reply to the comment.", comment). Print it, and read it.