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

update versions and notebook fix

parent a40f9db5
No related branches found
No related tags found
No related merge requests found
[tool.poetry] [tool.poetry]
name = "semantic-router" name = "semantic-router"
version = "0.0.5" version = "0.0.6"
description = "Super fast semantic router for AI decision making" description = "Super fast semantic router for AI decision making"
authors = [ authors = [
"James Briggs <james@aurelio.ai>", "James Briggs <james@aurelio.ai>",
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Semantic Router Walkthrough # Semantic Router Walkthrough
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
The Semantic Router library can be used as a super fast decision making layer on top of LLMs. That means rather than waiting on a slow agent to decide what to do, we can use the magic of semantic vector space to make decisions. Cutting decision making time down from seconds to milliseconds. The Semantic Router library can be used as a super fast decision making layer on top of LLMs. That means rather than waiting on a slow agent to decide what to do, we can use the magic of semantic vector space to make decisions. Cutting decision making time down from seconds to milliseconds.
%% 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.1 !pip install -qU semantic-router==0.0.6
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We start by defining a dictionary mapping decisions to example phrases that should trigger those decisions. We start by defining a dictionary mapping decisions to example phrases that should trigger those decisions.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from semantic_router.schema import Decision from semantic_router.schema import Decision
politics = Decision( politics = Decision(
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 love the president"
"don't you just hate 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!"
] ]
) )
``` ```
%% 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: %% Cell type:markdown id: tags:
Let's define another for good measure: Let's define another for good measure:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
chitchat = Decision( chitchat = Decision(
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"
] ]
) )
decisions = [politics, chitchat] decisions = [politics, chitchat]
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Now we initialize our embedding model: Now we initialize our embedding model:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from semantic_router.encoders import CohereEncoder from semantic_router.encoders import CohereEncoder
from getpass import getpass from getpass import getpass
import os import os
os.environ["COHERE_API_KEY"] = os.environ["COHERE_API_KEY"] or \ os.environ["COHERE_API_KEY"] = os.getenv("COHERE_API_KEY") or \
getpass("Enter Cohere API Key: ") getpass("Enter Cohere API Key: ")
encoder = CohereEncoder() encoder = CohereEncoder()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Now we define the `DecisionLayer`. When called, the decision layer will consume text (a query) and output the category (`Decision`) it belongs to — to initialize a `DecisionLayer` we need our `encoder` model and a list of `decisions`. Now we define the `DecisionLayer`. When called, the decision layer will consume text (a query) and output the category (`Decision`) it belongs to — to initialize a `DecisionLayer` we need our `encoder` model and a list of `decisions`.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from semantic_router import DecisionLayer from semantic_router.layer import DecisionLayer
dl = DecisionLayer(encoder=encoder, decisions=decisions) dl = DecisionLayer(encoder=encoder, decisions=decisions)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Now we can test it: Now we can test it:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
dl("don't you love politics?") dl("don't you love politics?")
``` ```
%% Output
'politics'
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
dl("how's the weather today?") dl("how's the weather today?")
``` ```
%% Output
'chitchat'
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Both are classified accurately, what if we send a query that is unrelated to our existing `Decision` objects? Both are classified accurately, what if we send a query that is unrelated to our existing `Decision` objects?
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
dl("I'm interested in learning about llama 2") dl("I'm interested in learning about llama 2")
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
In this case, we return `None` because no matches were identified. In this case, we return `None` because no matches were identified.
......
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