Skip to content
Snippets Groups Projects
Commit d36be55f authored by the-anup-das's avatar the-anup-das
Browse files

feature: adding pinecone namespace

parent 103a5e88
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/09-route-filter.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/09-route-filter.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 Filter # Semantic Router Filter
%% 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.32 !pip install -qU semantic-router==0.0.32
``` ```
%% 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!",
], ],
) )
``` ```
%% Output %% 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 /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 from .autonotebook import tqdm as notebook_tqdm
%% 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)
``` ```
%% Output %% Output
2024-03-28 14:24:37 INFO semantic_router.utils.logger local 2024-03-28 14:24:37 INFO semantic_router.utils.logger local
%% 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?")
``` ```
%% Output %% Output
RouteChoice(name='politics', function_call=None, similarity_score=None) RouteChoice(name='politics', function_call=None, similarity_score=None)
%% 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")
``` ```
%% Output %% Output
RouteChoice(name=None, function_call=None, similarity_score=None) RouteChoice(name=None, function_call=None, similarity_score=None)
%% 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.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Demonstrating the Filter Feature # 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. 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: For example, let's say we only want to consider the "chitchat" route for a particular query:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl("don't you love politics?", route_filter=["chitchat"]) rl("don't you love politics?", route_filter=["chitchat"])
``` ```
%% Output %% Output
RouteChoice(name='chitchat', function_call=None, similarity_score=None) RouteChoice(name='chitchat', function_call=None, similarity_score=None)
%% Cell type:markdown id: tags: %% 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. 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: Similarly, we can restrict it to the "politics" route:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl("how's the weather today?", route_filter=["politics"]) rl("how's the weather today?", route_filter=["politics"])
``` ```
%% Output %% Output
RouteChoice(name=None, function_call=None, similarity_score=None) RouteChoice(name=None, function_call=None, similarity_score=None)
%% Cell type:markdown id: tags: %% 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. In this case, it will return None because the query doesn't match the "politics" route well enough to pass the threshold.
......
This diff is collapsed.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
!pip install -qU "semantic-router[qdrant]" !pip install -qU "semantic-router[qdrant]"
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from semantic_router import Route from semantic_router import Route
# we could use this as a guide for our chatbot to avoid political conversations # we could use this as a guide for our chatbot to avoid political conversations
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 hate the president", "don't you just love 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!",
], ],
) )
# this could be used as an indicator to our chatbot to switch to a more # this could be used as an indicator to our chatbot to switch to a more
# conversational prompt # conversational prompt
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",
], ],
) )
# we place both of our decisions together into single list # we place both of our decisions together into single list
routes = [politics, chitchat] routes = [politics, chitchat]
``` ```
%% 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 from semantic_router.encoders import CohereEncoder
os.environ["COHERE_API_KEY"] = os.environ.get("COHERE_API_KEY") or getpass( os.environ["COHERE_API_KEY"] = os.environ.get("COHERE_API_KEY") or getpass(
"Enter COHERE API key: " "Enter COHERE API key: "
) )
encoder = CohereEncoder() encoder = CohereEncoder()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from semantic_router.index.qdrant import QdrantIndex from semantic_router.index.qdrant import QdrantIndex
qd_index = QdrantIndex(location=":memory:") qd_index = QdrantIndex(location=":memory:")
``` ```
%% 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, index=qd_index) rl = RouteLayer(encoder=encoder, routes=routes, index=qd_index)
``` ```
%% Output %% Output
2024-03-27 18:22:42 INFO semantic_router.utils.logger local 2024-03-27 18:22:42 INFO semantic_router.utils.logger local
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can check our route layer and index information. We can check our route layer and index information.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.list_route_names() rl.list_route_names()
``` ```
%% Output %% Output
['politics', 'chitchat'] ['politics', 'chitchat']
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
len(rl.index) len(rl.index)
``` ```
%% Output %% Output
10 10
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
And query: And query:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl("don't you love politics?").name rl("don't you love politics?").name
``` ```
%% Output %% Output
'politics' 'politics'
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl("how's the weather today?").name rl("how's the weather today?").name
``` ```
%% Output %% Output
'chitchat' 'chitchat'
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl("I'm interested in learning about llama 2").name rl("I'm interested in learning about llama 2").name
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can delete or update routes. We can delete or update routes.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
len(rl.index) len(rl.index)
``` ```
%% Output %% Output
10 10
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import time import time
rl.delete(route_name="chitchat") rl.delete(route_name="chitchat")
time.sleep(1) time.sleep(1)
len(rl.index) len(rl.index)
``` ```
%% Output %% Output
5 5
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl("how's the weather today?").name rl("how's the weather today?").name
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.index.get_routes() rl.index.get_routes()
``` ```
%% Output %% Output
[('politics', 'they will save the country!'), [('politics', 'they will save the country!'),
('politics', "isn't politics the best thing ever"), ('politics', "isn't politics the best thing ever"),
('politics', "why don't you tell me about your political opinions"), ('politics', "why don't you tell me about your political opinions"),
('politics', "they're going to destroy this country!"), ('politics', "they're going to destroy this country!"),
('politics', ('politics',
"don't you just love the presidentdon't you just hate the president")] "don't you just love the presidentdon't you just hate the president")]
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.index.describe() rl.index.describe()
``` ```
%% Output %% Output
{'type': 'qdrant', 'dimensions': 1024, 'vectors': 5} {'type': 'qdrant', 'dimensions': 1024, 'vectors': 5}
......
...@@ -35,7 +35,7 @@ class TfidfEncoder(BaseEncoder): ...@@ -35,7 +35,7 @@ class TfidfEncoder(BaseEncoder):
docs = [] docs = []
for route in routes: for route in routes:
for doc in route.utterances: for doc in route.utterances:
docs.append(self._preprocess(doc)) docs.append(self._preprocess(doc)) # type: ignore
self.word_index = self._build_word_index(docs) self.word_index = self._build_word_index(docs)
self.idf = self._compute_idf(docs) self.idf = self._compute_idf(docs)
......
...@@ -47,12 +47,14 @@ class PineconeIndex(BaseIndex): ...@@ -47,12 +47,14 @@ class PineconeIndex(BaseIndex):
client: Any = Field(default=None, exclude=True) client: Any = Field(default=None, exclude=True)
index: Optional[Any] = Field(default=None, exclude=True) index: Optional[Any] = Field(default=None, exclude=True)
ServerlessSpec: Any = Field(default=None, exclude=True) ServerlessSpec: Any = Field(default=None, exclude=True)
namespace: Optional[str] = ""
def __init__(self, **data): def __init__(self, **data):
super().__init__(**data) super().__init__(**data)
self._initialize_client() self._initialize_client()
self.type = "pinecone" self.type = "pinecone"
self.client = self._initialize_client() self.client = self._initialize_client()
self.index = self._init_index(force_create=True)
def _initialize_client(self, api_key: Optional[str] = None): def _initialize_client(self, api_key: Optional[str] = None):
try: try:
...@@ -68,7 +70,11 @@ class PineconeIndex(BaseIndex): ...@@ -68,7 +70,11 @@ class PineconeIndex(BaseIndex):
api_key = api_key or os.getenv("PINECONE_API_KEY") api_key = api_key or os.getenv("PINECONE_API_KEY")
if api_key is None: if api_key is None:
raise ValueError("Pinecone API key is required.") raise ValueError("Pinecone API key is required.")
return Pinecone(api_key=api_key, source_tag="semantic-router") pinecone_args = {"api_key": api_key, "source_tag": "semantic-router"}
if self.namespace:
pinecone_args["namespace"] = self.namespace
return Pinecone(**pinecone_args)
def _init_index(self, force_create: bool = False) -> Union[Any, None]: def _init_index(self, force_create: bool = False) -> Union[Any, None]:
index_exists = self.index_name in self.client.list_indexes().names() index_exists = self.index_name in self.client.list_indexes().names()
...@@ -89,7 +95,7 @@ class PineconeIndex(BaseIndex): ...@@ -89,7 +95,7 @@ class PineconeIndex(BaseIndex):
time.sleep(0.5) time.sleep(0.5)
elif index_exists: elif index_exists:
# if the index exists we just return it # if the index exists we just return it
index = self.client.Index(self.index_name) index = self.client.Index(self.index_name, namespace=self.namespace)
# grab the dimensions from the index # grab the dimensions from the index
self.dimensions = index.describe_index_stats()["dimension"] self.dimensions = index.describe_index_stats()["dimension"]
elif force_create and not dimensions_given: elif force_create and not dimensions_given:
...@@ -108,7 +114,7 @@ class PineconeIndex(BaseIndex): ...@@ -108,7 +114,7 @@ class PineconeIndex(BaseIndex):
def _batch_upsert(self, batch: List[dict]): def _batch_upsert(self, batch: List[dict]):
"""Helper method for upserting a single batch of records.""" """Helper method for upserting a single batch of records."""
if self.index is not None: if self.index is not None:
self.index.upsert(vectors=batch) self.index.upsert(vectors=batch, namespace=self.namespace)
else: else:
raise ValueError("Index is None, could not upsert.") raise ValueError("Index is None, could not upsert.")
...@@ -175,7 +181,7 @@ class PineconeIndex(BaseIndex): ...@@ -175,7 +181,7 @@ class PineconeIndex(BaseIndex):
# if we need metadata, we fetch it # if we need metadata, we fetch it
if include_metadata: if include_metadata:
res_meta = self.index.fetch(ids=vector_ids) res_meta = self.index.fetch(ids=vector_ids, namespace=self.namespace)
# extract metadata only # extract metadata only
metadata.extend([x["metadata"] for x in res_meta["vectors"].values()]) metadata.extend([x["metadata"] for x in res_meta["vectors"].values()])
...@@ -206,7 +212,7 @@ class PineconeIndex(BaseIndex): ...@@ -206,7 +212,7 @@ class PineconeIndex(BaseIndex):
raise ValueError("Index is None, could not delete.") raise ValueError("Index is None, could not delete.")
def delete_all(self): def delete_all(self):
self.index.delete(delete_all=True) self.index.delete(delete_all=True, namespace=self.namespace)
def describe(self) -> dict: def describe(self) -> dict:
if self.index is not None: if self.index is not None:
...@@ -237,6 +243,7 @@ class PineconeIndex(BaseIndex): ...@@ -237,6 +243,7 @@ class PineconeIndex(BaseIndex):
top_k=top_k, top_k=top_k,
filter=filter_query, filter=filter_query,
include_metadata=True, include_metadata=True,
namespace=self.namespace,
) )
scores = [result["score"] for result in results["matches"]] scores = [result["score"] for result in results["matches"]]
route_names = [result["metadata"]["sr_route"] for result in results["matches"]] route_names = [result["metadata"]["sr_route"] for result in results["matches"]]
......
...@@ -328,7 +328,7 @@ class RouteLayer: ...@@ -328,7 +328,7 @@ class RouteLayer:
def add(self, route: Route): def add(self, route: Route):
logger.info(f"Adding `{route.name}` route") logger.info(f"Adding `{route.name}` route")
# create embeddings # create embeddings
embeds = self.encoder(route.utterances) embeds = self.encoder(route.utterances) # type:ignore
# if route has no score_threshold, use default # if route has no score_threshold, use default
if route.score_threshold is None: if route.score_threshold is None:
route.score_threshold = self.score_threshold route.score_threshold = self.score_threshold
...@@ -337,7 +337,7 @@ class RouteLayer: ...@@ -337,7 +337,7 @@ class RouteLayer:
self.index.add( self.index.add(
embeddings=embeds, embeddings=embeds,
routes=[route.name] * len(route.utterances), routes=[route.name] * len(route.utterances),
utterances=route.utterances, utterances=route.utterances, # type:ignore
) )
self.routes.append(route) self.routes.append(route)
...@@ -383,14 +383,14 @@ class RouteLayer: ...@@ -383,14 +383,14 @@ class RouteLayer:
all_utterances = [ all_utterances = [
utterance for route in routes for utterance in route.utterances utterance for route in routes for utterance in route.utterances
] ]
embedded_utterances = self.encoder(all_utterances) embedded_utterances = self.encoder(all_utterances) # type:ignore
# create route array # create route array
route_names = [route.name for route in routes for _ in route.utterances] route_names = [route.name for route in routes for _ in route.utterances]
# add everything to the index # add everything to the index
self.index.add( self.index.add(
embeddings=embedded_utterances, embeddings=embedded_utterances,
routes=route_names, routes=route_names,
utterances=all_utterances, utterances=all_utterances, # type:ignore
) )
def _encode(self, text: str) -> Any: def _encode(self, text: str) -> Any:
......
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