0%

Conversations and Instructions · capstone

Capstone project: Project: A Study Chat That Stays on Topic

You have every piece of a study chat: a system message, functions that grow a history by whole turns, and a trim that keeps it within budget. This project joins them into one function, chat_turn, that a chat program can call once per question:

history = []
reply, history = chat_turn(history, "What does len() return?")
reply, history = chat_turn(history, "What's the weather like today?")

The second reply stays on Python, because chat_turn makes sure the study system message comes first. The caller only keeps history and passes it back in. It never has to remember to add the system message, trim, or build a request body.

You write two functions. prepare_messages decides what to send, and chat_turn sends it and builds the new history.

prepare_messages(history, question)

It returns the list of messages to send for a new question:

  1. Start from a copy of history, so the caller’s list is never changed.

  2. If the copy is empty, or its first message is not a system message, put system_message() in front of it. If the history already starts with a system message, keep that one as it is, even if its text is different from STUDY_SYSTEM: a caller that chose its own instructions should get them.

  3. Add the question with next_request, and take the "messages" from the body it returns.

  4. Return those messages trimmed with trim_history(messages, MAX_MESSAGES).

Do the steps in that order: put the system message in first, then trim. If you trimmed first, the system message would arrive afterward, uncounted, and a list that just fit the limit would go over it by one.

chat_turn(history, question, *, session=None)

It asks the question and returns two things, the reply text and the new history, as a tuple:

  1. messages = prepare_messages(history, question).

  2. Send {"model": MODEL, "messages": messages} with send_body, passing session=session along, as ask_model does.

  3. Read the reply with answer_text.

  4. Return (reply, add_turn(messages[:-1], question, reply)).

The last step needs a closer look. messages ends with the question as a pending user message, because that is what was sent. messages[:-1] is everything except that last message, and add_turn adds the question back together with the reply as one whole turn. The new history is therefore what was actually sent, already trimmed, plus the finished turn.

chat_turn catches nothing. If the request fails, the error reaches the caller, and since no list was changed, the caller’s history is exactly as it was before the call.

Run the chat

Run python conversation.py. The demonstration starts from an empty history and asks three questions in turn:

The study chat:
  You: What does len() return?
  Model: len() returns the number of items in a sequence or collection, such as the characters in a string or the elements in a list.
  You: What's the weather like today?
  Model: I can only help with Python and the course notes. Would you like to look at how loops work instead?
  You: What is a Python list?
  Model: A list is an ordered, changeable collection of values. You write one with square brackets, like [1, 2, 3], and you can add, remove and change items after creating it.
  The history now holds 7 messages.

Without a system message, the scripted model answers the weather question with I can't check the weather. I don't have access to live information. In the chat it redirects to Python, because the study system message came first. The history ends with seven messages: the system message and three whole turns.

The demonstration shows the happy path. Before you check your work, try the cases that matter in the : a history whose first message is a different system message, a history that has grown past MAX_MESSAGES, and a check that the list you passed in is unchanged afterward. prepare_messages sends nothing over the network, so you can call it directly. Start Python with python, as in Lesson 6, and try the first case:

from conversation import prepare_messages
print(prepare_messages([{"role": "system", "content": "Reply in Spanish."}], "Hola"))

The output should be two messages: the Spanish system message, not the study one, followed by the question Hola. If you get stuck on one of them, ask Monty for a hint about that case without asking for the whole function.

Chapter 3 keeps prepare_messages as it is, starting from a finished copy of this file, and changes only how the reply arrives: piece by piece, as the model writes it.

prepare_messages puts the system message in first and trims afterward. What goes wrong if it trims first and inserts the system message afterward?

Why does chat_turn return add_turn(messages[:-1], question, reply) and not add_turn(messages, question, reply)?

Task

In conversation.py, replace the unfinished bodies of prepare_messages and chat_turn.

  • prepare_messages(history, question): start from a copy of history. If it is empty or its first message is not a system message, put system_message() in front; a different system message that is already first stays as it is. Take next_request(that, question)["messages"] and return trim_history(those, MAX_MESSAGES). Put the system message in first, then trim.

  • chat_turn(history, question, *, session=None): messages = prepare_messages(history, question); send {"model": MODEL, "messages": messages} with send_body(..., session=session); reply = answer_text(response); return (reply, add_turn(messages[:-1], question, reply)).

Neither function may change the caller’s list, and chat_turn catches no exceptions. Run python conversation.py to see the study chat stay on topic, then check your work.