Skip to content

Commit

Permalink
fix: deprecated multiple routes query
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescalam committed Jan 3, 2025
1 parent ec4c216 commit d6a2058
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 161 deletions.
56 changes: 0 additions & 56 deletions docs/00-introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -279,62 +279,6 @@
"sr(\"I'm interested in learning about llama 2\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dDZF2eN4f3p4"
},
"source": [
"We can also retrieve multiple routes with its associated score:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "n27I7kmpf3p4",
"outputId": "2138e077-190b-41b7-a3eb-4fd76e2f59c2"
},
"outputs": [
{
"data": {
"text/plain": [
"[RouteChoice(name='politics', function_call=None, similarity_score=0.8595844842560181),\n",
" RouteChoice(name='chitchat', function_call=None, similarity_score=0.8356704527362284)]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sr.retrieve_multiple_routes(\"Hi! How are you doing in politics??\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "zi4XJ7Amf3p4",
"outputId": "cf05cd65-d4f4-454a-ef05-77f16f37cc8f"
},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sr.retrieve_multiple_routes(\"I'm interested in learning about llama 2\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
6 changes: 4 additions & 2 deletions semantic_router/index/pinecone.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class PineconeIndex(BaseIndex):
dimensions: Union[int, None] = None
metric: str = "dotproduct"
cloud: str = "aws"
region: str = "us-west-2"
region: str = "us-east-1"
host: str = ""
client: Any = Field(default=None, exclude=True)
async_client: Any = Field(default=None, exclude=True)
Expand All @@ -125,7 +125,7 @@ def __init__(
dimensions: Optional[int] = None,
metric: str = "dotproduct",
cloud: str = "aws",
region: str = "us-west-2",
region: str = "us-east-1",
host: str = "",
namespace: Optional[str] = "",
base_url: Optional[str] = "https://api.pinecone.io",
Expand All @@ -145,6 +145,8 @@ def __init__(
self.api_key = api_key or os.getenv("PINECONE_API_KEY")
self.base_url = base_url

logger.warning("Default region changed from us-west-2 to us-east-1 in v0.1.0.dev6")

if self.api_key is None:
raise ValueError("Pinecone API key is required.")

Expand Down
103 changes: 0 additions & 103 deletions tests/unit/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,109 +981,6 @@ def test_with_unrecognized_route(self, routes, index_cls, encoder_cls, router_cl
results = route_layer._semantic_classify_multiple_routes(query_results)
assert results == expected, "Should ignore and not return unrecognized routes"

def test_retrieve_with_text(self, routes, index_cls, encoder_cls, router_cls):
encoder = encoder_cls()
index = init_index(index_cls, index_name=encoder.__class__.__name__)
route_layer = router_cls(
encoder=encoder,
routes=routes,
index=index,
auto_sync="local",
)
text = "Hello"
results = route_layer.retrieve_multiple_routes(text=text)
assert len(results) >= 1, "Expected at least one result"
assert any(
result.name in ["Route 1", "Route 2"] for result in results
), "Expected the result to be either 'Route 1' or 'Route 2'"

def test_retrieve_with_vector(self, routes, index_cls, encoder_cls, router_cls):
encoder = encoder_cls()
index = init_index(index_cls, index_name=encoder.__class__.__name__)
route_layer = router_cls(
encoder=encoder,
routes=routes,
index=index,
auto_sync="local",
)
vector = [0.1, 0.2, 0.3]
if index_cls is PineconeIndex:
time.sleep(PINECONE_SLEEP) # allow for index to be populated
results = route_layer.retrieve_multiple_routes(vector=vector)
assert len(results) >= 1, "Expected at least one result"
assert any(
result.name in ["Route 1", "Route 2"] for result in results
), "Expected the result to be either 'Route 1' or 'Route 2'"

def test_retrieve_without_text_or_vector(
self, routes, index_cls, encoder_cls, router_cls
):
encoder = encoder_cls()
index = init_index(index_cls, index_name=encoder.__class__.__name__)
route_layer = router_cls(
encoder=encoder,
routes=routes,
index=index,
auto_sync="local",
)
with pytest.raises(ValueError, match="Either text or vector must be provided"):
route_layer.retrieve_multiple_routes()

def test_retrieve_no_matches(self, routes, index_cls, encoder_cls, router_cls):
encoder = encoder_cls()
index = init_index(index_cls, index_name=encoder.__class__.__name__)
route_layer = router_cls(
encoder=encoder,
routes=routes,
index=index,
auto_sync="local",
)
text = "Asparagus"
if index_cls is PineconeIndex:
time.sleep(PINECONE_SLEEP)
results = route_layer.retrieve_multiple_routes(text=text)
assert len(results) == 0, f"Expected no results, but got {len(results)}"

def test_retrieve_one_match(self, routes_3, index_cls, encoder_cls, router_cls):
encoder = encoder_cls()
index = init_index(index_cls, index_name=encoder.__class__.__name__)
route_layer = router_cls(
encoder=encoder,
routes=routes_3,
index=index,
auto_sync="local",
)
text = "Hello"
# set low threshold
route_layer.set_threshold(threshold=0.1, route_name="Route 1")
if index_cls is PineconeIndex:
time.sleep(PINECONE_SLEEP)
results = route_layer.retrieve_multiple_routes(text=text)
assert len(results) == 1, f"Expected one result, and got {len(results)}"
matched_routes = [result.name for result in results]
assert "Route 1" in matched_routes, "Expected 'Route 1' to be a match"

def test_retrieve_with_text_for_multiple_matches(
self, routes_2, index_cls, encoder_cls, router_cls
):
encoder = encoder_cls()
index = init_index(index_cls, index_name=encoder.__class__.__name__)
route_layer = router_cls(
encoder=encoder,
routes=routes_2,
index=index,
auto_sync="local",
)
text = "Hello"
route_layer.set_threshold(threshold=0.01, route_name=None)
if index_cls is PineconeIndex:
time.sleep(PINECONE_SLEEP)
results = route_layer.retrieve_multiple_routes(text=text)
assert len(results) == 2, "Expected two results"
matched_routes = [result.name for result in results]
assert "Route 1" in matched_routes, "Expected 'Route 1' to be a match"
assert "Route 2" in matched_routes, "Expected 'Route 2' to be a match"

def test_set_aggregation_method_with_unsupported_value(
self, routes, index_cls, encoder_cls, router_cls
):
Expand Down

0 comments on commit d6a2058

Please sign in to comment.