Start learning free

LESSON 67 OF 129 · AI ENGINEERING: RETRIEVAL (RAG)

Filter by Category

Practice

A customer asks When will my order arrive? and the assistant says it doesn't know. The answer is in shipping.md, but the word order is all over the shop's policy documents. A privacy paragraph and a returns paragraph took the top places, and the paragraph that answers came fourth. Only three are sent to the model.

The fix is a filter. Every document has a category (policy, product or shipping), listed in docs/categories.json. When a question is clearly about one category, you keep only the chunks of that category before the ranking. The other chunks never get a score.

The code tells a question's category from a few telling words in the dictionary CATEGORY_WORDS. It names a category only when the words point to exactly one. Two questions about a lamp show the difference:

Is the lamp LP-310 dimmable?      lamp, lp-310: both product          →  filter on product
My lamp broke, can I return it?   lamp: product, return: policy      →  no filter, rank everything

A wrong guess would hide the chunk that holds the answer, so two categories mean no guess. A question with no telling word is ranked against all the chunks, as before.

When your code is finished, the program prints:

paragraph 27 chunks, 5 of 5
Best score: 0.4
Orders placed before 2 pm ship the same day, and standard delivery takes 3 to 5 business days.
Source: shipping.md

The check also calls find_category with five questions:

How long does delivery take?           →  shipping
Is the teapot TP-220 dishwasher safe?  →  product
Can I pay with PayPal?                 →  policy
My teapot cracked, is it covered?      →  None
Can I get my money back?               →  None

Your task: give every chunk the category of its document, write find_category (the question's category, or None) and in_category (the chunks of one category), and use both at the top of top_matches, before any chunk gets a score.


Assignment
Follow the Coding Tutorial to tag every chunk with its category, write find_category and in_category, and filter the chunks in top_matches before they are ranked.


Hint
find_category names a category only when it found exactly one. For none or for two it returns None, and top_matches then ranks every chunk, as it did before.


STEP 1 OF 11
    Ctrl + Enter to check