Prompt design

Lesson 10 of 12

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 Free and free are 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

  1. Task 1, not checked yet. Make a variable draft with ask("Write a reply to the comment.", comment). Print it, and read it.

  2. 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 text too long: the reply has more than 30 words. The text promise: the reply in lower case has the text free or the text refund. The text no station: the text of station is not in the reply.

  3. Task 3, not checked yet. Put the rules into the prompt. Make a variable RULES with 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 variable reply with ask(RULES, comment), and a variable found with problems(reply, "Harbor Gate"). Print reply and found.

  4. Task 4, not checked yet. Write a function safe_reply(comment, station). It asks the model with RULES and the comment. When problems finds no problem for the reply and the station, the function returns the reply. When it finds a problem, the function returns FALLBACK.

Task 1 of 4

Make a variable draft with ask("Write a reply to the comment.", comment). Print it, and read it.

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.