Command Palette

Search for a command to run...

Hectal
Phase 8: Retrieval Basics (RAG)
LLM Engineering · Exercise 5 of 6 PreviousNext
Intermediate

Parse and check citations

Write citations(text) returning the list of integers found as [n] in the text, in order (repeats included). Brackets that do not hold only digits, like [x], are ignored.

Write check_reply(reply, n_sources) returning one string: "refusal" if the stripped reply equals REFUSAL; "no citation" if it has no citation; "bad citation N" for the first cited number N that is below 1 or above n_sources; otherwise "ok".

What Check tests

  1. citations finds numbers in order, repeats included
  2. Brackets without a number are ignored, and two digits work
  3. The refusal sentence gives "refusal"
  4. No citation gives "no citation"
  5. A source number out of range gives "bad citation N"
  6. Valid citations give "ok"

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.