Conversations and Instructions · practice
Keep a Conversation Going
In Lesson 1 you wrote a history by hand. A real conversation builds its history as it goes: send a question with everything said so far, read the reply, add both to the history, and repeat. This lesson builds the two functions that do that.
One turn at a time
A turn is one question and the reply to it: a user message followed by an assistant message. A conversation that starts with the study system message grows like this:
[system]
[system, user, assistant]
[system, user, assistant, user, assistant]
Two functions cover it:
next_request(history, question)builds the request body for a new question:{"model": MODEL, "messages": ...}, where the messages are the whole history followed by the question as ausermessage.add_turn(history, question, answer)returns the history with the finished turn added: the question as ausermessage, then the answer as anassistantmessage.
The demonstration in conversation.py uses them in a loop:
history = [system_message()]
for question in ["My name is Sam.", "What is my name?"]:
response = send_body(next_request(history, question))
history = add_turn(history, question, answer_text(response))
Notice the assignment in the last line. add_turn does not change history. It returns a new list, and the loop makes history refer to it.
Leave the caller’s list alone
Both functions must return a new list and leave the list they were given exactly as it was. Here is why that matters. Suppose next_request added the question to the caller’s list with append. The request fails because the service is down, and send_body raises. The caller’s history now ends with a question that never got an answer, and the next request sends it twice. Functions that return new lists cannot leave a history half-changed like that.
You have two ways to build a new list. + joins two lists into a new one and changes neither:
history = [{"role": "system", "content": "Be brief."}]
messages = history + [{"role": "user", "content": "What is a tuple?"}]
print(len(history), len(messages))
Or copy the list first and change the copy:
history = [{"role": "system", "content": "Be brief."}]
longer = history.copy()
longer.append({"role": "user", "content": "What is a tuple?"})
longer.append({"role": "assistant", "content": "An ordered collection you cannot change."})
print(len(history), len(longer))
Both print the original length first, because the original list was never touched. Use either in your functions. Calling append on history itself would change the caller’s list, which is the thing to avoid.
Use the MODEL constant in the body of next_request, as ask_model does in llm_basics.py. Keep the question and the answer exactly as they were given.
Run the conversation
Run python conversation.py. The loop sends My name is Sam., adds the turn, then sends What is my name? with the history in front of it:
A conversation that grows by whole turns:
You: My name is Sam.
Model: Nice to meet you, Sam.
You: What is my name?
Model: Your name is Sam.
The history now holds 5 messages.
Trimming a long history:
Before trimming:
[pl-scripted echo] No scripted reply matches this prompt, so here is what I received.
Messages: 12
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: How many messages did you get?
After trimming:
Run stopped at the next function to build: trim_history is built in Lesson 5.
The model answered the second question from the history your functions built: the system message and two whole turns, five messages. The demonstration then builds a longer conversation of five made-up turns and sends one more question. Twelve messages arrived. Every new turn adds two more, and the next two lessons deal with where that leads.
history holds three messages. You run longer = add_turn(history, "Why?", "Because."). Afterward, how many messages does each list hold?
Task
In conversation.py, replace the unfinished bodies of add_turn and next_request.
add_turn(history, question, answer)returns a new list: every message inhistory, then{"role": "user", "content": question}, then{"role": "assistant", "content": answer}.next_request(history, question)returns{"model": MODEL, "messages": messages}, wheremessagesis a new list: every message inhistory, then{"role": "user", "content": question}.
Neither function may change the list it was given. Keep the question and the answer exactly as given. Run python conversation.py to see the conversation grow, then check your work.