|
97 | 97 | }, |
98 | 98 | { |
99 | 99 | "cell_type": "code", |
100 | | - "execution_count": 3, |
| 100 | + "execution_count": 17, |
101 | 101 | "id": "aefda1d1", |
102 | 102 | "metadata": {}, |
103 | 103 | "outputs": [], |
|
132 | 132 | }, |
133 | 133 | { |
134 | 134 | "cell_type": "code", |
135 | | - "execution_count": 4, |
| 135 | + "execution_count": 18, |
136 | 136 | "id": "c52d454a", |
137 | 137 | "metadata": {}, |
138 | 138 | "outputs": [], |
|
170 | 170 | }, |
171 | 171 | { |
172 | 172 | "cell_type": "code", |
173 | | - "execution_count": 5, |
| 173 | + "execution_count": 19, |
174 | 174 | "id": "b986bf8d", |
175 | 175 | "metadata": {}, |
176 | 176 | "outputs": [ |
177 | 177 | { |
178 | 178 | "data": { |
179 | 179 | "text/plain": [ |
180 | | - "RouteMatch(name='block_list', distance=0.375403106213)" |
| 180 | + "RouteMatch(name='block_list', distance=0.375402927399)" |
181 | 181 | ] |
182 | 182 | }, |
183 | | - "execution_count": 5, |
| 183 | + "execution_count": 19, |
184 | 184 | "metadata": {}, |
185 | 185 | "output_type": "execute_result" |
186 | 186 | } |
|
219 | 219 | "\n", |
220 | 220 | "Each route has a set of references that cover the \"semantic surface area\" of the\n", |
221 | 221 | "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. " |
223 | 223 | ] |
224 | 224 | }, |
225 | 225 | { |
226 | 226 | "cell_type": "code", |
227 | | - "execution_count": 6, |
| 227 | + "execution_count": 20, |
228 | 228 | "id": "60ad280c", |
229 | 229 | "metadata": {}, |
230 | 230 | "outputs": [], |
|
239 | 239 | " \"tell me about the newest gadgets\",\n", |
240 | 240 | " \"what's trending in tech?\"\n", |
241 | 241 | " ],\n", |
242 | | - " metadata={\"category\": \"tech\", \"priority\": 1}\n", |
| 242 | + " metadata={\"category\": \"tech\", \"priority\": 1},\n", |
| 243 | + " distance_threshold=0.5\n", |
243 | 244 | ")\n", |
244 | 245 | "\n", |
245 | 246 | "sports = Route(\n", |
|
251 | 252 | " \"sports\",\n", |
252 | 253 | " \"basketball and football\"\n", |
253 | 254 | " ],\n", |
254 | | - " metadata={\"category\": \"sports\", \"priority\": 2}\n", |
| 255 | + " metadata={\"category\": \"sports\", \"priority\": 2},\n", |
| 256 | + " distance_threshold=0.7\n", |
255 | 257 | ")\n", |
256 | 258 | "\n", |
257 | 259 | "entertainment = Route(\n", |
|
261 | 263 | " \"who won the best actor award?\",\n", |
262 | 264 | " \"what's new in the entertainment industry?\"\n", |
263 | 265 | " ],\n", |
264 | | - " metadata={\"category\": \"entertainment\", \"priority\": 3}\n", |
| 266 | + " metadata={\"category\": \"entertainment\", \"priority\": 3},\n", |
| 267 | + " distance_threshold=0.6\n", |
265 | 268 | ")" |
266 | 269 | ] |
267 | 270 | }, |
|
277 | 280 | }, |
278 | 281 | { |
279 | 282 | "cell_type": "code", |
280 | | - "execution_count": 7, |
| 283 | + "execution_count": 21, |
281 | 284 | "id": "e80aaf84", |
282 | 285 | "metadata": {}, |
283 | 286 | "outputs": [], |
|
306 | 309 | }, |
307 | 310 | { |
308 | 311 | "cell_type": "code", |
309 | | - "execution_count": 8, |
| 312 | + "execution_count": 22, |
310 | 313 | "id": "3caedb77", |
311 | 314 | "metadata": {}, |
312 | 315 | "outputs": [ |
|
348 | 351 | }, |
349 | 352 | { |
350 | 353 | "cell_type": "code", |
351 | | - "execution_count": 9, |
| 354 | + "execution_count": 23, |
352 | 355 | "id": "5b0e3208", |
353 | 356 | "metadata": {}, |
354 | 357 | "outputs": [ |
355 | 358 | { |
356 | 359 | "data": { |
357 | 360 | "text/plain": [ |
358 | | - "RouteMatch(name='technology', distance=0.119614601135)" |
| 361 | + "RouteMatch(name='technology', distance=0.419145862261)" |
359 | 362 | ] |
360 | 363 | }, |
361 | | - "execution_count": 9, |
| 364 | + "execution_count": 23, |
362 | 365 | "metadata": {}, |
363 | 366 | "output_type": "execute_result" |
364 | 367 | } |
|
371 | 374 | }, |
372 | 375 | { |
373 | 376 | "cell_type": "code", |
374 | | - "execution_count": 10, |
| 377 | + "execution_count": 24, |
375 | 378 | "id": "ef90a1b8", |
376 | 379 | "metadata": {}, |
377 | 380 | "outputs": [ |
|
381 | 384 | "RouteMatch(name=None, distance=None)" |
382 | 385 | ] |
383 | 386 | }, |
384 | | - "execution_count": 10, |
| 387 | + "execution_count": 24, |
385 | 388 | "metadata": {}, |
386 | 389 | "output_type": "execute_result" |
387 | 390 | } |
|
392 | 395 | "route_match" |
393 | 396 | ] |
394 | 397 | }, |
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 | | - }, |
418 | 398 | { |
419 | 399 | "cell_type": "markdown", |
420 | 400 | "id": "c3f8600a", |
|
425 | 405 | }, |
426 | 406 | { |
427 | 407 | "cell_type": "code", |
428 | | - "execution_count": 12, |
| 408 | + "execution_count": 34, |
429 | 409 | "id": "70335f93", |
430 | 410 | "metadata": {}, |
431 | 411 | "outputs": [ |
432 | 412 | { |
433 | 413 | "data": { |
434 | 414 | "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)]" |
438 | 417 | ] |
439 | 418 | }, |
440 | | - "execution_count": 12, |
| 419 | + "execution_count": 34, |
441 | 420 | "metadata": {}, |
442 | 421 | "output_type": "execute_result" |
443 | 422 | } |
444 | 423 | ], |
445 | 424 | "source": [ |
446 | 425 | "# 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", |
448 | 427 | "route_matches" |
449 | 428 | ] |
450 | 429 | }, |
451 | 430 | { |
452 | 431 | "cell_type": "code", |
453 | | - "execution_count": 14, |
| 432 | + "execution_count": 35, |
454 | 433 | "id": "874b80fc", |
455 | 434 | "metadata": {}, |
456 | 435 | "outputs": [ |
457 | 436 | { |
458 | 437 | "data": { |
459 | 438 | "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)]" |
463 | 441 | ] |
464 | 442 | }, |
465 | | - "execution_count": 14, |
| 443 | + "execution_count": 35, |
466 | 444 | "metadata": {}, |
467 | 445 | "output_type": "execute_result" |
468 | 446 | } |
|
471 | 449 | "# Toggle the aggregation method -- note the different distances in the result\n", |
472 | 450 | "from redisvl.extensions.router.schema import DistanceAggregationMethod\n", |
473 | 451 | "\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", |
475 | 453 | "route_matches" |
476 | 454 | ] |
477 | 455 | }, |
|
487 | 465 | }, |
488 | 466 | { |
489 | 467 | "cell_type": "code", |
490 | | - "execution_count": 15, |
| 468 | + "execution_count": 36, |
491 | 469 | "id": "86919de5", |
492 | 470 | "metadata": {}, |
493 | 471 | "outputs": [], |
494 | 472 | "source": [ |
495 | 473 | "from redisvl.extensions.router import RoutingConfig\n", |
496 | 474 | "\n", |
497 | 475 | "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", |
499 | 477 | ")" |
500 | 478 | ] |
501 | 479 | }, |
502 | 480 | { |
503 | 481 | "cell_type": "code", |
504 | | - "execution_count": 16, |
| 482 | + "execution_count": 37, |
505 | 483 | "id": "cb883785", |
506 | 484 | "metadata": {}, |
507 | 485 | "outputs": [ |
508 | 486 | { |
509 | 487 | "data": { |
510 | 488 | "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)]" |
514 | 490 | ] |
515 | 491 | }, |
516 | | - "execution_count": 16, |
| 492 | + "execution_count": 37, |
517 | 493 | "metadata": {}, |
518 | 494 | "output_type": "execute_result" |
519 | 495 | } |
|
533 | 509 | }, |
534 | 510 | { |
535 | 511 | "cell_type": "code", |
536 | | - "execution_count": 18, |
| 512 | + "execution_count": 38, |
537 | 513 | "id": "f5ea2e61", |
538 | 514 | "metadata": {}, |
539 | 515 | "outputs": [ |
|
545 | 521 | " 'references': ['what are the latest advancements in AI?',\n", |
546 | 522 | " 'tell me about the newest gadgets',\n", |
547 | 523 | " \"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", |
549 | 526 | " {'name': 'sports',\n", |
550 | 527 | " 'references': ['who won the game last night?',\n", |
551 | 528 | " 'tell me about the upcoming sports events',\n", |
552 | 529 | " \"what's the latest in the world of sports?\",\n", |
553 | 530 | " 'sports',\n", |
554 | 531 | " 'basketball and football'],\n", |
555 | | - " 'metadata': {'category': 'sports', 'priority': '2'}},\n", |
| 532 | + " 'metadata': {'category': 'sports', 'priority': 2},\n", |
| 533 | + " 'distance_threshold': 0.7},\n", |
556 | 534 | " {'name': 'entertainment',\n", |
557 | 535 | " 'references': ['what are the top movies right now?',\n", |
558 | 536 | " 'who won the best actor award?',\n", |
559 | 537 | " \"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", |
561 | 540 | " 'vectorizer': {'type': 'hf',\n", |
562 | 541 | " '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'}}" |
566 | 543 | ] |
567 | 544 | }, |
568 | | - "execution_count": 18, |
| 545 | + "execution_count": 38, |
569 | 546 | "metadata": {}, |
570 | 547 | "output_type": "execute_result" |
571 | 548 | } |
|
576 | 553 | }, |
577 | 554 | { |
578 | 555 | "cell_type": "code", |
579 | | - "execution_count": 19, |
| 556 | + "execution_count": 42, |
580 | 557 | "id": "36ae6f50", |
581 | 558 | "metadata": {}, |
582 | 559 | "outputs": [ |
583 | 560 | { |
584 | 561 | "name": "stdout", |
585 | 562 | "output_type": "stream", |
586 | 563 | "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" |
588 | 565 | ] |
589 | 566 | } |
590 | 567 | ], |
591 | 568 | "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\")" |
595 | 570 | ] |
596 | 571 | }, |
597 | 572 | { |
598 | 573 | "cell_type": "code", |
599 | | - "execution_count": 20, |
| 574 | + "execution_count": 43, |
600 | 575 | "id": "f601b065", |
601 | 576 | "metadata": {}, |
602 | 577 | "outputs": [], |
|
606 | 581 | }, |
607 | 582 | { |
608 | 583 | "cell_type": "code", |
609 | | - "execution_count": 21, |
| 584 | + "execution_count": 44, |
610 | 585 | "id": "63e4a847", |
611 | 586 | "metadata": {}, |
612 | 587 | "outputs": [ |
613 | 588 | { |
614 | 589 | "name": "stdout", |
615 | 590 | "output_type": "stream", |
616 | 591 | "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" |
618 | 593 | ] |
619 | 594 | } |
620 | 595 | ], |
621 | 596 | "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\")" |
625 | 598 | ] |
626 | 599 | }, |
627 | 600 | { |
|
0 commit comments