Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 7.6 — Routing
RAG and Vector Search · Example 1 of 1

A router: rules, a small classifier, a default

The orders route is a stand-in: a real one would look the booking up in a database.

Not every question belongs in the handbook index. Routing sends a question to the right place (documents, a database, or nowhere): clear rules first, then a small classifier, and a safe default when nothing is sure.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write route(question) that returns orders if any word matches lb followed by digits (use re.fullmatch), else docs. Print the route for three questions.

02

Write classify(scores, minimum, default) that returns the name with the highest score only when it reaches minimum and no other name has the same score. Otherwise return default. Print the result for three score dicts.

Explain it without notes

01

Why use rules before a classifier?

02

What should the default route be, and why?

03

Why does the example send "Can I cancel my booking and get a refund?" to the documents?

04

How would you test a router?

A router: rules, a small classifier, a default
Sign in to run this example in your browser.

Expected output

Where is my booking LB1042?                 orders  rule: order id lb1042
show my last invoice                        orders  classifier score 2
Hello!                                      none    classifier score 1
Do shops open on holidays?                  docs    default
What is the weather tomorrow?               none    classifier score 1
Can I cancel my booking and get a refund?   docs    default