Skip to content

Enhance semantic router loading #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/user_guide/router.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: topic-router
routes:
- name: technology
references:
- what are the latest advancements in AI?
- tell me about the newest gadgets
- what's trending in tech?
metadata:
category: tech
priority: '1'
- name: sports
references:
- who won the game last night?
- tell me about the upcoming sports events
- what's the latest in the world of sports?
- sports
- basketball and football
metadata:
category: sports
priority: '2'
- name: entertainment
references:
- what are the top movies right now?
- who won the best actor award?
- what's new in the entertainment industry?
metadata:
category: entertainment
priority: '3'
vectorizer:
type: hf
model: sentence-transformers/all-mpnet-base-v2
routing_config:
distance_threshold: 1.0
max_k: 3
aggregation_method: min
120 changes: 108 additions & 12 deletions docs/user_guide/semantic_router_08.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
{
"data": {
"text/plain": [
"RouteMatch(name='technology', distance=0.119614183903)"
"RouteMatch(name='technology', distance=0.119614243507)"
]
},
"execution_count": 5,
Expand Down Expand Up @@ -244,9 +244,9 @@
{
"data": {
"text/plain": [
"[RouteMatch(name='sports', distance=0.758580672741),\n",
" RouteMatch(name='entertainment', distance=0.812423805396),\n",
" RouteMatch(name='technology', distance=0.884235262871)]"
"[RouteMatch(name='sports', distance=0.758580708504),\n",
" RouteMatch(name='entertainment', distance=0.812423825264),\n",
" RouteMatch(name='technology', distance=0.88423516353)]"
]
},
"execution_count": 8,
Expand All @@ -268,9 +268,9 @@
{
"data": {
"text/plain": [
"[RouteMatch(name='sports', distance=0.663254022598),\n",
" RouteMatch(name='entertainment', distance=0.712985336781),\n",
" RouteMatch(name='technology', distance=0.832674443722)]"
"[RouteMatch(name='sports', distance=0.663253903389),\n",
" RouteMatch(name='entertainment', distance=0.712985396385),\n",
" RouteMatch(name='technology', distance=0.832674384117)]"
]
},
"execution_count": 9,
Expand Down Expand Up @@ -321,9 +321,9 @@
{
"data": {
"text/plain": [
"[RouteMatch(name='sports', distance=0.663254022598),\n",
" RouteMatch(name='entertainment', distance=0.712985336781),\n",
" RouteMatch(name='technology', distance=0.832674443722)]"
"[RouteMatch(name='sports', distance=0.663253903389),\n",
" RouteMatch(name='entertainment', distance=0.712985396385),\n",
" RouteMatch(name='technology', distance=0.832674384117)]"
]
},
"execution_count": 11,
Expand All @@ -340,13 +340,109 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clean up the router"
"## Router serialization"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'name': 'topic-router',\n",
" 'routes': [{'name': 'technology',\n",
" 'references': ['what are the latest advancements in AI?',\n",
" 'tell me about the newest gadgets',\n",
" \"what's trending in tech?\"],\n",
" 'metadata': {'category': 'tech', 'priority': '1'}},\n",
" {'name': 'sports',\n",
" 'references': ['who won the game last night?',\n",
" 'tell me about the upcoming sports events',\n",
" \"what's the latest in the world of sports?\",\n",
" 'sports',\n",
" 'basketball and football'],\n",
" 'metadata': {'category': 'sports', 'priority': '2'}},\n",
" {'name': 'entertainment',\n",
" 'references': ['what are the top movies right now?',\n",
" 'who won the best actor award?',\n",
" \"what's new in the entertainment industry?\"],\n",
" 'metadata': {'category': 'entertainment', 'priority': '3'}}],\n",
" 'vectorizer': {'type': 'hf',\n",
" 'model': 'sentence-transformers/all-mpnet-base-v2'},\n",
" 'routing_config': {'distance_threshold': 1.0,\n",
" 'max_k': 3,\n",
" 'aggregation_method': 'min'}}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"router.to_dict()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15:16:28 redisvl.index.index INFO Index already exists, not overwriting.\n"
]
}
],
"source": [
"router2 = SemanticRouter.from_dict(router.to_dict(), redis_url=\"redis://localhost:6379\")\n",
"\n",
"assert router2 == router"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"router.to_yaml(\"router.yaml\", overwrite=True)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15:17:42 redisvl.index.index INFO Index already exists, not overwriting.\n"
]
}
],
"source": [
"router3 = SemanticRouter.from_yaml(\"router.yaml\", redis_url=\"redis://localhost:6379\")\n",
"\n",
"assert router3 == router2 == router"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clean up the router"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"# Use clear to flush all routes from the index\n",
Expand All @@ -355,7 +451,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand Down
Loading
Loading