Skip to content
Snippets Groups Projects
Commit 0cef1bde authored by Luca Mannini's avatar Luca Mannini
Browse files

Lint

parent 2d3ec8b5
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/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) [![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: %% Cell type:markdown id: tags:
# Semantic Router Intro # Semantic Router Intro
%% Cell type:markdown id: tags: %% 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. 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: %% 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.20 !pip install -qU semantic-router==0.0.20
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We start by defining a dictionary mapping routes to example phrases that should trigger those routes. We start by defining a dictionary mapping routes to example phrases that should trigger those 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 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!",
], ],
) )
``` ```
%% 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 = 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]
``` ```
%% 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
import os import os
from getpass import getpass from getpass import getpass
from semantic_router.encoders import CohereEncoder, OpenAIEncoder from semantic_router.encoders import CohereEncoder, OpenAIEncoder
# 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: "
# ) # )
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") or getpass( os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") or getpass(
"Enter OpenAI API Key: " "Enter OpenAI API Key: "
) )
# encoder = CohereEncoder() # encoder = CohereEncoder()
encoder = OpenAIEncoder() encoder = OpenAIEncoder()
``` ```
%% Cell type:markdown id: tags: %% 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`. 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: %% Cell type:code id: tags:
``` python ``` python
from semantic_router.layer import RouteLayer from semantic_router.layer import RouteLayer
rl = RouteLayer(encoder=encoder, routes=routes) rl = RouteLayer(encoder=encoder, routes=routes)
``` ```
%% 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
rl("don't you love politics?") rl("don't you love politics?")
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl("how's the weather today?") rl("how's the weather today?")
``` ```
%% 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 `Route` objects? Both are classified accurately, what if we send a query that is unrelated to our existing `Route` objects?
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl("I'm interested in learning about llama 2") rl("I'm interested in learning about llama 2")
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can also retrieve multiple routes with its associated score: We can also retrieve multiple routes with its associated score:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.retrieve_multiple_routes("Hi! How are you doing in politics??") rl.retrieve_multiple_routes("Hi! How are you doing in politics??")
``` ```
%% 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.
......
...@@ -257,13 +257,17 @@ class RouteLayer: ...@@ -257,13 +257,17 @@ class RouteLayer:
results = self._retrieve(xq=vector_arr) results = self._retrieve(xq=vector_arr)
# decide most relevant routes # decide most relevant routes
categories_with_scores = self._semantic_classify_multiple_routes(results, self.score_threshold) categories_with_scores = self._semantic_classify_multiple_routes(
results, self.score_threshold
)
route_choices = [] route_choices = []
for category, score in categories_with_scores: for category, score in categories_with_scores:
route = self.check_for_matching_routes(category) route = self.check_for_matching_routes(category)
if route: if route:
route_choice = RouteChoice(name=route.name, similarity_score=score, route=route) route_choice = RouteChoice(
name=route.name, similarity_score=score, route=route
)
route_choices.append(route_choice) route_choices.append(route_choice)
return route_choices return route_choices
...@@ -401,7 +405,9 @@ class RouteLayer: ...@@ -401,7 +405,9 @@ class RouteLayer:
logger.warning("No classification found for semantic classifier.") logger.warning("No classification found for semantic classifier.")
return "", [] return "", []
def _semantic_classify_multiple_routes(self, query_results: List[dict], threshold: float) -> List[Tuple[str, float]]: def _semantic_classify_multiple_routes(
self, query_results: List[dict], threshold: float
) -> List[Tuple[str, float]]:
scores_by_class: Dict[str, List[float]] = {} scores_by_class: Dict[str, List[float]] = {}
for result in query_results: for result in query_results:
score = result["score"] score = result["score"]
...@@ -411,7 +417,6 @@ class RouteLayer: ...@@ -411,7 +417,6 @@ class RouteLayer:
else: else:
scores_by_class[route] = [score] scores_by_class[route] = [score]
# Filter classes based on threshold and find max score for each # Filter classes based on threshold and find max score for each
classes_above_threshold = [] classes_above_threshold = []
for route, scores in scores_by_class.items(): for route, scores in scores_by_class.items():
...@@ -420,8 +425,7 @@ class RouteLayer: ...@@ -420,8 +425,7 @@ class RouteLayer:
classes_above_threshold.append((route, max_score)) classes_above_threshold.append((route, max_score))
return classes_above_threshold return classes_above_threshold
def _pass_threshold(self, scores: List[float], threshold: float) -> bool: def _pass_threshold(self, scores: List[float], threshold: float) -> bool:
if scores: if scores:
return max(scores) > threshold return max(scores) > threshold
...@@ -541,8 +545,8 @@ def threshold_random_search( ...@@ -541,8 +545,8 @@ def threshold_random_search(
if __name__ == "__main__": if __name__ == "__main__":
from semantic_router import Route from semantic_router import Route
from semantic_router.layer import RouteLayer
from semantic_router.encoders import OpenAIEncoder from semantic_router.encoders import OpenAIEncoder
from semantic_router.layer import RouteLayer
# Define routes with example phrases # Define routes with example phrases
politics = Route( politics = Route(
...@@ -577,7 +581,13 @@ if __name__ == "__main__": ...@@ -577,7 +581,13 @@ if __name__ == "__main__":
rl = RouteLayer(encoder=encoder, routes=routes) rl = RouteLayer(encoder=encoder, routes=routes)
# Test the RouteLayer with example queries # Test the RouteLayer with example queries
print(rl.retrieve_multiple_routes("how's the weather today?")) # Expected to match the chitchat route print(
print(rl.retrieve_multiple_routes("don't you love politics?")) # Expected to match the politics route rl.retrieve_multiple_routes("how's the weather today?")
print(rl.retrieve_multiple_routes("I'm interested in learning about llama 2")) # Expected to return None since it doesn't match any route ) # Expected to match the chitchat route
print(rl.retrieve_multiple_routes("Hi! How are you doing in politics??")) print(
\ No newline at end of file rl.retrieve_multiple_routes("don't you love politics?")
) # Expected to match the politics route
print(
rl.retrieve_multiple_routes("I'm interested in learning about llama 2")
) # Expected to return None since it doesn't match any route
print(rl.retrieve_multiple_routes("Hi! How are you doing in politics??"))
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