Skip to content
Snippets Groups Projects
Unverified Commit 884e15e1 authored by Siraj R Aizlewood's avatar Siraj R Aizlewood
Browse files

Linting.

parent 9b98205c
No related branches found
No related tags found
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/00-introduction.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/00-introduction.ipynb)
%% Cell type:markdown id: tags:
# Semantic Router Intro
%% Cell type:markdown id: tags:
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.21
```
%% Output
[notice] A new release of pip is available: 23.1.2 -> 24.0
[notice] To update, run: python.exe -m pip install --upgrade pip
%% Cell type:markdown id: tags:
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 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!",
],
)
```
%% Output
c:\Users\Siraj\Documents\Personal\Work\Aurelio\Virtual Environments\semantic_layer\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
from .autonotebook import tqdm as notebook_tqdm
%% Cell type:markdown id: tags:
Let's define another for good measure:
%% Cell type:code id: tags:
``` python
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]
```
%% Cell type:markdown id: tags:
Now we initialize our embedding model:
%% Cell type:code id: tags:
``` python
import os
from getpass import getpass
from semantic_router.encoders import CohereEncoder, OpenAIEncoder
# os.environ["COHERE_API_KEY"] = os.getenv("COHERE_API_KEY") or getpass(
# "Enter Cohere API Key: "
# )
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") or getpass(
"Enter OpenAI API Key: "
)
# encoder = CohereEncoder()
encoder = OpenAIEncoder()
```
%% Cell type:markdown id: tags:
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.layer import RouteLayer
rl = RouteLayer(encoder=encoder, routes=routes)
```
%% Output
2024-04-02 22:18:59 INFO semantic_router.utils.logger Initializing RouteLayer
2024-04-02 22:45:21 INFO semantic_router.utils.logger Initializing RouteLayer
%% Cell type:markdown id: tags:
Now we can test it:
%% Cell type:code id: tags:
``` python
rl("don't you love politics?")
```
%% Output
RouteChoice(name='politics', function_call=None, similarity_score=None, trigger=None)
%% Cell type:code id: tags:
``` python
rl("how's the weather today?")
```
%% Output
RouteChoice(name='chitchat', function_call=None, similarity_score=None, trigger=None)
%% Cell type:markdown id: tags:
Both are classified accurately, what if we send a query that is unrelated to our existing `Route` objects?
%% Cell type:code id: tags:
``` python
rl("I'm interested in learning about llama 2")
```
%% Output
RouteChoice(name=None, function_call=None, similarity_score=None, trigger=None)
%% Cell type:markdown id: tags:
We can also retrieve multiple routes with its associated score:
%% Cell type:code id: tags:
``` python
rl.retrieve_multiple_routes("Hi! How are you doing in politics??")
```
%% Output
[RouteChoice(name='politics', function_call=None, similarity_score=0.8596186767854479, trigger=None),
RouteChoice(name='chitchat', function_call=None, similarity_score=0.8356239688161818, trigger=None)]
......
......@@ -264,7 +264,7 @@ class RouteLayer:
route = self.check_for_matching_routes(category)
if route:
route_choice = RouteChoice(
name=route.name, similarity_score=score, route=route
name=route.name, similarity_score=score
)
route_choices.append(route_choice)
......@@ -395,7 +395,7 @@ class RouteLayer:
else:
logger.warning("No classification found for semantic classifier.")
return "", []
def get(self, name: str) -> Optional[Route]:
for route in self.routes:
if route.name == name:
......@@ -415,14 +415,20 @@ class RouteLayer:
route_obj = self.get(route_name)
if route_obj is not None:
# Use the Route object's threshold if it exists, otherwise use the provided threshold
_threshold = route_obj.score_threshold if route_obj.score_threshold is not None else self.score_threshold
_threshold = (
route_obj.score_threshold
if route_obj.score_threshold is not None
else self.score_threshold
)
if self._pass_threshold(scores, _threshold):
max_score = max(scores)
classes_above_threshold.append((route_name, max_score))
return classes_above_threshold
def group_scores_by_class(self, query_results: List[dict]) -> Dict[str, List[float]]:
def group_scores_by_class(
self, query_results: List[dict]
) -> Dict[str, List[float]]:
scores_by_class: Dict[str, List[float]] = {}
for result in query_results:
score = result["score"]
......@@ -433,7 +439,6 @@ class RouteLayer:
scores_by_class[route] = [score]
return scores_by_class
def _pass_threshold(self, scores: List[float], threshold: float) -> bool:
if scores:
return max(scores) > threshold
......
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