Skip to content

Commit ea5bab3

Browse files
committed
cleanup routing example
1 parent 6c50700 commit ea5bab3

File tree

1 file changed

+51
-78
lines changed

1 file changed

+51
-78
lines changed

python-recipes/semantic-router/00_semantic_routing.ipynb

Lines changed: 51 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
},
9898
{
9999
"cell_type": "code",
100-
"execution_count": 3,
100+
"execution_count": 17,
101101
"id": "aefda1d1",
102102
"metadata": {},
103103
"outputs": [],
@@ -132,7 +132,7 @@
132132
},
133133
{
134134
"cell_type": "code",
135-
"execution_count": 4,
135+
"execution_count": 18,
136136
"id": "c52d454a",
137137
"metadata": {},
138138
"outputs": [],
@@ -170,17 +170,17 @@
170170
},
171171
{
172172
"cell_type": "code",
173-
"execution_count": 5,
173+
"execution_count": 19,
174174
"id": "b986bf8d",
175175
"metadata": {},
176176
"outputs": [
177177
{
178178
"data": {
179179
"text/plain": [
180-
"RouteMatch(name='block_list', distance=0.375403106213)"
180+
"RouteMatch(name='block_list', distance=0.375402927399)"
181181
]
182182
},
183-
"execution_count": 5,
183+
"execution_count": 19,
184184
"metadata": {},
185185
"output_type": "execute_result"
186186
}
@@ -219,12 +219,12 @@
219219
"\n",
220220
"Each route has a set of references that cover the \"semantic surface area\" of the\n",
221221
"route. The incoming query from a user needs to be semantically similar to one or\n",
222-
"more of the references in order to \"match\" on the route."
222+
"more of the references in order to \"match\" on the route. Note that each route can have it's own distinct `distance_threshold` that defines what is considered a match for the particular query. "
223223
]
224224
},
225225
{
226226
"cell_type": "code",
227-
"execution_count": 6,
227+
"execution_count": 20,
228228
"id": "60ad280c",
229229
"metadata": {},
230230
"outputs": [],
@@ -239,7 +239,8 @@
239239
" \"tell me about the newest gadgets\",\n",
240240
" \"what's trending in tech?\"\n",
241241
" ],\n",
242-
" metadata={\"category\": \"tech\", \"priority\": 1}\n",
242+
" metadata={\"category\": \"tech\", \"priority\": 1},\n",
243+
" distance_threshold=0.5\n",
243244
")\n",
244245
"\n",
245246
"sports = Route(\n",
@@ -251,7 +252,8 @@
251252
" \"sports\",\n",
252253
" \"basketball and football\"\n",
253254
" ],\n",
254-
" metadata={\"category\": \"sports\", \"priority\": 2}\n",
255+
" metadata={\"category\": \"sports\", \"priority\": 2},\n",
256+
" distance_threshold=0.7\n",
255257
")\n",
256258
"\n",
257259
"entertainment = Route(\n",
@@ -261,7 +263,8 @@
261263
" \"who won the best actor award?\",\n",
262264
" \"what's new in the entertainment industry?\"\n",
263265
" ],\n",
264-
" metadata={\"category\": \"entertainment\", \"priority\": 3}\n",
266+
" metadata={\"category\": \"entertainment\", \"priority\": 3},\n",
267+
" distance_threshold=0.6\n",
265268
")"
266269
]
267270
},
@@ -277,7 +280,7 @@
277280
},
278281
{
279282
"cell_type": "code",
280-
"execution_count": 7,
283+
"execution_count": 21,
281284
"id": "e80aaf84",
282285
"metadata": {},
283286
"outputs": [],
@@ -306,7 +309,7 @@
306309
},
307310
{
308311
"cell_type": "code",
309-
"execution_count": 8,
312+
"execution_count": 22,
310313
"id": "3caedb77",
311314
"metadata": {},
312315
"outputs": [
@@ -348,17 +351,17 @@
348351
},
349352
{
350353
"cell_type": "code",
351-
"execution_count": 9,
354+
"execution_count": 23,
352355
"id": "5b0e3208",
353356
"metadata": {},
354357
"outputs": [
355358
{
356359
"data": {
357360
"text/plain": [
358-
"RouteMatch(name='technology', distance=0.119614601135)"
361+
"RouteMatch(name='technology', distance=0.419145862261)"
359362
]
360363
},
361-
"execution_count": 9,
364+
"execution_count": 23,
362365
"metadata": {},
363366
"output_type": "execute_result"
364367
}
@@ -371,7 +374,7 @@
371374
},
372375
{
373376
"cell_type": "code",
374-
"execution_count": 10,
377+
"execution_count": 24,
375378
"id": "ef90a1b8",
376379
"metadata": {},
377380
"outputs": [
@@ -381,7 +384,7 @@
381384
"RouteMatch(name=None, distance=None)"
382385
]
383386
},
384-
"execution_count": 10,
387+
"execution_count": 24,
385388
"metadata": {},
386389
"output_type": "execute_result"
387390
}
@@ -392,29 +395,6 @@
392395
"route_match"
393396
]
394397
},
395-
{
396-
"cell_type": "code",
397-
"execution_count": 11,
398-
"id": "a937b471",
399-
"metadata": {},
400-
"outputs": [
401-
{
402-
"data": {
403-
"text/plain": [
404-
"RouteMatch(name='sports', distance=0.554210424423)"
405-
]
406-
},
407-
"execution_count": 11,
408-
"metadata": {},
409-
"output_type": "execute_result"
410-
}
411-
],
412-
"source": [
413-
"# Toggle the runtime distance threshold\n",
414-
"route_match = multi_topic_router(\"Which basketball team will win the NBA finals?\", distance_threshold=0.7)\n",
415-
"route_match"
416-
]
417-
},
418398
{
419399
"cell_type": "markdown",
420400
"id": "c3f8600a",
@@ -425,44 +405,42 @@
425405
},
426406
{
427407
"cell_type": "code",
428-
"execution_count": 12,
408+
"execution_count": 34,
429409
"id": "70335f93",
430410
"metadata": {},
431411
"outputs": [
432412
{
433413
"data": {
434414
"text/plain": [
435-
"[RouteMatch(name='sports', distance=0.758580780029),\n",
436-
" RouteMatch(name='entertainment', distance=0.812423845132),\n",
437-
" RouteMatch(name='technology', distance=0.884235262871)]"
415+
"[RouteMatch(name='sports', distance=0.274198234081),\n",
416+
" RouteMatch(name='entertainment', distance=0.521298646927)]"
438417
]
439418
},
440-
"execution_count": 12,
419+
"execution_count": 34,
441420
"metadata": {},
442421
"output_type": "execute_result"
443422
}
444423
],
445424
"source": [
446425
"# Perform multi-class classification with route_many() -- toggle the max_k and the distance_threshold\n",
447-
"route_matches = multi_topic_router.route_many(\"Lebron James\", distance_threshold=1.0, max_k=3)\n",
426+
"route_matches = multi_topic_router.route_many(\"entertainment and sports\", max_k=3)\n",
448427
"route_matches"
449428
]
450429
},
451430
{
452431
"cell_type": "code",
453-
"execution_count": 14,
432+
"execution_count": 35,
454433
"id": "874b80fc",
455434
"metadata": {},
456435
"outputs": [
457436
{
458437
"data": {
459438
"text/plain": [
460-
"[RouteMatch(name='sports', distance=0.663254141808),\n",
461-
" RouteMatch(name='entertainment', distance=0.712985336781),\n",
462-
" RouteMatch(name='technology', distance=0.832674443722)]"
439+
"[RouteMatch(name='sports', distance=0.263298630714),\n",
440+
" RouteMatch(name='entertainment', distance=0.512374281883)]"
463441
]
464442
},
465-
"execution_count": 14,
443+
"execution_count": 35,
466444
"metadata": {},
467445
"output_type": "execute_result"
468446
}
@@ -471,7 +449,7 @@
471449
"# Toggle the aggregation method -- note the different distances in the result\n",
472450
"from redisvl.extensions.router.schema import DistanceAggregationMethod\n",
473451
"\n",
474-
"route_matches = multi_topic_router.route_many(\"Lebron James\", aggregation_method=DistanceAggregationMethod.min, distance_threshold=1.0, max_k=3)\n",
452+
"route_matches = multi_topic_router.route_many(\"sports and entertainment\", aggregation_method=DistanceAggregationMethod.min, max_k=3)\n",
475453
"route_matches"
476454
]
477455
},
@@ -487,33 +465,31 @@
487465
},
488466
{
489467
"cell_type": "code",
490-
"execution_count": 15,
468+
"execution_count": 36,
491469
"id": "86919de5",
492470
"metadata": {},
493471
"outputs": [],
494472
"source": [
495473
"from redisvl.extensions.router import RoutingConfig\n",
496474
"\n",
497475
"multi_topic_router.update_routing_config(\n",
498-
" RoutingConfig(distance_threshold=1.0, aggregation_method=DistanceAggregationMethod.min, max_k=3)\n",
476+
" RoutingConfig(aggregation_method=DistanceAggregationMethod.min, max_k=3)\n",
499477
")"
500478
]
501479
},
502480
{
503481
"cell_type": "code",
504-
"execution_count": 16,
482+
"execution_count": 37,
505483
"id": "cb883785",
506484
"metadata": {},
507485
"outputs": [
508486
{
509487
"data": {
510488
"text/plain": [
511-
"[RouteMatch(name='sports', distance=0.663254141808),\n",
512-
" RouteMatch(name='entertainment', distance=0.712985336781),\n",
513-
" RouteMatch(name='technology', distance=0.832674443722)]"
489+
"[RouteMatch(name='sports', distance=0.663253903389)]"
514490
]
515491
},
516-
"execution_count": 16,
492+
"execution_count": 37,
517493
"metadata": {},
518494
"output_type": "execute_result"
519495
}
@@ -533,7 +509,7 @@
533509
},
534510
{
535511
"cell_type": "code",
536-
"execution_count": 18,
512+
"execution_count": 38,
537513
"id": "f5ea2e61",
538514
"metadata": {},
539515
"outputs": [
@@ -545,27 +521,28 @@
545521
" 'references': ['what are the latest advancements in AI?',\n",
546522
" 'tell me about the newest gadgets',\n",
547523
" \"what's trending in tech?\"],\n",
548-
" 'metadata': {'category': 'tech', 'priority': '1'}},\n",
524+
" 'metadata': {'category': 'tech', 'priority': 1},\n",
525+
" 'distance_threshold': 0.5},\n",
549526
" {'name': 'sports',\n",
550527
" 'references': ['who won the game last night?',\n",
551528
" 'tell me about the upcoming sports events',\n",
552529
" \"what's the latest in the world of sports?\",\n",
553530
" 'sports',\n",
554531
" 'basketball and football'],\n",
555-
" 'metadata': {'category': 'sports', 'priority': '2'}},\n",
532+
" 'metadata': {'category': 'sports', 'priority': 2},\n",
533+
" 'distance_threshold': 0.7},\n",
556534
" {'name': 'entertainment',\n",
557535
" 'references': ['what are the top movies right now?',\n",
558536
" 'who won the best actor award?',\n",
559537
" \"what's new in the entertainment industry?\"],\n",
560-
" 'metadata': {'category': 'entertainment', 'priority': '3'}}],\n",
538+
" 'metadata': {'category': 'entertainment', 'priority': 3},\n",
539+
" 'distance_threshold': 0.6}],\n",
561540
" 'vectorizer': {'type': 'hf',\n",
562541
" 'model': 'sentence-transformers/all-mpnet-base-v2'},\n",
563-
" 'routing_config': {'distance_threshold': 1.0,\n",
564-
" 'max_k': 3,\n",
565-
" 'aggregation_method': 'min'}}"
542+
" 'routing_config': {'max_k': 3, 'aggregation_method': 'min'}}"
566543
]
567544
},
568-
"execution_count": 18,
545+
"execution_count": 38,
569546
"metadata": {},
570547
"output_type": "execute_result"
571548
}
@@ -576,27 +553,25 @@
576553
},
577554
{
578555
"cell_type": "code",
579-
"execution_count": 19,
556+
"execution_count": 42,
580557
"id": "36ae6f50",
581558
"metadata": {},
582559
"outputs": [
583560
{
584561
"name": "stdout",
585562
"output_type": "stream",
586563
"text": [
587-
"10:50:18 redisvl.index.index INFO Index already exists, not overwriting.\n"
564+
"11:18:33 redisvl.index.index INFO Index already exists, not overwriting.\n"
588565
]
589566
}
590567
],
591568
"source": [
592-
"router2 = SemanticRouter.from_dict(multi_topic_router.to_dict(), redis_url=\"redis://localhost:6379\")\n",
593-
"\n",
594-
"assert router2 == multi_topic_router"
569+
"router2 = SemanticRouter.from_dict(multi_topic_router.to_dict(), redis_url=\"redis://localhost:6379\")"
595570
]
596571
},
597572
{
598573
"cell_type": "code",
599-
"execution_count": 20,
574+
"execution_count": 43,
600575
"id": "f601b065",
601576
"metadata": {},
602577
"outputs": [],
@@ -606,22 +581,20 @@
606581
},
607582
{
608583
"cell_type": "code",
609-
"execution_count": 21,
584+
"execution_count": 44,
610585
"id": "63e4a847",
611586
"metadata": {},
612587
"outputs": [
613588
{
614589
"name": "stdout",
615590
"output_type": "stream",
616591
"text": [
617-
"10:50:43 redisvl.index.index INFO Index already exists, not overwriting.\n"
592+
"11:18:53 redisvl.index.index INFO Index already exists, not overwriting.\n"
618593
]
619594
}
620595
],
621596
"source": [
622-
"router3 = SemanticRouter.from_yaml(\"router.yaml\", redis_url=\"redis://localhost:6379\")\n",
623-
"\n",
624-
"assert router3 == router2 == multi_topic_router"
597+
"router3 = SemanticRouter.from_yaml(\"router.yaml\", redis_url=\"redis://localhost:6379\")"
625598
]
626599
},
627600
{

0 commit comments

Comments
 (0)