Sub topic 1
Chat Messages: System, User, and Assistant Roles
Prerequisites
This chapter assumes basic Python or JavaScript syntax, lists and dictionaries or arrays and objects, JSON, and a general idea of how a language model API works.
What a Chat Message Is: Role Plus Content
A chat message is one structured item in a conversation. It normally has two important fields:
roleidentifies who produced the message and how the API should interpret it.contentcontains the text or other data for that message.
A conversation is therefore an ordered list of messages:
messages = [
{
"role": "system",
"content": "Answer clearly and keep responses under three sentences."
},
{
"role": "user",
"content": "What is a REST API?"
}
]The order matters. The model uses earlier messages as context for later messages. A message is not a permanent memory item. Your application must send the relevant messages with each request.
The Three Core Roles at a Glance
The three common roles describe the conversation's participants:
| Role | Main purpose | Typical content |
|---|---|---|
| `system` | Set general behavior and constraints | Tone, format, safety rules |
| `user` | Provide a request or input | A question, task, or document |
| `assistant` | Represent a model response | The answer generated in an earlier turn |
The role is metadata. Putting “system” in the text of a user message does not make that text a system instruction.
System Messages: Setting Behavior and Constraints
A system message establishes the model's operating instructions. It can specify the audience, response format, tone, limits, and task boundaries.
system_message = {
"role": "system",
"content": (
"You explain programming to beginners. "
"Use short paragraphs and include one example."
)
}Keep stable instructions in the system message. Put changing requests in user messages. This separation makes the conversation easier to test and maintain.
User Messages: What You Ask the Model
A user message contains the current task or input. It can refer to earlier turns when those turns are included in the same request.
messages = [
{"role": "system", "content": "Answer in plain English."},
{"role": "user", "content": "Explain Python lists."},
{"role": "assistant", "content": "A list stores multiple values in order."},
{"role": "user", "content": "How do I add an item to one?"}
]The final user message depends on the earlier assistant message for the meaning of “one.” Without that history, the model may not know what the pronoun refers to.
Exercise
Which field identifies whether a message is a system instruction or a user request?
Show answer
The role field.