AI support copilot project · Chapter 10 of 12
Conversations
By the end of this chapter you can store a conversation and its messages in Postgres, read back its last few turns in the right order, turn a follow-up such as "And for how long?" into a question that stands on its own, and answer it inside the conversation. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Ready for members
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of AI support copilot project are free. This one comes with the all labs plan.
Why you need this
Members do not write one perfect question. Tara asks "How do I pause my plan?", reads the answer, and then writes "And by when do I have to do it?". Sana's WhatsApp is full of messages like that: "what about Thane?", "is it free?", "how long does that take?". Each one means something only next to the message before it.
The copilot answers every question alone. This is chapter 9's POST /ask with Tara's second message:
{"question":"And by when do I have to do it?","answer":"I can only help with DabbaDrop: orders, delivery, refunds, plans and the menu.","found":false,"citations":[],"tokens":{"input":0,"output":0}}
Refused as off topic. The classifier was right: on its own, "And by when do I have to do it?" has nothing to do with DabbaDrop. The meaning is in the turn before it, and nothing in the app remembers the turn before. A model does not either: every chat request stands alone, and the model knows only what that one request carries.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
service.py: store a conversation and read its history
In app/conversations/service.py write three functions. start_conversation(db): add a Conversation with id secrets.token_hex(8), commit, return the id. add_message(db, conversation_id, role, content): add a Message (role "member" or "copilot") without committing. history(db, conversation_id, limit=6): raise AppError(404, "conversation_not_found", ...) when the conversation does not exist (db.get); otherwise return its last limit messages, oldest first, as {"role", "content"} dicts (select newest first by Message.id with a limit, then reverse).
The Check starts a conversation, adds a message it never commits, stores nine turns, reads the last 6 and the last 2, a new conversation's empty history, and a conversation that does not exist.
- Task 2
service.py: rewrite a follow-up as a standalone question
Write rewrite_question(ai, turns, question) in app/conversations/service.py. When no turn is the member's, return the question as it is, without a model call. Otherwise send one chat request (settings.chat_model, temperature=0) with REWRITE_INSTRUCTIONS as the system message, then each turn (a member's as "user", a copilot's as "assistant"), then the question as the last "user" message. Return the reply stripped, or the question when the reply is empty.
The Check rewrites "And by when do I have to do it?" after a question about pausing the plan and reads the request the lab model got, then gives your function an empty reply and a history with only the copilot's greeting.
- Task 3 · interview question
Interview: service.py, answer in a conversation
Write ask_in_conversation(db, ai, conversation_id, question) in app/conversations/service.py. turns = history(db, conversation_id) (a 404 for a conversation that does not exist); standalone = rewrite_question(ai, turns, question); result = answer_question(db, ai, standalone). Store the member's question as they typed it and the copilot's answer with add_message, commit, and return the result with "question" (as typed), "standalone_question" and "conversation_id" added.
The Check (its own history, add_message and rewrite_question, so this task stands alone) asks two questions in one conversation, reads what answer_question was asked and the stored messages, then a conversation that does not exist, then POST /conversations.
Kunal will ask: why rewrite the follow-up before searching, instead of searching with the follow-up as it is, or with the whole conversation pasted together?
What you will learn
- Memory is your job: a chat model keeps nothing between requests.
- Conversations in Postgres: an id that cannot be guessed, a row per message, and who commits.
- The last few turns: newest first in SQL, then reversed in Python.
- Chat roles: how stored turns become user and assistant messages.
- Rewriting a follow-up: one small model request that makes a standalone question, and why search needs it.
More in Ready for members
Build "Conversations" yourself
DabbaDrop answers the same customer questions all day. You build the copilot that answers them from its own help pages, with sources, and says so when it does not know. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of AI support copilot project are free. This one comes with the all labs plan.