Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Local Index Example"
]
},
{
"cell_type": "code",
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"source": [
"from semantic_router import Route\n",
"\n",
"# we could use this as a guide for our chatbot to avoid political conversations\n",
"politics = Route(\n",
" name=\"politics\",\n",
" utterances=[\n",
" \"isn't politics the best thing ever\",\n",
" \"why don't you tell me about your political opinions\",\n",
" \"don't you just love the president\" \"don't you just hate the president\",\n",
" \"they're going to destroy this country!\",\n",
" \"they will save the country!\",\n",
" ],\n",
")\n",
"\n",
"# this could be used as an indicator to our chatbot to switch to a more\n",
"# conversational prompt\n",
"chitchat = Route(\n",
" name=\"chitchat\",\n",
" utterances=[\n",
" \"how's the weather today?\",\n",
" \"how are things going?\",\n",
" \"lovely weather today\",\n",
" \"the weather is horrendous\",\n",
" \"let's go to the chippy\",\n",
" ],\n",
")\n",
"\n",
"# we place both of our decisions together into single list\n",
"routes = [politics, chitchat]"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from semantic_router.encoders import HuggingFaceEncoder\n",
"\n",
"encoder = HuggingFaceEncoder(model_name=\"intfloat/e5-base-v2\")"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"from semantic_router.index.local import LocalIndex\n",
"\n",
"local_index = LocalIndex()"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2025-01-07 12:08:10 - semantic_router.utils.logger - WARNING - local.py:148 - _write_config() - No config is written for LocalIndex.\n"
"from semantic_router.routers import SemanticRouter\n",
"local_rl = SemanticRouter(\n",
" encoder=encoder, routes=routes, index=local_index, auto_sync=\"local\"\n",
")"
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['politics', 'chitchat']"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"local_rl.list_route_names()"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'politics'"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"local_rl(\"don't you love politics?\").name"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'chitchat'"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"local_rl(\"how's the weather today?\").name"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"local_rl(\"I'm interested in learning about llama 2\").name"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can delete or update routes."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(local_rl.index.index)"
]
},
{
"cell_type": "code",
{
"name": "stderr",
"output_type": "stream",
"text": [
"2025-01-07 12:08:29 - semantic_router.utils.logger - WARNING - base.py:172 - _read_config() - This method should be implemented by subclasses.\n",
"2025-01-07 12:08:29 - semantic_router.utils.logger - WARNING - base.py:172 - _read_config() - This method should be implemented by subclasses.\n",
"2025-01-07 12:08:29 - semantic_router.utils.logger - WARNING - base.py:823 - delete() - Route `chitchat` not found in SemanticRouter\n",
"2025-01-07 12:08:29 - semantic_router.utils.logger - WARNING - local.py:148 - _write_config() - No config is written for LocalIndex.\n"
]
},
{
"data": {
"text/plain": [
"5"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"local_rl.delete(\"chitchat\")\n",
"len(local_rl.index.index)"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"local_rl(\"how's the weather today?\").name"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'type': 'local', 'dimensions': 384, 'vectors': 5}"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"local_rl.index.describe()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "semantic_router_1",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
}
},
"nbformat": 4,
"nbformat_minor": 2
}