Skip to content
Snippets Groups Projects
Unverified Commit 1afe0ec9 authored by James Briggs's avatar James Briggs Committed by GitHub
Browse files

Merge pull request #50 from aurelio-labs/james/docs-lint

Fix for failing black lint in docs
parents 98052cb3 5877a42f
No related branches found
Tags v0.1.10
No related merge requests found
%% 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)
%% Cell type:markdown id: tags:
# 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.
%% Cell type:markdown id: tags:
## Getting Started
%% Cell type:markdown id: tags:
We start by installing the library:
%% Cell type:code id: tags:
``` python
!pip install -qU semantic-router==0.0.14
```
%% Cell type:markdown id: tags:
_**⚠️ If using Google Colab, install the prerequisites and then restart the notebook before continuing**_
%% Cell type:markdown id: tags:
## Saving to JSON
%% Cell type:markdown id: tags:
First let's create a list of routes:
%% Cell type:code id: tags:
``` python
from semantic_router import Route
politics = 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 president" "don't you just hate the president",
"they're going to destroy this country!",
"they will save the country!",
],
)
chitchat = 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",
],
)
routes = [politics, chitchat]
```
%% 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
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.
%% Cell type:markdown id: tags:
We define a route layer using these routes and using the default Cohere encoder.
%% Cell type:code id: tags:
``` python
import os
from getpass import getpass
from semantic_router import RouteLayer
# dashboard.cohere.ai
os.environ["COHERE_API_KEY"] = os.getenv("COHERE_API_KEY") or getpass(
"Enter Cohere API Key: "
)
layer = RouteLayer(routes=routes)
```
%% Output
2023-12-28 19:16:54 INFO semantic_router.utils.logger Initializing RouteLayer
%% Cell type:markdown id: tags:
To save our route layer we call the `to_json` method:
%% Cell type:code id: tags:
``` python
layer.to_json("layer.json")
```
%% Output
2023-12-28 19:17:03 INFO semantic_router.utils.logger Saving route config to layer.json
%% Cell type:markdown id: tags:
## Loading from JSON
%% Cell type:markdown id: tags:
We can view the router file we just saved to see what information is stored.
%% Cell type:code id: tags:
``` python
import json
with open("layer.json", "r") as f:
router_json = json.load(f)
print(router_json)
```
%% 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}]}
%% 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.
%% Cell type:code id: tags:
``` python
layer = RouteLayer.from_json("layer.json")
```
%% 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 Initializing RouteLayer
%% Cell type:markdown id: tags:
We can confirm that our layer has been initialized with the expected attributes by viewing the `RouteLayer` object:
%% Cell type:code id: tags:
``` python
print(f"""{layer.encoder.type=}
print(
f"""{layer.encoder.type=}
{layer.encoder.name=}
{layer.routes=}""")
{layer.routes=}"""
)
```
%% Output
layer.encoder.type='cohere'
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)]
%% 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.
Finish editing this message first!
Please register or to comment