Skip to content
Snippets Groups Projects
Commit fe0a3304 authored by James Briggs's avatar James Briggs
Browse files

chore: lint

parent 0c570391
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#!pip install -qU "semantic-router[pinecone]==0.1.0-alpha.1" #!pip install -qU "semantic-router[pinecone]==0.1.0-alpha.1"
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Syncing Routes with Pinecone Index # Syncing Routes with Pinecone Index
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
When using the `PineconeIndex`, our `RouteLayer` is stored in two places: When using the `PineconeIndex`, our `RouteLayer` is stored in two places:
* We keep route layer metadata locally. * We keep route layer metadata locally.
* Vectors alongside a backup of our metadata is stored remotely in Pinecone. * Vectors alongside a backup of our metadata is stored remotely in Pinecone.
By storing some data locally and some remotely we achieve improved persistence and the ability to recover our local state if lost. However, it does come with challenges around keep our local and remote instances synchronized. Fortunately, we have [several synchronization options](https://docs.aurelio.ai/semantic-router/route_layer/sync.html). In this example, we'll see how to use these options to keep our local and remote Pinecone instances synchronized. By storing some data locally and some remotely we achieve improved persistence and the ability to recover our local state if lost. However, it does come with challenges around keep our local and remote instances synchronized. Fortunately, we have [several synchronization options](https://docs.aurelio.ai/semantic-router/route_layer/sync.html). In this example, we'll see how to use these options to keep our local and remote Pinecone instances synchronized.
%% 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 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!",
], ],
) )
# 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 OpenAIEncoder from semantic_router.encoders import OpenAIEncoder
# get at platform.openai.com # get at platform.openai.com
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY") or getpass( os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY") or getpass(
"Enter OpenAI API key: " "Enter OpenAI API key: "
) )
encoder = OpenAIEncoder(name="text-embedding-3-small") encoder = OpenAIEncoder(name="text-embedding-3-small")
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
For our `PineconeIndex` we do the exact same thing, ie we initialize as usual: For our `PineconeIndex` we do the exact same thing, ie we initialize as usual:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import os import os
from semantic_router.index.pinecone import PineconeIndex from semantic_router.index.pinecone import PineconeIndex
# get at app.pinecone.io # get at app.pinecone.io
os.environ["PINECONE_API_KEY"] = os.environ.get("PINECONE_API_KEY") or getpass( os.environ["PINECONE_API_KEY"] = os.environ.get("PINECONE_API_KEY") or getpass(
"Enter Pinecone API key: " "Enter Pinecone API key: "
) )
pc_index = PineconeIndex( pc_index = PineconeIndex(
dimensions=1536, dimensions=1536,
init_async_index=True, # enables asynchronous methods, it's optional init_async_index=True, # enables asynchronous methods, it's optional
sync=None, # defines whether we sync between local and remote route layers sync=None, # defines whether we sync between local and remote route layers
# when sync is None, no sync is performed # when sync is None, no sync is performed
) )
pc_index.index = pc_index._init_index(force_create=True) pc_index.index = pc_index._init_index(force_create=True)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## RouteLayer ## RouteLayer
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
The `RouteLayer` class supports both sync and async operations by default, so we initialize as usual: The `RouteLayer` class supports both sync and async operations by default, so we initialize as usual:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from semantic_router.layer import RouteLayer from semantic_router.layer import RouteLayer
import time import time
rl = RouteLayer(encoder=encoder, routes=routes, index=pc_index) rl = RouteLayer(encoder=encoder, routes=routes, index=pc_index)
# due to pinecone indexing latency we wait 3 seconds # due to pinecone indexing latency we wait 3 seconds
time.sleep(3) time.sleep(3)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Let's see if our local and remote instances are synchronized... Let's see if our local and remote instances are synchronized...
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.is_synced() rl.is_synced()
``` ```
%% Output %% Output
hash_id: sr_hash# hash_id: sr_hash#
False False
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
It looks like everything is synced! Let's try deleting our local route layer, initializing it with just the politics route, and checking again. It looks like everything is synced! Let's try deleting our local route layer, initializing it with just the politics route, and checking again.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
del rl del rl
rl = RouteLayer(encoder=encoder, routes=[politics], index=pc_index) rl = RouteLayer(encoder=encoder, routes=[politics], index=pc_index)
time.sleep(3) time.sleep(3)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Let's try `rl.is_synced()` again: Let's try `rl.is_synced()` again:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.is_synced() rl.is_synced()
``` ```
%% Output %% Output
hash_id: sr_hash# hash_id: sr_hash#
False False
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can use the `get_utterance_diff` method to see exactly _why_ our local and remote are not synced We can use the `get_utterance_diff` method to see exactly _why_ our local and remote are not synced
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.get_utterance_diff() rl.get_utterance_diff()
``` ```
%% Output %% Output
["- politics: don't you just hate the president", ["- politics: don't you just hate the president",
"- politics: don't you just love the president", "- politics: don't you just love the president",
"- politics: isn't politics the best thing ever", "- politics: isn't politics the best thing ever",
'- politics: they will save the country!', '- politics: they will save the country!',
"- politics: they're going to destroy this country!", "- politics: they're going to destroy this country!",
"- politics: why don't you tell me about your political opinions", "- politics: why don't you tell me about your political opinions",
'+ Route 1: Hello', '+ Route 1: Hello',
'+ Route 1: Hi', '+ Route 1: Hi',
'+ Route 2: Au revoir', '+ Route 2: Au revoir',
'+ Route 2: Bye', '+ Route 2: Bye',
'+ Route 2: Goodbye', '+ Route 2: Goodbye',
'+ Route 3: Boo'] '+ Route 3: Boo']
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Handling Synchronization ## Handling Synchronization
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We may want to handle the resynchronization ourselves and to do that we ideally want a more structured version of the utterance diff returned above. To create that we first need to get a list of utterance objects from our remote and local instances: We may want to handle the resynchronization ourselves and to do that we ideally want a more structured version of the utterance diff returned above. To create that we first need to get a list of utterance objects from our remote and local instances:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
remote_utterances = rl.index.get_utterances() remote_utterances = rl.index.get_utterances()
remote_utterances remote_utterances
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
local_utterances = rl.to_config().to_utterances() local_utterances = rl.to_config().to_utterances()
local_utterances local_utterances
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can add the `diff_tag` attribute to each of these utterances by loading both lists into a `UtteranceDiff` object: We can add the `diff_tag` attribute to each of these utterances by loading both lists into a `UtteranceDiff` object:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from semantic_router.schema import UtteranceDiff from semantic_router.schema import UtteranceDiff
diff = UtteranceDiff.from_utterances( diff = UtteranceDiff.from_utterances(
local_utterances=local_utterances, local_utterances=local_utterances, remote_utterances=remote_utterances
remote_utterances=remote_utterances
) )
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
`UtteranceDiff` objects include all diff information inside the `diff` attribute (which is a list of `Utterance` objects): `UtteranceDiff` objects include all diff information inside the `diff` attribute (which is a list of `Utterance` objects):
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
diff.diff diff.diff
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Each of our `Utterance` objects now contains a populate `diff_tag` attribute. Where: Each of our `Utterance` objects now contains a populate `diff_tag` attribute. Where:
* `diff_tag='+'` means the utterance exists in the remote instance *only* * `diff_tag='+'` means the utterance exists in the remote instance *only*
* `diff_tag='-'` means the utterance exists in the local instance *only* * `diff_tag='-'` means the utterance exists in the local instance *only*
* `diff_tag=' '` means the utterance exists in both remote and local instances * `diff_tag=' '` means the utterance exists in both remote and local instances
So, to collect utterances missing from our local instance we can run: So, to collect utterances missing from our local instance we can run:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
diff.get_tag("+") diff.get_tag("+")
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
To collect utterances missing from our remote instance we can run: To collect utterances missing from our remote instance we can run:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
diff.get_tag("-") diff.get_tag("-")
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
And, if needed, we can get all utterances that exist in both with: And, if needed, we can get all utterances that exist in both with:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
diff.get_tag(" ") diff.get_tag(" ")
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Synchronization ## Synchronization
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
There are six synchronization methods that we can use, those are: There are six synchronization methods that we can use, those are:
* `error`: Raise an error if local and remote are not synchronized. * `error`: Raise an error if local and remote are not synchronized.
* `remote`: Take remote as the source of truth and update local to align. * `remote`: Take remote as the source of truth and update local to align.
* `local`: Take local as the source of truth and update remote to align. * `local`: Take local as the source of truth and update remote to align.
* `merge-force-remote`: Merge both local and remote keeping local as the priority. Remote utterances are only merged into local *if* a matching route for the utterance is found in local, all other route-utterances are dropped. Where a route exists in both local and remote, but each contains different `function_schema` or `metadata` information, the local version takes priority and local `function_schemas` and `metadata` is propogated to all remote utterances belonging to the given route. * `merge-force-remote`: Merge both local and remote keeping local as the priority. Remote utterances are only merged into local *if* a matching route for the utterance is found in local, all other route-utterances are dropped. Where a route exists in both local and remote, but each contains different `function_schema` or `metadata` information, the local version takes priority and local `function_schemas` and `metadata` is propogated to all remote utterances belonging to the given route.
* `merge-force-local`: Merge both local and remote keeping remote as the priority. Local utterances are only merged into remote *if* a matching route for the utterance is found in the remote, all other route-utterances are dropped. Where a route exists in both local and remote, but each contains different `function_schema` or `metadata` information, the remote version takes priotity and remote `function_schemas` and `metadata` are propogated to all local routes. * `merge-force-local`: Merge both local and remote keeping remote as the priority. Local utterances are only merged into remote *if* a matching route for the utterance is found in the remote, all other route-utterances are dropped. Where a route exists in both local and remote, but each contains different `function_schema` or `metadata` information, the remote version takes priotity and remote `function_schemas` and `metadata` are propogated to all local routes.
* `merge`: Merge both local and remote, merging also local and remote utterances when a route with same route name is present both locally and remotely. If a route exists in both local and remote but contains different `function_schemas` or `metadata` information, the remote version takes priority and remote `function_schemas` and `metadata` are propogated to all local routes. * `merge`: Merge both local and remote, merging also local and remote utterances when a route with same route name is present both locally and remotely. If a route exists in both local and remote but contains different `function_schemas` or `metadata` information, the remote version takes priority and remote `function_schemas` and `metadata` are propogated to all local routes.
We can get the synchronization strategy for each of these (with the exception of `error`) using the `diff.get_sync_strategy` method. We can get the synchronization strategy for each of these (with the exception of `error`) using the `diff.get_sync_strategy` method.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
diff.get_sync_strategy("local") diff.get_sync_strategy("local")
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
diff.get_sync_strategy("remote") diff.get_sync_strategy("remote")
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
diff.get_sync_strategy("merge-force-remote") diff.get_sync_strategy("merge-force-remote")
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
diff.get_sync_strategy("merge-force-local") diff.get_sync_strategy("merge-force-local")
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
diff.get_sync_strategy("merge") diff.get_sync_strategy("merge")
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Each of these sync strategies can be fed to our route layer via the `rl._execute_sync_strategy` method: Each of these sync strategies can be fed to our route layer via the `rl._execute_sync_strategy` method:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
strategy = diff.get_sync_strategy("local") strategy = diff.get_sync_strategy("local")
rl._execute_sync_strategy(strategy=strategy) rl._execute_sync_strategy(strategy=strategy)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
time.sleep(3) time.sleep(3)
rl.is_synced() rl.is_synced()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can check our diff method to see what the `local` sync did: We can check our diff method to see what the `local` sync did:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.get_utterance_diff() rl.get_utterance_diff()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
As expected, it took all local utterances and applied them to the remote instance, removing all utterances that were only present in the remote instance. As expected, it took all local utterances and applied them to the remote instance, removing all utterances that were only present in the remote instance.
We can simplify this process significantly by running the `rl.sync` method with our chosen `sync_mode`: We can simplify this process significantly by running the `rl.sync` method with our chosen `sync_mode`:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.sync(sync_mode="local") rl.sync(sync_mode="local")
``` ```
......
...@@ -645,4 +645,6 @@ class PineconeIndex(BaseIndex): ...@@ -645,4 +645,6 @@ class PineconeIndex(BaseIndex):
) )
def __len__(self): def __len__(self):
return self.index.describe_index_stats()["namespaces"][self.namespace]["vector_count"] return self.index.describe_index_stats()["namespaces"][self.namespace][
"vector_count"
]
...@@ -90,7 +90,7 @@ class ConfigParameter(BaseModel): ...@@ -90,7 +90,7 @@ class ConfigParameter(BaseModel):
class Utterance(BaseModel): class Utterance(BaseModel):
route: str route: str
utterance: str utterance: Union[str, Any]
function_schemas: Optional[List[Dict]] = None function_schemas: Optional[List[Dict]] = None
metadata: dict = {} metadata: dict = {}
diff_tag: str = " " diff_tag: str = " "
......
...@@ -186,6 +186,7 @@ def get_test_indexes(): ...@@ -186,6 +186,7 @@ def get_test_indexes():
return indexes return indexes
def get_test_encoders(): def get_test_encoders():
encoders = [OpenAIEncoder] encoders = [OpenAIEncoder]
if importlib.util.find_spec("cohere") is not None: if importlib.util.find_spec("cohere") is not None:
...@@ -193,17 +194,23 @@ def get_test_encoders(): ...@@ -193,17 +194,23 @@ def get_test_encoders():
return encoders return encoders
@pytest.mark.parametrize("index_cls,encoder_cls", [ @pytest.mark.parametrize(
(index, encoder) "index_cls,encoder_cls",
for index in get_test_indexes() [
for encoder in get_test_encoders() (index, encoder)
]) for index in get_test_indexes()
for encoder in get_test_encoders()
],
)
class TestIndexEncoders: class TestIndexEncoders:
def test_initialization(self, routes, openai_encoder, index_cls, encoder_cls): def test_initialization(self, routes, openai_encoder, index_cls, encoder_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=encoder_cls(), routes=routes, index=index, encoder=encoder_cls(),
auto_sync="local", top_k=10, routes=routes,
index=index,
auto_sync="local",
top_k=10,
) )
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
time.sleep(PINECONE_SLEEP) # allow for index to be populated time.sleep(PINECONE_SLEEP) # allow for index to be populated
...@@ -218,9 +225,7 @@ class TestIndexEncoders: ...@@ -218,9 +225,7 @@ class TestIndexEncoders:
else 0 == 2 else 0 == 2
) )
def test_initialization_different_encoders( def test_initialization_different_encoders(self, encoder_cls, index_cls):
self, encoder_cls, index_cls
):
index = init_index(index_cls) index = init_index(index_cls)
encoder = encoder_cls() encoder = encoder_cls()
route_layer = RouteLayer(encoder=encoder, index=index) route_layer = RouteLayer(encoder=encoder, index=index)
...@@ -239,7 +244,9 @@ class TestRouteLayer: ...@@ -239,7 +244,9 @@ class TestRouteLayer:
): ):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=dynamic_routes, index=index, encoder=openai_encoder,
routes=dynamic_routes,
index=index,
auto_sync="local", auto_sync="local",
) )
assert route_layer.score_threshold == openai_encoder.score_threshold assert route_layer.score_threshold == openai_encoder.score_threshold
...@@ -248,7 +255,9 @@ class TestRouteLayer: ...@@ -248,7 +255,9 @@ class TestRouteLayer:
# TODO merge .delete_index() and .delete_all() and get working # TODO merge .delete_index() and .delete_all() and get working
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
...@@ -261,8 +270,7 @@ class TestRouteLayer: ...@@ -261,8 +270,7 @@ class TestRouteLayer:
def test_add_route(self, routes, openai_encoder, index_cls): def test_add_route(self, routes, openai_encoder, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=[], index=index, encoder=openai_encoder, routes=[], index=index, auto_sync="local"
auto_sync="local"
) )
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
time.sleep(PINECONE_SLEEP) # allow for index to be updated time.sleep(PINECONE_SLEEP) # allow for index to be updated
...@@ -290,7 +298,9 @@ class TestRouteLayer: ...@@ -290,7 +298,9 @@ class TestRouteLayer:
def test_list_route_names(self, openai_encoder, routes, index_cls): def test_list_route_names(self, openai_encoder, routes, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
...@@ -303,7 +313,9 @@ class TestRouteLayer: ...@@ -303,7 +313,9 @@ class TestRouteLayer:
def test_delete_route(self, openai_encoder, routes, index_cls): def test_delete_route(self, openai_encoder, routes, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
...@@ -336,7 +348,8 @@ class TestRouteLayer: ...@@ -336,7 +348,8 @@ class TestRouteLayer:
def test_add_multiple_routes(self, openai_encoder, routes, index_cls): def test_add_multiple_routes(self, openai_encoder, routes, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, index=index, encoder=openai_encoder,
index=index,
auto_sync="local", auto_sync="local",
) )
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
...@@ -350,7 +363,9 @@ class TestRouteLayer: ...@@ -350,7 +363,9 @@ class TestRouteLayer:
def test_query_and_classification(self, openai_encoder, routes, index_cls): def test_query_and_classification(self, openai_encoder, routes, index_cls):
index = init_index(index_cls, dimensions=3) index = init_index(index_cls, dimensions=3)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
...@@ -361,7 +376,9 @@ class TestRouteLayer: ...@@ -361,7 +376,9 @@ class TestRouteLayer:
def test_query_filter(self, openai_encoder, routes, index_cls): def test_query_filter(self, openai_encoder, routes, index_cls):
index = init_index(index_cls, dimensions=3) index = init_index(index_cls, dimensions=3)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
...@@ -382,7 +399,9 @@ class TestRouteLayer: ...@@ -382,7 +399,9 @@ class TestRouteLayer:
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
pineconeindex = init_index(index_cls, dimensions=3) pineconeindex = init_index(index_cls, dimensions=3)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=pineconeindex, encoder=openai_encoder,
routes=routes,
index=pineconeindex,
auto_sync="local", auto_sync="local",
) )
time.sleep(PINECONE_SLEEP) # allow for index to be populated time.sleep(PINECONE_SLEEP) # allow for index to be populated
...@@ -402,7 +421,9 @@ class TestRouteLayer: ...@@ -402,7 +421,9 @@ class TestRouteLayer:
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
pineconeindex = init_index(index_cls, namespace="test") pineconeindex = init_index(index_cls, namespace="test")
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=pineconeindex, encoder=openai_encoder,
routes=routes,
index=pineconeindex,
auto_sync="local", auto_sync="local",
) )
time.sleep(PINECONE_SLEEP) # allow for index to be populated time.sleep(PINECONE_SLEEP) # allow for index to be populated
...@@ -424,7 +445,9 @@ class TestRouteLayer: ...@@ -424,7 +445,9 @@ class TestRouteLayer:
def test_query_with_vector(self, openai_encoder, routes, index_cls): def test_query_with_vector(self, openai_encoder, routes, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
...@@ -442,7 +465,9 @@ class TestRouteLayer: ...@@ -442,7 +465,9 @@ class TestRouteLayer:
def test_semantic_classify(self, openai_encoder, routes, index_cls): def test_semantic_classify(self, openai_encoder, routes, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
...@@ -459,7 +484,9 @@ class TestRouteLayer: ...@@ -459,7 +484,9 @@ class TestRouteLayer:
def test_semantic_classify_multiple_routes(self, openai_encoder, routes, index_cls): def test_semantic_classify_multiple_routes(self, openai_encoder, routes, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
if index_cls is PineconeIndex: if index_cls is PineconeIndex:
...@@ -488,7 +515,8 @@ class TestRouteLayer: ...@@ -488,7 +515,8 @@ class TestRouteLayer:
def test_pass_threshold(self, openai_encoder, index_cls): def test_pass_threshold(self, openai_encoder, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, index=index, encoder=openai_encoder,
index=index,
auto_sync="local", auto_sync="local",
) )
assert not route_layer._pass_threshold([], 0.3) assert not route_layer._pass_threshold([], 0.3)
...@@ -497,7 +525,8 @@ class TestRouteLayer: ...@@ -497,7 +525,8 @@ class TestRouteLayer:
def test_failover_score_threshold(self, openai_encoder, index_cls): def test_failover_score_threshold(self, openai_encoder, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, index=index, encoder=openai_encoder,
index=index,
auto_sync="local", auto_sync="local",
) )
assert route_layer.score_threshold == 0.3 assert route_layer.score_threshold == 0.3
...@@ -510,7 +539,9 @@ class TestRouteLayer: ...@@ -510,7 +539,9 @@ class TestRouteLayer:
os.environ["OPENAI_API_KEY"] = "test_api_key" os.environ["OPENAI_API_KEY"] = "test_api_key"
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
route_layer.to_json(temp_path) route_layer.to_json(temp_path)
...@@ -533,7 +564,9 @@ class TestRouteLayer: ...@@ -533,7 +564,9 @@ class TestRouteLayer:
os.environ["OPENAI_API_KEY"] = "test_api_key" os.environ["OPENAI_API_KEY"] = "test_api_key"
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
route_layer.to_yaml(temp_path) route_layer.to_yaml(temp_path)
...@@ -735,7 +768,9 @@ class TestRouteLayer: ...@@ -735,7 +768,9 @@ class TestRouteLayer:
def test_retrieve_with_text(self, openai_encoder, routes, index_cls): def test_retrieve_with_text(self, openai_encoder, routes, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
text = "Hello" text = "Hello"
...@@ -748,7 +783,9 @@ class TestRouteLayer: ...@@ -748,7 +783,9 @@ class TestRouteLayer:
def test_retrieve_with_vector(self, openai_encoder, routes, index_cls): def test_retrieve_with_vector(self, openai_encoder, routes, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
vector = [0.1, 0.2, 0.3] vector = [0.1, 0.2, 0.3]
...@@ -761,7 +798,9 @@ class TestRouteLayer: ...@@ -761,7 +798,9 @@ class TestRouteLayer:
def test_retrieve_without_text_or_vector(self, openai_encoder, routes, index_cls): def test_retrieve_without_text_or_vector(self, openai_encoder, routes, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
with pytest.raises(ValueError, match="Either text or vector must be provided"): with pytest.raises(ValueError, match="Either text or vector must be provided"):
...@@ -770,7 +809,9 @@ class TestRouteLayer: ...@@ -770,7 +809,9 @@ class TestRouteLayer:
def test_retrieve_no_matches(self, openai_encoder, routes, index_cls): def test_retrieve_no_matches(self, openai_encoder, routes, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, index=index, encoder=openai_encoder,
routes=routes,
index=index,
auto_sync="local", auto_sync="local",
) )
text = "Asparagus" text = "Asparagus"
...@@ -780,7 +821,9 @@ class TestRouteLayer: ...@@ -780,7 +821,9 @@ class TestRouteLayer:
def test_retrieve_one_match(self, openai_encoder, routes_3, index_cls): def test_retrieve_one_match(self, openai_encoder, routes_3, index_cls):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes_3, index=index, encoder=openai_encoder,
routes=routes_3,
index=index,
auto_sync="local", auto_sync="local",
) )
text = "Hello" text = "Hello"
...@@ -796,7 +839,9 @@ class TestRouteLayer: ...@@ -796,7 +839,9 @@ class TestRouteLayer:
): ):
index = init_index(index_cls) index = init_index(index_cls)
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes_2, index=index, encoder=openai_encoder,
routes=routes_2,
index=index,
auto_sync="local", auto_sync="local",
) )
text = "Hello" text = "Hello"
...@@ -871,7 +916,8 @@ class TestRouteLayer: ...@@ -871,7 +916,8 @@ class TestRouteLayer:
class TestLayerFit: class TestLayerFit:
def test_eval(self, openai_encoder, routes, test_data): def test_eval(self, openai_encoder, routes, test_data):
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, encoder=openai_encoder,
routes=routes,
auto_sync="local", auto_sync="local",
) )
# unpack test data # unpack test data
...@@ -881,7 +927,8 @@ class TestLayerFit: ...@@ -881,7 +927,8 @@ class TestLayerFit:
def test_fit(self, openai_encoder, routes, test_data): def test_fit(self, openai_encoder, routes, test_data):
route_layer = RouteLayer( route_layer = RouteLayer(
encoder=openai_encoder, routes=routes, encoder=openai_encoder,
routes=routes,
auto_sync="local", auto_sync="local",
) )
# unpack test data # unpack test data
......
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