Conversations and Instructions · practice
System, User and Assistant Messages
Your history from Lesson 1 used two roles. A chat request has a third, and it is the one that lets your program, rather than the person typing, decide how the model should behave.
Three roles
Every message in messages has a role:
useris what the person typed: a question, a request, or text they pasted in.assistantis something the model said earlier in the conversation. Your program puts the model’s replies back in with this role, as you did in Lesson 1.systemis an instruction from your program about how the model should behave for the whole conversation: what it is for, what to stay away from, how to answer.
The person using your program never types the system message. You write it once, in your code, and send it with every request. The model treats it as standing instructions that apply to every user message after it.
The system message goes first
Put the system message at the start of messages, before any user or assistant message, and send only one:
messages = [
{"role": "system", "content": "You are a patient Python tutor."},
{"role": "user", "content": "My name is Sam."},
{"role": "assistant", "content": "Nice to meet you, Sam."},
{"role": "user", "content": "What is my name?"},
]
for message in messages:
print(message["role"])
A system message is a strong steer, not a rule the model is forced to follow. Models are trained to give it weight, and good instructions are followed most of the time, but a determined user message can still pull a model off course. Lesson 6 looks at what makes an instruction work, and Lesson 7 at text that tries to override it.
The study system message
conversation.py already holds the system message this chapter’s study chat uses, in the constant STUDY_SYSTEM:
STUDY_SYSTEM = (
"You are a patient Python tutor. Answer only questions about the course "
"notes and Python. If a question is about something else, say so and "
"suggest a Python topic instead."
)
The parentheses let one long string continue over several lines: Python joins string literals that sit next to each other into one string. Lesson 6 explains why it is worded the way it is.
Build system_message() so it returns that text as a chat message: a dictionary with "role" set to "system" and "content" set to STUDY_SYSTEM. Use the constant rather than typing the text again, so there is one place to change the wording. Return a new dictionary on every call, for the same reason as in Lesson 1: a caller that changes the message it got must not change the next caller’s.
See it arrive
Run python conversation.py. The demonstration sends your system message followed by a user message, Hello. Hello is not a prepared question, so the echo reports what arrived:
A system message first:
[pl-scripted echo] No scripted reply matches this prompt, so here is what I received.
Messages: 2
System message: You are a patient Python tutor. Answer only questions about the course notes and Python. If a question is about something else, say so and suggest a Python topic instead.
Last user message: Hello
A conversation that grows by whole turns:
Run stopped at the next function to build: next_request is built in Lesson 3.
Two messages arrived: your system message, and Hello as the last user message. In Chapter 1 the echo said System message: (none).
Your program sends a system message that says "Answer only questions about Python." Who writes that message, and when does it apply?
In a request to llm-api, where does the system message go?
Task
In conversation.py, replace the unfinished body of system_message().
Return a new dictionary on every call, with exactly two keys: "role" set to "system" and "content" set to the STUDY_SYSTEM constant.
Run python conversation.py to see the echo report your system message, then check your work.