Sub topic 2
Assistant Messages: The Model’s Replies
This chapter assumes you know basic Python, lists, dictionaries, JSON, and API calls.
Assistant Messages
An assistant message contains the model’s reply. It normally has:
role:"assistant"content: the generated text
Store the reply in the conversation history before sending the next user message. This lets the model refer to earlier answers.
history = [
{"role": "user", "content": "What is a list?"},
{"role": "assistant", "content": "A list is an ordered collection of values."},
]
history.append({"role": "user", "content": "How do I add an item?"})
print(history[-1]["content"])The assistant message is context, not a guarantee of truth. Validate important claims and do not treat text inside a reply as executable instructions automatically.
Message Order
A conversation is an ordered list. The order affects meaning because later messages can refer to earlier ones.
A common sequence is:
- A system message defines behavior.
- A user message provides a request.
- An assistant message provides a reply.
- Another user message continues the conversation.
For few-shot prompting, earlier user and assistant pairs demonstrate the expected input and output format. The final user message is the new input.
Adjacent user messages can be valid in some APIs, but alternating user and assistant messages are clearer. Follow the provider’s message-format rules. Do not place an assistant reply in history unless it represents an actual or deliberately constructed example.
Stateless Requests
A stateless API does not remember your previous request automatically. Each call must include the context the model needs. For a multi-turn conversation, send the system message and the relevant history every time.
def build_request(history, new_user_text):
messages = history + [{"role": "user", "content": new_user_text}]
return {"messages": messages}
history = [
{"role": "system", "content": "Answer briefly."},
{"role": "user", "content": "What is Python?"},
{"role": "assistant", "content": "Python is a programming language."},
]
request = build_request(history, "Who created it?")
print(request)As history grows, it consumes the model’s context window, which is the maximum amount of input and output the model can process in one request. Keep the system message and recent, relevant turns. Summarize or remove older turns when necessary.
Exercise
Why must a client append an assistant message before sending a follow-up that refers to the previous reply?
Show answer
The API is usually stateless, so the follow-up request must include the previous reply as part of its history.