Skip to content

Commit

Permalink
fix: update old hybrid example
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescalam committed Nov 23, 2024
1 parent e365910 commit e8f00a3
Showing 1 changed file with 29 additions and 41 deletions.
70 changes: 29 additions & 41 deletions docs/examples/hybrid-layer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The Hybrid Layer in the Semantic Router library can improve making performance particularly for niche use-cases that contain specific terminology, such as finance or medical. It helps us provide more importance to making based on the keywords contained in our utterances and user queries.\n"
"The `HybridRouter` in the Semantic Router library can improve making performance particularly for niche use-cases that contain specific terminology, such as finance or medical.\n",
"\n",
"It helps us provide more importance to making based on the keywords contained in our utterances and user queries."
]
},
{
Expand All @@ -34,7 +36,7 @@
"metadata": {},
"outputs": [],
"source": [
"#!pip install -qU semantic-router==0.0.11"
"#!pip install -qU semantic-router==0.1.0.dev2"
]
},
{
Expand All @@ -46,14 +48,14 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\Siraj\\Documents\\Personal\\Work\\Aurelio\\Virtual Environments\\semantic_router_3\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
"/Users/jamesbriggs/Library/Caches/pypoetry/virtualenvs/semantic-router-C1zr4a78-py3.12/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
Expand Down Expand Up @@ -83,28 +85,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"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",
"chitchat = Route(\n",
" name=\"chitchat\",\n",
" utterances=[\n",
Expand Down Expand Up @@ -133,14 +117,14 @@
"outputs": [],
"source": [
"import os\n",
"from semantic_router.encoders import CohereEncoder, BM25Encoder, TfidfEncoder\n",
"from semantic_router.encoders import OpenAIEncoder, TfidfEncoder\n",
"from getpass import getpass\n",
"\n",
"os.environ[\"COHERE_API_KEY\"] = os.environ[\"COHERE_API_KEY\"] or getpass(\n",
" \"Enter Cohere API Key: \"\n",
"os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\") or getpass(\n",
" \"Enter OpenAI API Key: \"\n",
")\n",
"\n",
"dense_encoder = CohereEncoder()\n",
"dense_encoder = OpenAIEncoder()\n",
"# sparse_encoder = BM25Encoder()\n",
"sparse_encoder = TfidfEncoder()"
]
Expand All @@ -154,63 +138,67 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m2024-05-07 21:15:31 INFO semantic_router.utils.logger Creating embeddings for all routes...\u001b[0m\n"
"\u001b[32m2024-11-24 00:37:14 INFO semantic_router.utils.logger Downloading and initializing default sBM25 model parameters.\u001b[0m\n",
"\u001b[32m2024-11-24 00:37:22 INFO semantic_router.utils.logger Encoding route politics\u001b[0m\n",
"\u001b[32m2024-11-24 00:37:23 INFO semantic_router.utils.logger Encoding route chitchat\u001b[0m\n"
]
}
],
"source": [
"from semantic_router.hybrid_layer import HybridRouteLayer\n",
"from semantic_router.routers import HybridRouter\n",
"\n",
"dl = HybridRouteLayer(\n",
" encoder=dense_encoder, sparse_encoder=sparse_encoder, routes=routes\n",
"router = HybridRouter(\n",
" encoder=dense_encoder,\n",
" sparse_encoder=sparse_encoder,\n",
" routes=routes\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'politics'"
"RouteChoice(name='politics', function_call=None, similarity_score=1.1909239963848142)"
]
},
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dl(\"don't you love politics?\")"
"router(\"don't you love politics?\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'chitchat'"
"RouteChoice(name='chitchat', function_call=None, similarity_score=2.0)"
]
},
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dl(\"how's the weather today?\")"
"router(\"how's the weather today?\")"
]
},
{
Expand All @@ -237,7 +225,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down

0 comments on commit e8f00a3

Please sign in to comment.