I build the request by putting a system message in front and spreading the entire history into the messages list, then print the complete thread.
history = []
def record(role, content):
history.append({"role": role, "content": content})
record("user", "My name is Ada.")
record("assistant", "Nice to meet you, Ada.")
record("user", "What is my name?")
system = {"role": "system", "content": "You are a helpful assistant."}
thread = [system, *history]
print("Messages in history: %d" % len(thread))
for m in thread:
print("%s: %s" % (m["role"], m["content"]))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
Messages in history: 4
system: You are a helpful assistant.
user: My name is Ada.
assistant: Nice to meet you, Ada.
user: What is my name?
Spreading ...history injects every past message, so the model reasons over the whole thread. You must send it all again on the next call.