Model APIs

Lesson 2 of 16

Lesson ยท 12 min

Roles and the system message

Three roles

Each message has a role. There are three roles.

Role Who writes it What it is for
system You, the developer. Instructions that stay the same for every call: the job, the rules, and the form of the answer.
user The person who uses your program, or your program. The text for this call: a question, or the data to work on.
assistant The model. An answer that the model gave before. Lesson 5 uses it.

A system message is always the first message of the list.

RULES = "Summarize the text in 8 words."
comment = "The dock at Town Hall was full."

messages = [
    {"role": "system", "content": RULES},
    {"role": "user", "content": comment},
]

Why two messages

You can put the instructions and the data in one user message, as you did in lesson 1. Two messages are better for a program.

  • The instructions are in one place. You write them one time, and each call uses them.
  • The data stays data. Your program puts the comment of a rider in the user message, and it does not mix the comment with your instructions.
  • You can change the instructions and test them without a change to the rest of the program.

Say what you want

A model does what the messages say, and nothing tells it what you have in your head. If you send only a comment, the model does not know that you want a label. If you ask for a label but not for the form, you get a full sentence. A program cannot work with a sentence as well as with one word.

Compare these instructions. The answers are those of the simulated model of this course.

Instructions Answer
Classify the comment. I think this comment is a complaint. It has the word "full".
Classify the comment as one of: praise, complaint, question, other. Answer with one word. complaint

The second form names the allowed labels and the form of the answer. Your code can compare the answer with a label directly.

Your turn

main.py has four comments of riders. Do the tasks in order.

Tasks

4 to do

  1. Task 1, not checked yet. Call the model with one user message. Its content is the first comment, and nothing else. Store the text of the answer in a variable plain.

  2. Task 2, not checked yet. The model did not know what to do with the comment. Make a variable SYSTEM with these instructions: Classify the comment as one of: praise, complaint, question, other. Answer with one word.

  3. Task 3, not checked yet. Write a function classify(comment). It sends two messages: a message with the role system and the content SYSTEM, and then a message with the role user and the comment. It returns the text of the answer.

  4. Task 4, not checked yet. Make a list labels with the label of each comment in comments. Use your function.

Task 1 of 4

Call the model with one user message. Its content is the first comment, and nothing else. Store the text of the answer in a variable plain.

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.