Skip to content
Snippets Groups Projects
Unverified Commit 15c38d02 authored by James Briggs's avatar James Briggs Committed by GitHub
Browse files

Merge pull request #463 from aurelio-labs/james/function-schema-fix

fix: function schemas
parents 43c20152 a5103d65
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.dev0" !pip install -qU "semantic-router[pinecone]==0.1.0.dev1"
``` ```
%% 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, remote_utterances=remote_utterances local_utterances=local_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 local version takes priority and local `function_schemas` and `metadata` are propogated to all remote 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 local version takes priority and local `function_schemas` and `metadata` are propogated to all remote 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")
``` ```
......
...@@ -15,7 +15,7 @@ sys.path.insert(0, os.path.abspath("../..")) # Source code dir relative to this ...@@ -15,7 +15,7 @@ sys.path.insert(0, os.path.abspath("../..")) # Source code dir relative to this
project = "Semantic Router" project = "Semantic Router"
copyright = "2024, Aurelio AI" copyright = "2024, Aurelio AI"
author = "Aurelio AI" author = "Aurelio AI"
release = "0.1.0.dev0" release = "0.1.0.dev1"
# -- General configuration --------------------------------------------------- # -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
......
[tool.poetry] [tool.poetry]
name = "semantic-router" name = "semantic-router"
version = "0.1.0.dev0" version = "0.1.0.dev1"
description = "Super fast semantic router for AI decision making" description = "Super fast semantic router for AI decision making"
authors = ["Aurelio AI <hello@aurelio.ai>"] authors = ["Aurelio AI <hello@aurelio.ai>"]
readme = "README.md" readme = "README.md"
......
...@@ -4,4 +4,4 @@ from semantic_router.route import Route ...@@ -4,4 +4,4 @@ from semantic_router.route import Route
__all__ = ["RouteLayer", "HybridRouteLayer", "Route", "LayerConfig"] __all__ = ["RouteLayer", "HybridRouteLayer", "Route", "LayerConfig"]
__version__ = "0.1.0.dev0" __version__ = "0.1.0.dev1"
...@@ -488,7 +488,7 @@ class RouteLayer: ...@@ -488,7 +488,7 @@ class RouteLayer:
remote_utterances=remote_utterances, remote_utterances=remote_utterances,
) )
# generate sync strategy # generate sync strategy
sync_strategy = diff.to_sync_strategy() sync_strategy = diff.get_sync_strategy(sync_mode=sync_mode)
# and execute # and execute
self._execute_sync_strategy(sync_strategy) self._execute_sync_strategy(sync_strategy)
return diff.to_utterance_str() return diff.to_utterance_str()
......
...@@ -98,6 +98,8 @@ class Utterance(BaseModel): ...@@ -98,6 +98,8 @@ class Utterance(BaseModel):
""" """
route, utterance = tuple_obj[0], tuple_obj[1] route, utterance = tuple_obj[0], tuple_obj[1]
function_schemas = tuple_obj[2] if len(tuple_obj) > 2 else None function_schemas = tuple_obj[2] if len(tuple_obj) > 2 else None
if isinstance(function_schemas, dict):
function_schemas = [function_schemas]
metadata = tuple_obj[3] if len(tuple_obj) > 3 else {} metadata = tuple_obj[3] if len(tuple_obj) > 3 else {}
return cls( return cls(
route=route, route=route,
......
...@@ -381,6 +381,21 @@ class TestRouteLayer: ...@@ -381,6 +381,21 @@ class TestRouteLayer:
Utterance(route="Route 3", utterance="Boo"), Utterance(route="Route 3", utterance="Boo"),
], "The routes in the index should match the local routes" ], "The routes in the index should match the local routes"
@pytest.mark.skipif(
os.environ.get("PINECONE_API_KEY") is None, reason="Pinecone API key required"
)
def test_sync(self, openai_encoder, index_cls):
route_layer = RouteLayer(
encoder=openai_encoder,
routes=[],
index=init_index(index_cls),
auto_sync=None,
)
route_layer.sync("remote")
time.sleep(PINECONE_SLEEP) # allow for index to be populated
# confirm local and remote are synced
assert route_layer.is_synced()
@pytest.mark.skipif( @pytest.mark.skipif(
os.environ.get("PINECONE_API_KEY") is None, reason="Pinecone API key required" os.environ.get("PINECONE_API_KEY") is None, reason="Pinecone API key required"
) )
......
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