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

update to use route rather than decision

parent 4a81de14
No related branches found
No related tags found
No related merge requests found
[tool.poetry]
name = "semantic-router"
version = "0.0.5"
version = "0.0.7"
description = "Super fast semantic router for AI decision making"
authors = [
"James Briggs <james@aurelio.ai>",
......
from .layer import DecisionLayer, HybridDecisionLayer
from .layer import RouteLayer, HybridRouteLayer
__all__ = ["DecisionLayer", "HybridDecisionLayer"]
__all__ = ["RouteLayer", "HybridRouteLayer"]
......@@ -9,15 +9,15 @@ from semantic_router.encoders import (
BM25Encoder,
)
from semantic_router.linear import similarity_matrix, top_scores
from semantic_router.schema import Decision
from semantic_router.schema import Route
class DecisionLayer:
class RouteLayer:
index = None
categories = None
score_threshold = 0.82
def __init__(self, encoder: BaseEncoder, decisions: list[Decision] = []):
def __init__(self, encoder: BaseEncoder, routes: list[Route] = []):
self.encoder = encoder
# decide on default threshold based on encoder
if isinstance(encoder, OpenAIEncoder):
......@@ -26,11 +26,11 @@ class DecisionLayer:
self.score_threshold = 0.3
else:
self.score_threshold = 0.82
# if decisions list has been passed, we initialize index now
if decisions:
# if routes list has been passed, we initialize index now
if routes:
# initialize index now
for decision in tqdm(decisions):
self._add_decision(decision=decision)
for route in tqdm(routes):
self._add_route(route=route)
def __call__(self, text: str) -> str | None:
results = self._query(text)
......@@ -41,18 +41,18 @@ class DecisionLayer:
else:
return None
def add(self, decision: Decision):
self._add_decision(decision=decision)
def add(self, route: Route):
self._add_route(route=route)
def _add_decision(self, decision: Decision):
def _add_route(self, route: Route):
# create embeddings
embeds = self.encoder(decision.utterances)
embeds = self.encoder(route.utterances)
# create decision array
# create route array
if self.categories is None:
self.categories = np.array([decision.name] * len(embeds))
self.categories = np.array([route.name] * len(embeds))
else:
str_arr = np.array([decision.name] * len(embeds))
str_arr = np.array([route.name] * len(embeds))
self.categories = np.concatenate([self.categories, str_arr])
# create utterance array (the index)
if self.index is None:
......@@ -73,10 +73,10 @@ class DecisionLayer:
# calculate similarity matrix
sim = similarity_matrix(xq, self.index)
scores, idx = top_scores(sim, top_k)
# get the utterance categories (decision names)
decisions = self.categories[idx] if self.categories is not None else []
# get the utterance categories (route names)
routes = self.categories[idx] if self.categories is not None else []
return [
{"decision": d, "score": s.item()} for d, s in zip(decisions, scores)
{"route": d, "score": s.item()} for d, s in zip(routes, scores)
]
else:
return []
......@@ -85,15 +85,15 @@ class DecisionLayer:
scores_by_class = {}
for result in query_results:
score = result["score"]
decision = result["decision"]
if decision in scores_by_class:
scores_by_class[decision].append(score)
route = result["route"]
if route in scores_by_class:
scores_by_class[route].append(score)
else:
scores_by_class[decision] = [score]
scores_by_class[route] = [score]
# Calculate total score for each class
total_scores = {
decision: sum(scores) for decision, scores in scores_by_class.items()
route: sum(scores) for route, scores in scores_by_class.items()
}
top_class = max(total_scores, key=lambda x: total_scores[x], default=None)
......@@ -107,14 +107,14 @@ class DecisionLayer:
return False
class HybridDecisionLayer:
class HybridRouteLayer:
index = None
sparse_index = None
categories = None
score_threshold = 0.82
def __init__(
self, encoder: BaseEncoder, decisions: list[Decision] = [], alpha: float = 0.3
self, encoder: BaseEncoder, routes: list[Route] = [], alpha: float = 0.3
):
self.encoder = encoder
self.sparse_encoder = BM25Encoder()
......@@ -126,11 +126,11 @@ class HybridDecisionLayer:
self.score_threshold = 0.3
else:
self.score_threshold = 0.82
# if decisions list has been passed, we initialize index now
if decisions:
# if routes list has been passed, we initialize index now
if routes:
# initialize index now
for decision in tqdm(decisions):
self._add_decision(decision=decision)
for route in tqdm(routes):
self._add_route(route=route)
def __call__(self, text: str) -> str | None:
results = self._query(text)
......@@ -141,25 +141,25 @@ class HybridDecisionLayer:
else:
return None
def add(self, decision: Decision):
self._add_decision(decision=decision)
def add(self, route: Route):
self._add_route(route=route)
def _add_decision(self, decision: Decision):
def _add_route(self, route: Route):
# create embeddings
dense_embeds = np.array(self.encoder(decision.utterances)) # * self.alpha
dense_embeds = np.array(self.encoder(route.utterances)) # * self.alpha
sparse_embeds = np.array(
self.sparse_encoder(decision.utterances)
self.sparse_encoder(route.utterances)
) # * (1 - self.alpha)
# create decision array
# create route array
if self.categories is None:
self.categories = np.array([decision.name] * len(decision.utterances))
self.utterances = np.array(decision.utterances)
self.categories = np.array([route.name] * len(route.utterances))
self.utterances = np.array(route.utterances)
else:
str_arr = np.array([decision.name] * len(decision.utterances))
str_arr = np.array([route.name] * len(route.utterances))
self.categories = np.concatenate([self.categories, str_arr])
self.utterances = np.concatenate(
[self.utterances, np.array(decision.utterances)]
[self.utterances, np.array(route.utterances)]
)
# create utterance array (the dense index)
if self.index is None:
......@@ -199,10 +199,10 @@ class HybridDecisionLayer:
top_k = min(top_k, total_sim.shape[0])
idx = np.argpartition(total_sim, -top_k)[-top_k:]
scores = total_sim[idx]
# get the utterance categories (decision names)
decisions = self.categories[idx] if self.categories is not None else []
# get the utterance categories (route names)
routes = self.categories[idx] if self.categories is not None else []
return [
{"decision": d, "score": s.item()} for d, s in zip(decisions, scores)
{"route": d, "score": s.item()} for d, s in zip(routes, scores)
]
else:
return []
......@@ -217,15 +217,15 @@ class HybridDecisionLayer:
scores_by_class = {}
for result in query_results:
score = result["score"]
decision = result["decision"]
if decision in scores_by_class:
scores_by_class[decision].append(score)
route = result["route"]
if route in scores_by_class:
scores_by_class[route].append(score)
else:
scores_by_class[decision] = [score]
scores_by_class[route] = [score]
# Calculate total score for each class
total_scores = {
decision: sum(scores) for decision, scores in scores_by_class.items()
route: sum(scores) for route, scores in scores_by_class.items()
}
top_class = max(total_scores, key=lambda x: total_scores[x], default=None)
......
......@@ -10,7 +10,7 @@ from semantic_router.encoders import (
)
class Decision(BaseModel):
class Route(BaseModel):
name: str
utterances: list[str]
description: str | None = None
......@@ -45,12 +45,12 @@ class Encoder:
@dataclass
class SemanticSpace:
id: str
decisions: list[Decision]
routes: list[Route]
encoder: str = ""
def __init__(self, decisions: list[Decision] = []):
def __init__(self, routes: list[Route] = []):
self.id = ""
self.decisions = decisions
self.routes = routes
def add(self, decision: Decision):
self.decisions.append(decision)
def add(self, route: Route):
self.routes.append(route)
%% Cell type:markdown id: tags:
# Semantic Router Walkthrough
%% 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 route 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 routes. Cutting route making time down from seconds to milliseconds.
%% 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.1
```
%% 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 routes to example phrases that should trigger those routes.
%% Cell type:code id: tags:
``` python
from semantic_router.schema import Decision
from semantic_router.schema import Route
politics = Decision(
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!"
]
)
```
%% 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:
Let's define another for good measure:
%% Cell type:code id: tags:
``` python
chitchat = Decision(
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"
]
)
decisions = [politics, chitchat]
routes = [politics, chitchat]
```
%% Cell type:markdown id: tags:
Now we initialize our embedding model:
%% Cell type:code id: tags:
``` python
from semantic_router.encoders import CohereEncoder
from getpass import getpass
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: ")
encoder = CohereEncoder()
```
%% 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 `RouteLayer`. When called, the route layer will consume text (a query) and output the category (`Route`) it belongs to — to initialize a `RouteLayer` we need our `encoder` model and a list of `routes`.
%% Cell type:code id: tags:
``` python
from semantic_router import DecisionLayer
from semantic_router.layer import RouteLayer
dl = DecisionLayer(encoder=encoder, decisions=decisions)
dl = RouteLayer(encoder=encoder, routes=routes)
```
%% Output
100%|██████████| 2/2 [00:01<00:00, 1.04it/s]
%% Cell type:markdown id: tags:
Now we can test it:
%% Cell type:code id: tags:
``` python
dl("don't you love politics?")
```
%% Output
'politics'
%% Cell type:code id: tags:
``` python
dl("how's the weather today?")
```
%% Output
'chitchat'
%% 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 `Route` objects?
%% Cell type:code id: tags:
``` python
dl("I'm interested in learning about llama 2")
```
%% Cell type:markdown id: tags:
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