Skip to content
Snippets Groups Projects
Unverified Commit 5877a42f authored by James Briggs's avatar James Briggs
Browse files

lint

parent b168b2d4
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
[![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) [![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 id: tags: %% Cell type:markdown id: tags:
# Route Layers from File # Route Layers from File
Here we will show how to save routers to YAML or JSON files, and how to load a route layer from file. 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 id: tags: %% Cell type:markdown id: tags:
## Getting Started ## Getting Started
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We start by installing the library: We start by installing the library:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
!pip install -qU semantic-router==0.0.14 !pip install -qU semantic-router==0.0.14
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
_**⚠️ If using Google Colab, install the prerequisites and then restart the notebook before continuing**_ _**⚠️ If using Google Colab, install the prerequisites and then restart the notebook before continuing**_
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Saving to JSON ## Saving to JSON
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
First let's create a list of routes: First let's create a list of routes:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from semantic_router import Route from semantic_router import Route
politics = Route( politics = Route(
name="politics", name="politics",
utterances=[ utterances=[
"isn't politics the best thing ever", "isn't politics the best thing ever",
"why don't you tell me about your political opinions", "why don't you tell me about your political opinions",
"don't you just love the president" "don't you just hate the president", "don't you just love the president" "don't you just hate the president",
"they're going to destroy this country!", "they're going to destroy this country!",
"they will save the country!", "they will save the country!",
], ],
) )
chitchat = Route( chitchat = Route(
name="chitchat", name="chitchat",
utterances=[ utterances=[
"how's the weather today?", "how's the weather today?",
"how are things going?", "how are things going?",
"lovely weather today", "lovely weather today",
"the weather is horrendous", "the weather is horrendous",
"let's go to the chippy", "let's go to the chippy",
], ],
) )
routes = [politics, chitchat] routes = [politics, chitchat]
``` ```
%% Output %% Output
/Users/jamesbriggs/opt/anaconda3/envs/decision-layer/lib/python3.11/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 /Users/jamesbriggs/opt/anaconda3/envs/decision-layer/lib/python3.11/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
from .autonotebook import tqdm as notebook_tqdm from .autonotebook import tqdm as notebook_tqdm
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used. None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We define a route layer using these routes and using the default Cohere encoder. We define a route layer using these routes and using the default Cohere encoder.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import os import os
from getpass import getpass from getpass import getpass
from semantic_router import RouteLayer from semantic_router import RouteLayer
# dashboard.cohere.ai # dashboard.cohere.ai
os.environ["COHERE_API_KEY"] = os.getenv("COHERE_API_KEY") or getpass( os.environ["COHERE_API_KEY"] = os.getenv("COHERE_API_KEY") or getpass(
"Enter Cohere API Key: " "Enter Cohere API Key: "
) )
layer = RouteLayer(routes=routes) layer = RouteLayer(routes=routes)
``` ```
%% Output %% Output
2023-12-28 19:16:54 INFO semantic_router.utils.logger Initializing RouteLayer 2023-12-28 19:16:54 INFO semantic_router.utils.logger Initializing RouteLayer
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
To save our route layer we call the `to_json` method: To save our route layer we call the `to_json` method:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
layer.to_json("layer.json") layer.to_json("layer.json")
``` ```
%% Output %% Output
2023-12-28 19:17:03 INFO semantic_router.utils.logger Saving route config to layer.json 2023-12-28 19:17:03 INFO semantic_router.utils.logger Saving route config to layer.json
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Loading from JSON ## Loading from JSON
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can view the router file we just saved to see what information is stored. We can view the router file we just saved to see what information is stored.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import json import json
with open("layer.json", "r") as f: with open("layer.json", "r") as f:
router_json = json.load(f) router_json = json.load(f)
print(router_json) print(router_json)
``` ```
%% Output %% Output
{'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_schema': None}, {'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_schema': None}]} {'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_schema': None}, {'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_schema': None}]}
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
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. 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 id: tags: %% Cell type:code id: tags:
``` python ``` python
layer = RouteLayer.from_json("layer.json") layer = RouteLayer.from_json("layer.json")
``` ```
%% Output %% Output
2023-12-28 19:17:08 INFO semantic_router.utils.logger Loading route config from layer.json 2023-12-28 19:17:08 INFO semantic_router.utils.logger Loading route config from layer.json
2023-12-28 19:17:08 INFO semantic_router.utils.logger Initializing RouteLayer 2023-12-28 19:17:08 INFO semantic_router.utils.logger Initializing RouteLayer
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can confirm that our layer has been initialized with the expected attributes by viewing the `RouteLayer` object: We can confirm that our layer has been initialized with the expected attributes by viewing the `RouteLayer` object:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
print(f"""{layer.encoder.type=} print(
f"""{layer.encoder.type=}
{layer.encoder.name=} {layer.encoder.name=}
{layer.routes=}""") {layer.routes=}"""
)
``` ```
%% Output %% Output
layer.encoder.type='cohere' layer.encoder.type='cohere'
layer.encoder.name='embed-english-v3.0' layer.encoder.name='embed-english-v3.0'
layer.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_schema=None), 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_schema=None)] layer.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_schema=None), 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_schema=None)]
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
--- ---
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment