{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zgD8f9t-g6T5"
      },
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/01-save-load-from-file.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/01-save-load-from-file.ipynb)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hTb9WLITg6T6"
      },
      "source": [
        "# Route Layers from File\n",
        "\n",
        "Here we will show how to save routers to YAML or JSON files, and how to load a route layer from file."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "22V5zm43g6T6"
      },
      "source": [
        "## Getting Started"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ccVpLiSRg6T7"
      },
      "source": [
        "We start by installing the library:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "mXyJWdrbg6T7"
      },
      "outputs": [],
      "source": [
        "!pip install -qU \"semantic-router==0.1.1\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "nN0wWa84g6T8"
      },
      "source": [
        "## Define Route"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yo8sWSkBg6T8"
      },
      "source": [
        "First let's create a list of routes:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "U_YPXSNAg6T9",
        "outputId": "e6916925-e55c-49e8-e574-5b0cc136d8c2"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "c:\\Users\\Joshu\\OneDrive\\Documents\\Aurelio\\agents-course\\07-pratical-ai\\.venv\\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",
            "  from .autonotebook import tqdm as notebook_tqdm\n"
          ]
        }
      ],
      "source": [
        "from semantic_router import Route\n",
        "\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",
        "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",
        "routes = [politics, chitchat]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0FwEXPJ4g6T-"
      },
      "source": [
        "We define a route layer using these routes and using the Cohere encoder."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "id": "BnwbMv1Cg6T_",
        "outputId": "383c2dc7-1271-41bd-facf-6db5bd76bc9a"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "2025-01-06 11:59:56 - semantic_router.utils.logger - WARNING - base.py:356 - _get_index() - No index provided. Using default LocalIndex.\n",
            "2025-01-06 11:59:56 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed \"HTTP/1.1 200 OK\"\n",
            "2025-01-06 11:59:57 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed \"HTTP/1.1 200 OK\"\n",
            "2025-01-06 11:59:57 - semantic_router.utils.logger - WARNING - local.py:148 - _write_config() - No config is written for LocalIndex.\n"
          ]
        }
      ],
      "source": [
        "import os\n",
        "from getpass import getpass\n",
        "from semantic_router.encoders import CohereEncoder\n",
        "from semantic_router.routers import SemanticRouter\n",
        "\n",
        "# dashboard.cohere.ai\n",
        "os.environ[\"COHERE_API_KEY\"] = os.getenv(\"COHERE_API_KEY\") or getpass(\n",
        "    \"Enter Cohere API Key: \"\n",
        ")\n",
        "\n",
        "encoder = CohereEncoder()\n",
        "\n",
        "rl = SemanticRouter(encoder=encoder, routes=routes, auto_sync=\"local\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5RC4a-s8g6UA"
      },
      "source": [
        "## Test Route"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "VlKURQQCg6UB",
        "outputId": "98bf19ec-4ba0-4cdc-d232-7629aa5afea2"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "2025-01-06 12:00:03 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed \"HTTP/1.1 200 OK\"\n",
            "2025-01-06 12:00:08 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed \"HTTP/1.1 200 OK\"\n",
            "2025-01-06 12:00:42 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed \"HTTP/1.1 200 OK\"\n",
            "2025-01-06 12:03:49 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed \"HTTP/1.1 200 OK\"\n",
            "2025-01-06 12:03:49 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed \"HTTP/1.1 200 OK\"\n",
            "2025-01-06 12:04:00 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed \"HTTP/1.1 200 OK\"\n",
            "2025-01-06 12:04:09 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed \"HTTP/1.1 200 OK\"\n",
            "2025-01-06 12:04:38 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed \"HTTP/1.1 200 OK\"\n",
            "2025-01-06 12:04:38 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed \"HTTP/1.1 200 OK\"\n"
          ]
        },
        {
          "data": {
            "text/plain": [
              "RouteChoice(name='politics', function_call=None, similarity_score=None)"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "rl(\"isn't politics the best thing ever\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "1FUnWyN8g6UB",
        "outputId": "697f9937-8739-4e13-93f7-cfbfbbb61a01"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "RouteChoice(name='chitchat', function_call=None, similarity_score=None)"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "rl(\"how's the weather today?\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CUlzIg9kg6UB"
      },
      "source": [
        "## Save To JSON"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QZ-N5UdFg6UB"
      },
      "source": [
        "To save our route layer we call the `to_json` method:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "id": "jnLwulrCg6UC",
        "outputId": "cfd85636-1030-4b00-bebe-9167319e8b62"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "2025-01-06 12:00:14 - semantic_router.utils.logger - INFO - base.py:211 - to_file() - Saving route config to layer.json\n"
          ]
        }
      ],
      "source": [
        "rl.to_json(\"layer.json\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YkMsmpLZg6UE"
      },
      "source": [
        "## Loading from JSON"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CCirv8Jjg6UE"
      },
      "source": [
        "We can view the router file we just saved to see what information is stored."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "id": "NRfi40IAg6UE",
        "outputId": "29c3f322-59ef-4103-abf9-2184aac5e465"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{'encoder_type': 'cohere', 'encoder_name': 'embed-english-v3.0', 'routes': [{'name': 'politics', 'utterances': [\"isn't politics the best thing ever\", \"why don't you tell me about your political opinions\", \"don't you just love the presidentdon't you just hate the president\", \"they're going to destroy this country!\", 'they will save the country!'], 'description': None, 'function_schemas': None, 'llm': None, 'score_threshold': 0.3, 'metadata': {}}, {'name': 'chitchat', 'utterances': [\"how's the weather today?\", 'how are things going?', 'lovely weather today', 'the weather is horrendous', \"let's go to the chippy\"], 'description': None, 'function_schemas': None, 'llm': None, 'score_threshold': 0.3, 'metadata': {}}]}\n"
          ]
        }
      ],
      "source": [
        "import json\n",
        "\n",
        "with open(\"layer.json\", \"r\") as f:\n",
        "    layer_json = json.load(f)\n",
        "\n",
        "print(layer_json)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1wKu-xPzg6UF"
      },
      "source": [
        "It tells us our encoder type, encoder name, and routes. This is everything we need to initialize a new router. To do so, we use the `from_json` method."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "id": "K40Ig7Gkg6UF",
        "outputId": "2ab1eef7-f8d1-4801-ad97-9f436922d0a4"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "2025-01-06 12:00:30 - semantic_router.utils.logger - INFO - base.py:96 - from_file() - Loading route config from layer.json\n",
            "2025-01-06 12:00:31 - semantic_router.utils.logger - WARNING - base.py:356 - _get_index() - No index provided. Using default LocalIndex.\n"
          ]
        }
      ],
      "source": [
        "rl = SemanticRouter.from_json(\"layer.json\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "R-2vjb39g6UF"
      },
      "source": [
        "We can confirm that our layer has been initialized with the expected attributes by viewing the `RouteLayer` object:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "id": "Ah9EVIITg6UF",
        "outputId": "4ab0af28-b62b-4d18-df9e-3243e0ba56b7"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "rl.encoder.type='cohere'\n",
            "rl.encoder.name='embed-english-v3.0'\n",
            "rl.routes=[Route(name='politics', utterances=[\"isn't politics the best thing ever\", \"why don't you tell me about your political opinions\", \"don't you just love the presidentdon't you just hate the president\", \"they're going to destroy this country!\", 'they will save the country!'], description=None, function_schemas=None, llm=None, score_threshold=0.3, metadata={}), Route(name='chitchat', utterances=[\"how's the weather today?\", 'how are things going?', 'lovely weather today', 'the weather is horrendous', \"let's go to the chippy\"], description=None, function_schemas=None, llm=None, score_threshold=0.3, metadata={})]\n"
          ]
        }
      ],
      "source": [
        "print(\n",
        "    f\"\"\"{rl.encoder.type=}\n",
        "{rl.encoder.name=}\n",
        "{rl.routes=}\"\"\"\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {},
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "2025-01-06 12:04:38 - semantic_router.utils.logger - WARNING - base.py:356 - _get_index() - No index provided. Using default LocalIndex.\n",
            "2025-01-06 12:04:38 - semantic_router.utils.logger - WARNING - local.py:148 - _write_config() - No config is written for LocalIndex.\n"
          ]
        }
      ],
      "source": [
        "new_rl = SemanticRouter(encoder=rl.encoder, routes=rl.routes, auto_sync=\"local\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uWMQI2Omg6UF"
      },
      "source": [
        "---"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "joc_mQRRg6UF"
      },
      "source": [
        "## Test Route Again"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "id": "RXkJldaxg6UF",
        "outputId": "222e6cdb-1c0c-4aff-df3a-062d72137744"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "RouteChoice(name='politics', function_call=None, similarity_score=None)"
            ]
          },
          "execution_count": 13,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "new_rl(\"isn't politics the best thing ever\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {
        "id": "u07b2W-Jg6UG",
        "outputId": "9a07d46a-0b73-4a3b-eb61-b5df49c92b67"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "RouteChoice(name='chitchat', function_call=None, similarity_score=None)"
            ]
          },
          "execution_count": 14,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "new_rl(\"how's the weather today?\")"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "decision-layer",
      "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": 0
}