Sub topic 6
System, user, and assistant messages
This chapter assumes you can already send a basic chat completion to a local server through the OpenAI Python client, as in the previous chapter. You should also know that the server is stateless: it keeps no memory between requests.
The messages list
A chat completion request carries a messages list. Each item is a dictionary with a role and a content string. The model reads the whole list in order and writes the next message. Three roles matter here:
| Role | Who writes it | Purpose |
|---|---|---|
| `system` | Your code | Sets behavior, tone, and rules for the whole conversation |
| `user` | The person using your app | Asks questions or gives tasks |
| `assistant` | The model, or your code replaying earlier replies | Records what the model already said |
The system message usually comes first. Most chat templates expect it there, and some local models handle it poorly when it appears later in the list.
Conversation history is your job
The server does not remember earlier requests. To continue a conversation, you resend every earlier message each time, including the model's own replies with the assistant role.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local")
messages = [
{"role": "system", "content": "You are a concise assistant."},
]
def ask(question):
messages.append({"role": "user", "content": question})
response = client.chat.completions.create(
model="llama3.2",
messages=messages,
temperature=0.2,
)
reply = response.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
return reply
print(ask("My name is Priya. Remember it."))
print(ask("What is my name?"))The second call works because the list now holds four messages: the system message, the first question, the first reply, and the new question. If you skipped the append of the assistant reply, the model would see two user messages in a row and lose the thread.
Writing a useful system message
A system message works best when it is short and specific. Compare these two:
- Weak:
You are helpful. - Stronger:
You are a support assistant for a bicycle shop. Answer in at most three sentences. If you do not know, say so.
Small local models follow instructions less reliably than large hosted ones. Keep rules few, put the most important rule first, and test the result on your own model. Also avoid placing secrets in a system message. A user can often coax the model into repeating it.
Few-shot examples with fake turns
You can steer the format of replies by inserting example turns before the real question. These are ordinary user and assistant messages that you wrote yourself.
messages = [
{"role": "system", "content": "Classify the sentiment as positive or negative. Reply with one word."},
{"role": "user", "content": "I love this phone."},
{"role": "assistant", "content": "positive"},
{"role": "user", "content": "The battery died in an hour."},
{"role": "assistant", "content": "negative"},
{"role": "user", "content": "The screen is gorgeous."},
]The model sees a pattern and tends to continue it. This technique often helps small models more than extra instructions do.
Common errors
- Forgetting history. Each request starts blank, so the model "forgets" unless you resend earlier turns.
- Unbounded growth. The list grows every turn and eventually exceeds the model's context window. Servers may truncate silently or return an error. Trim the oldest user and assistant turns, and keep the system message.
- Wrong role names. Roles such as
botorhumanare rejected or ignored. Use onlysystem,user, andassistant. - Two user messages in a row, or an assistant message last. Some chat templates reject or mishandle these. End the list with a
usermessage when you want a new reply. - Template differences. Some models ignore the
systemrole or fold it into the first user message. If a rule is not followed, test by moving the same text into the first user message.
Exercise
A user chats with your app for ten turns. On turn eleven, the model acts as if it has never seen the conversation. Your code calls client.chat.completions.create(model=..., messages=[{"role": "user", "content": new_question}]). What is wrong, and how do you fix it?
Show answer
The request contains only the newest question, and the server keeps no state between requests. Keep one messages list, append each user message and each assistant reply to it, and send the whole list on every call. Begin the list with the system message.
Exercise
Your chat loop has run for hours and requests now fail or the model ignores early instructions. Name the likely cause and one way to handle it.
Show answer
The history has grown past the model's context window. Trim the oldest user and assistant messages while keeping the system message, or summarize older turns into a single short message.