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

Linting.

parent 694cf2b7
No related branches found
Tags v0.1.39
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 Filter
%% 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.29
```
%% 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
/Users/zahidsyed/anaconda3/envs/semantic_router/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
%% 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-03-28 14:24:37 INFO semantic_router.utils.logger local
%% 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)
%% Cell type:code id: tags:
``` python
rl("how's the weather today?")
```
%% 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)
%% Cell type:markdown id: tags:
In this case, we return `None` because no matches were identified.
%% Cell type:markdown id: tags:
# Demonstrating the Filter Feature
Now, let's demonstrate the filter feature. We can specify a subset of routes to consider when making a classification. This can be useful if we want to restrict the scope of possible routes based on some context.
For example, let's say we only want to consider the "chitchat" route for a particular query:
%% Cell type:code id: tags:
``` python
rl("don't you love politics?", route_filter=["chitchat"])
```
%% Output
RouteChoice(name='chitchat', function_call=None, similarity_score=None)
%% Cell type:markdown id: tags:
Even though the query might be more related to the "politics" route, it will be classified as "chitchat" because we've restricted the routes to consider.
Similarly, we can restrict it to the "politics" route:
%% Cell type:code id: tags:
``` python
rl("how's the weather today?", route_filter=["politics"])
```
%% Output
RouteChoice(name=None, function_call=None, similarity_score=None)
%% Cell type:markdown id: tags:
In this case, it will return None because the query doesn't match the "politics" route well enough to pass the threshold.
......
%% Cell type:code id: tags:
``` python
!pip install -qU "semantic-router[qdrant]"
```
%% Cell type:code id: tags:
``` python
from semantic_router import Route
# we could use this as a guide for our chatbot to avoid political conversations
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!",
],
)
# this could be used as an indicator to our chatbot to switch to a more
# conversational prompt
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",
],
)
# we place both of our decisions together into single list
routes = [politics, chitchat]
```
%% Cell type:code id: tags:
``` python
import os
from getpass import getpass
from semantic_router.encoders import CohereEncoder
os.environ["COHERE_API_KEY"] = os.environ.get("COHERE_API_KEY") or getpass(
"Enter COHERE API key: "
)
encoder = CohereEncoder()
```
%% Cell type:code id: tags:
``` python
from semantic_router.index.qdrant import QdrantIndex
qd_index = QdrantIndex(location=":memory:")
```
%% Cell type:code id: tags:
``` python
from semantic_router.layer import RouteLayer
rl = RouteLayer(encoder=encoder, routes=routes, index=qd_index)
```
%% Output
2024-03-27 18:22:42 INFO semantic_router.utils.logger local
%% Cell type:markdown id: tags:
We can check our route layer and index information.
%% Cell type:code id: tags:
``` python
rl.list_route_names()
```
%% Output
['politics', 'chitchat']
%% Cell type:code id: tags:
``` python
len(rl.index)
```
%% Output
10
%% Cell type:markdown id: tags:
And query:
%% Cell type:code id: tags:
``` python
rl("don't you love politics?").name
```
%% Output
'politics'
%% Cell type:code id: tags:
``` python
rl("how's the weather today?").name
```
%% Output
'chitchat'
%% Cell type:code id: tags:
``` python
rl("I'm interested in learning about llama 2").name
```
%% Cell type:markdown id: tags:
We can delete or update routes.
%% Cell type:code id: tags:
``` python
len(rl.index)
```
%% Output
10
%% Cell type:code id: tags:
``` python
import time
rl.delete(route_name="chitchat")
time.sleep(1)
len(rl.index)
```
%% Output
5
%% Cell type:code id: tags:
``` python
rl("how's the weather today?").name
```
%% Cell type:code id: tags:
``` python
rl.index.get_routes()
```
%% Output
[('politics', 'they will save the country!'),
('politics', "isn't politics the best thing ever"),
('politics', "why don't you tell me about your political opinions"),
('politics', "they're going to destroy this country!"),
('politics',
"don't you just love the presidentdon't you just hate the president")]
%% Cell type:code id: tags:
``` python
rl.index.describe()
```
%% Output
{'type': 'qdrant', 'dimensions': 1024, 'vectors': 5}
......
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