0%

Capstone · practice

Build quiz

The quiz is the most finished part of the study assistant. Chapter 9’s make_quiz sends the quiz request through call_model, validates the reply as a QuizSet and asks once more if it is invalid, and Chapter 4’s format_quiz turns a QuizSet into numbered questions with lettered choices and an answers line. The quiz command needs one more thing: the person names the note, and may ask for a number of questions.

One change in reliable.py

Open reliable.py and find make_quiz. It is Chapter 9’s function with one change, supplied:

def make_quiz(note_text, *, client, budget, sleep, count=3):
    messages = [{"role": "system", "content": QUIZ_SYSTEM}, note_message(note_text, QUIZ_PROMPTS[count])]
    request = {"model": MODEL, "messages": messages, "response_format": QUIZ_FORMAT}

count is the number of questions, and QUIZ_PROMPTS in prompts.py holds one fixed prompt for each count from 1 to 5, such as Write two multiple-choice questions about the note below. A fixed prompt per count, rather than a number put into one sentence, means the scripted model has a reply ready for each of them. QUIZ_PROMPTS[3] is QUIZ_PROMPT itself, so a three-question quiz sends exactly what Chapters 4 to 9 sent. The rest of make_quiz is unchanged: it raises ValueError("The model sent no quiz text.") for a reply with no text, and a second invalid reply’s ValidationError reaches the caller.

quiz_command

quiz_command(note_id, count, *, client, budget, sleep) returns the quiz as text, or raises:

  1. Check the note id first. If note_id is not in note_ids(), raise FileNotFoundError(f"There is no note called {note_id}."), before any request.

  2. Read the note: text = read_note({"note_id": note_id}). read_note is Chapter 6’s tool function, so it takes a dictionary of arguments.

  3. Return format_quiz(make_quiz(text, client=client, budget=budget, sleep=sleep, count=count)).

Catch nothing. Lesson 6 turns each error into an exit code.

Why check the id yourself, when read_note already raises ValueError for a note that does not exist? In ask, a missing note is a tool error: the model asked for it, and the message There is no note called 99-magic. Call list_notes to see the ids. goes back to the model, which can try again. Here the person typed the id, so the program answers them directly, before it spends any tokens. FileNotFoundError is Python’s own error for “there is no such file”, and it gets its own exit code, 4, because it is the person’s typo, not a failed run.

note_ids() reads tools.NOTES_DIR, which main will point at the right folder with use_settings. Checking your work points it at a folder of notes you have never seen.

Run it

When quiz_command is written, run python stages.py. Lesson 4’s block asks for two questions about 03-lists, then for a quiz about 99-magic:

Lesson 4: quiz_command
--- 03-lists, 2 questions
1. For scores = [90, 72, 85], what is scores[-1]?
   a) 90
   b) 72
   c) 85

2. What does scores.append(60) return?
   a) The new list
   b) None
   c) 60

Answers: 1c, 2b
(tokens used: 448)
--- 99-magic
FileNotFoundError: There is no note called 99-magic.
Lesson 5: chat_command, typing two questions and then an empty line
Run stopped at the next function to build: chat_command is built in Lesson 5.

The two questions are the first two of the Chapter 4 quiz: the scripted model has one prepared reply for each count. The second call sent no request, because the id was checked first.

read_note already raises a ValueError for a note that does not exist. Why does quiz_command check note_ids() first?

The quiz reply is invalid, and so is the reply to make_quiz's one retry. What does quiz_command do?

Task

In study_assistant.py, write quiz_command(note_id, count, *, client, budget, sleep):

  1. If note_id is not in note_ids(), raise FileNotFoundError(f"There is no note called {note_id}.") before any request.

  2. Read the note with text = read_note({"note_id": note_id}).

  3. Return format_quiz(make_quiz(text, client=client, budget=budget, sleep=sleep, count=count)).

Catch nothing: errors from make_quiz reach the caller. Run python stages.py to see a two-question quiz and a note that does not exist, then check your work.