{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Local Index Example" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "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", "execution_count": 3, "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", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from semantic_router.index.local import LocalIndex\n", "\n", "local_index = LocalIndex()" ] }, { "cell_type": "code", "execution_count": 10, "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" ] } ], "source": [ "from semantic_router.routers import SemanticRouter\n", "\n", "local_rl = SemanticRouter(\n", " encoder=encoder, routes=routes, index=local_index, auto_sync=\"local\"\n", ")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['politics', 'chitchat']" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "local_rl.list_route_names()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'politics'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "local_rl(\"don't you love politics?\").name" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'chitchat'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "local_rl(\"how's the weather today?\").name" ] }, { "cell_type": "code", "execution_count": 14, "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", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(local_rl.index.index)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "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" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "local_rl.delete(\"chitchat\")\n", "len(local_rl.index.index)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "local_rl(\"how's the weather today?\").name" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'type': 'local', 'dimensions': 384, 'vectors': 5}" ] }, "execution_count": 19, "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", "version": "3.12.7" } }, "nbformat": 4, "nbformat_minor": 2 }