Skip to content

Commit 92e285c

Browse files
Enhance semantic router loading (#187)
This PR introduces some serialization methods for the `SemanticRouter` so that it can be loaded to and from file or dict.
1 parent 61c2f5a commit 92e285c

File tree

17 files changed

+503
-42
lines changed

17 files changed

+503
-42
lines changed

docs/user_guide/router.yaml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: topic-router
2+
routes:
3+
- name: technology
4+
references:
5+
- what are the latest advancements in AI?
6+
- tell me about the newest gadgets
7+
- what's trending in tech?
8+
metadata:
9+
category: tech
10+
priority: '1'
11+
- name: sports
12+
references:
13+
- who won the game last night?
14+
- tell me about the upcoming sports events
15+
- what's the latest in the world of sports?
16+
- sports
17+
- basketball and football
18+
metadata:
19+
category: sports
20+
priority: '2'
21+
- name: entertainment
22+
references:
23+
- what are the top movies right now?
24+
- who won the best actor award?
25+
- what's new in the entertainment industry?
26+
metadata:
27+
category: entertainment
28+
priority: '3'
29+
vectorizer:
30+
type: hf
31+
model: sentence-transformers/all-mpnet-base-v2
32+
routing_config:
33+
distance_threshold: 1.0
34+
max_k: 3
35+
aggregation_method: min

docs/user_guide/semantic_router_08.ipynb

+108-12
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
{
172172
"data": {
173173
"text/plain": [
174-
"RouteMatch(name='technology', distance=0.119614183903)"
174+
"RouteMatch(name='technology', distance=0.119614243507)"
175175
]
176176
},
177177
"execution_count": 5,
@@ -244,9 +244,9 @@
244244
{
245245
"data": {
246246
"text/plain": [
247-
"[RouteMatch(name='sports', distance=0.758580672741),\n",
248-
" RouteMatch(name='entertainment', distance=0.812423805396),\n",
249-
" RouteMatch(name='technology', distance=0.884235262871)]"
247+
"[RouteMatch(name='sports', distance=0.758580708504),\n",
248+
" RouteMatch(name='entertainment', distance=0.812423825264),\n",
249+
" RouteMatch(name='technology', distance=0.88423516353)]"
250250
]
251251
},
252252
"execution_count": 8,
@@ -268,9 +268,9 @@
268268
{
269269
"data": {
270270
"text/plain": [
271-
"[RouteMatch(name='sports', distance=0.663254022598),\n",
272-
" RouteMatch(name='entertainment', distance=0.712985336781),\n",
273-
" RouteMatch(name='technology', distance=0.832674443722)]"
271+
"[RouteMatch(name='sports', distance=0.663253903389),\n",
272+
" RouteMatch(name='entertainment', distance=0.712985396385),\n",
273+
" RouteMatch(name='technology', distance=0.832674384117)]"
274274
]
275275
},
276276
"execution_count": 9,
@@ -321,9 +321,9 @@
321321
{
322322
"data": {
323323
"text/plain": [
324-
"[RouteMatch(name='sports', distance=0.663254022598),\n",
325-
" RouteMatch(name='entertainment', distance=0.712985336781),\n",
326-
" RouteMatch(name='technology', distance=0.832674443722)]"
324+
"[RouteMatch(name='sports', distance=0.663253903389),\n",
325+
" RouteMatch(name='entertainment', distance=0.712985396385),\n",
326+
" RouteMatch(name='technology', distance=0.832674384117)]"
327327
]
328328
},
329329
"execution_count": 11,
@@ -340,13 +340,109 @@
340340
"cell_type": "markdown",
341341
"metadata": {},
342342
"source": [
343-
"## Clean up the router"
343+
"## Router serialization"
344344
]
345345
},
346346
{
347347
"cell_type": "code",
348348
"execution_count": 12,
349349
"metadata": {},
350+
"outputs": [
351+
{
352+
"data": {
353+
"text/plain": [
354+
"{'name': 'topic-router',\n",
355+
" 'routes': [{'name': 'technology',\n",
356+
" 'references': ['what are the latest advancements in AI?',\n",
357+
" 'tell me about the newest gadgets',\n",
358+
" \"what's trending in tech?\"],\n",
359+
" 'metadata': {'category': 'tech', 'priority': '1'}},\n",
360+
" {'name': 'sports',\n",
361+
" 'references': ['who won the game last night?',\n",
362+
" 'tell me about the upcoming sports events',\n",
363+
" \"what's the latest in the world of sports?\",\n",
364+
" 'sports',\n",
365+
" 'basketball and football'],\n",
366+
" 'metadata': {'category': 'sports', 'priority': '2'}},\n",
367+
" {'name': 'entertainment',\n",
368+
" 'references': ['what are the top movies right now?',\n",
369+
" 'who won the best actor award?',\n",
370+
" \"what's new in the entertainment industry?\"],\n",
371+
" 'metadata': {'category': 'entertainment', 'priority': '3'}}],\n",
372+
" 'vectorizer': {'type': 'hf',\n",
373+
" 'model': 'sentence-transformers/all-mpnet-base-v2'},\n",
374+
" 'routing_config': {'distance_threshold': 1.0,\n",
375+
" 'max_k': 3,\n",
376+
" 'aggregation_method': 'min'}}"
377+
]
378+
},
379+
"execution_count": 12,
380+
"metadata": {},
381+
"output_type": "execute_result"
382+
}
383+
],
384+
"source": [
385+
"router.to_dict()"
386+
]
387+
},
388+
{
389+
"cell_type": "code",
390+
"execution_count": 13,
391+
"metadata": {},
392+
"outputs": [
393+
{
394+
"name": "stdout",
395+
"output_type": "stream",
396+
"text": [
397+
"15:16:28 redisvl.index.index INFO Index already exists, not overwriting.\n"
398+
]
399+
}
400+
],
401+
"source": [
402+
"router2 = SemanticRouter.from_dict(router.to_dict(), redis_url=\"redis://localhost:6379\")\n",
403+
"\n",
404+
"assert router2 == router"
405+
]
406+
},
407+
{
408+
"cell_type": "code",
409+
"execution_count": 14,
410+
"metadata": {},
411+
"outputs": [],
412+
"source": [
413+
"router.to_yaml(\"router.yaml\", overwrite=True)"
414+
]
415+
},
416+
{
417+
"cell_type": "code",
418+
"execution_count": 16,
419+
"metadata": {},
420+
"outputs": [
421+
{
422+
"name": "stdout",
423+
"output_type": "stream",
424+
"text": [
425+
"15:17:42 redisvl.index.index INFO Index already exists, not overwriting.\n"
426+
]
427+
}
428+
],
429+
"source": [
430+
"router3 = SemanticRouter.from_yaml(\"router.yaml\", redis_url=\"redis://localhost:6379\")\n",
431+
"\n",
432+
"assert router3 == router2 == router"
433+
]
434+
},
435+
{
436+
"cell_type": "markdown",
437+
"metadata": {},
438+
"source": [
439+
"## Clean up the router"
440+
]
441+
},
442+
{
443+
"cell_type": "code",
444+
"execution_count": 17,
445+
"metadata": {},
350446
"outputs": [],
351447
"source": [
352448
"# Use clear to flush all routes from the index\n",
@@ -355,7 +451,7 @@
355451
},
356452
{
357453
"cell_type": "code",
358-
"execution_count": 13,
454+
"execution_count": 18,
359455
"metadata": {},
360456
"outputs": [],
361457
"source": [

0 commit comments

Comments
 (0)