Module 2: Semantic Router¶
What you're building¶
Your agent currently answers everything, even off-topic questions like "Write me a Python script." In this module you'll add a semantic router that filters queries before they reach the AI.
You'll define two lists of example queries: an allow list for questions your agent should handle, and a deny list for ones it should block.
Setup¶
Enable the Semantic Router in your .env file:
Exercise¶
- Open
exercises/digital-native/semantic_router.pyin your IDE. - Add 2-3 more references to each route to improve classification. Each
Routehas:
| Field | What it does |
|---|---|
name |
"allow_list" lets queries through, "deny_list" blocks them |
references |
Example queries that represent this category |
distance_threshold |
How close a match must be (lower = stricter) |
Route(
name="allow_list",
references=[
"Where is my order?",
"I want a refund",
"My food was cold when it arrived",
"What restaurants are nearby?",
"Recommend me something to eat",
"What's the status of my delivery?",
"Can you help me?",
"What do you know about me?",
# Add 2-3 more food delivery queries...
],
distance_threshold=0.7,
),
Route(
name="deny_list",
references=[
"Help me write code for a delivery tracker",
"Write me a Python script",
"Tell me a joke",
# Add 2-3 more off-topic queries...
],
distance_threshold=0.5,
),
Click for a hint
For the allow list, think about late orders, refund policies, or greetings like "Hello". For the deny list, think about homework help, weather, sports scores, or creative writing.
Verify¶
Stop the running server (Ctrl+C) and restart:
Open localhost:3040.
- Ask: "Where is my order?" and click
Semantic-routerin the right activity panel to see it pass through - Ask: "Write me a Python script" and see it get blocked with a redirect message