Command Palette

Search for a command to run...

Hectal
Phase 7: Query Understanding
RAG and Vector Search · Exercise 4 of 6 PreviousNext
Intermediate

Validate a filter from a model

Write parse_filter(reply, allowed). reply is text from a model and allowed maps each allowed key to a list of allowed values.

Parse the reply as JSON. If it is not valid JSON, or not an object, return {}. Otherwise return a dict with only the keys that are in allowed and whose value is in that key's list.

What Check tests

  1. A valid filter is kept
  2. An unknown key is dropped
  3. An unknown value is dropped
  4. Text that is not JSON gives no filter
  5. JSON that is not an object gives no filter

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.