Lab: Saved Conversations
Due: Wednesday, December 2 at 11:59pm · Worth: 8 points
A chat app needs to remember which messages belong to each conversation. You’ll write SQL to save the chats, reopen them in order, and search their messages. The starter provides the API and browser interface.
Start
Download and extract the starter ZIP. Open the inner lab-conversations folder in VS Code and publish it as a new repository named lab-conversations. Activate your Python environment and install the dependencies.
python -m pip install -r requirements.txtOpen database.py and look at the two tables in init_db. The title of each chat is in conversations. Each row in messages holds one message, and its conversation_id refers back to the chat it belongs to.
Save and search conversations
Write the five unfinished functions in database.py. Use the supplied get_conversation as a working example. It opens the database with with connect(path) as db: and passes two arguments to db.execute: the SQL and a tuple supplying the values for its ? placeholders. It reads one row with fetchone() and returns a dictionary to the app. When you write a query that changes the database, the same with block commits the change when it finishes successfully.
- Create and list conversations. Use
INSERTto store a title. The cursor returned bydb.executehas alastrowidattribute that gives you the new ID. To list conversations, useSELECTandORDER BYto put the newest first. Usefetchall()to read the rows and return a dictionary for each one. - Save and retrieve messages. Store each message with its conversation ID, role, and text, and return the new message ID. To open a chat’s history, select just that chat’s messages in the order they were sent.
- Search across conversations. Join the two tables so each result includes the conversation title. To search for a word anywhere in a message, put
%on either side of the search term and pass that pattern as the value for aLIKE ?condition.
Each function’s docstring lists the fields to return and their order where it matters. Use the same placeholder pattern for titles and message text so a message like I'm ready is saved as typed. The SQL reading has examples of each query.
-
Create conversations and return their generated ids with
create_conversation. -
List conversations newest first with
list_conversations. -
Save user and assistant messages with
add_message. -
Return a conversation’s messages in sending order with
get_messages. -
Keep each conversation’s history separate and use the database path passed to the function.
-
Use a join in
search_messagesto include each matching message’s conversation title. -
Search by substring without regard to ASCII letter case. Handle empty searches and no matches.
-
Save changes permanently and preserve text containing quotes and newlines. Bind values separately from SQL.
Check and submit
Run python check.py from the lab-conversations folder. Once you see Database checks passed., run python app.py and open http://127.0.0.1:8000. Local mode supplies replies and works without an API key.
Create two conversations and mention “library” in both. Open each chat to check its history, then search for “library” and check the conversation titles in the results. Stop the server with Ctrl+C and restart it to see whether your chats were saved.
Commit and sync the starter files with your completed database.py. The supplied .gitignore keeps your database and .env file out of Git. On Gradescope, submit lab-conversations and the branch containing your work. See the submission guide.
Optional live replies
To try the same app with Groq, stop the server, copy .env.example to .env, enter your Groq key, and run python app.py --live. Open a saved conversation and ask a follow-up about an earlier message. Local mode is enough for full credit.