Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
simjak committed Dec 20, 2023
2 parents bf3d5d6 + 610ea58 commit a75f260
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 43 deletions.
21 changes: 9 additions & 12 deletions coverage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<lines>
<line number="1" hits="1"/>
<line number="2" hits="1"/>
<line number="3" hits="1"/>
<line number="5" hits="1"/>
<line number="4" hits="1"/>
<line number="10" hits="1"/>
<line number="11" hits="1"/>
<line number="13" hits="1"/>
<line number="16" hits="1"/>
Expand Down Expand Up @@ -93,9 +93,7 @@
<line number="118" hits="1"/>
<line number="119" hits="1"/>
<line number="121" hits="1"/>
<line number="122" hits="1"/>
<line number="123" hits="1"/>
<line number="124" hits="1"/>
<line number="125" hits="1"/>
<line number="126" hits="1"/>
<line number="127" hits="1"/>
Expand Down Expand Up @@ -619,19 +617,18 @@
<line number="8" hits="1"/>
<line number="23" hits="1"/>
<line number="24" hits="1"/>
<line number="25" hits="1"/>
<line number="26" hits="1"/>
<line number="27" hits="1"/>
<line number="29" hits="1"/>
<line number="28" hits="1"/>
<line number="31" hits="1"/>
<line number="32" hits="1"/>
<line number="33" hits="1"/>
<line number="35" hits="1"/>
<line number="37" hits="1"/>
<line number="38" hits="1"/>
<line number="40" hits="1"/>
<line number="41" hits="1"/>
<line number="42" hits="1"/>
<line number="44" hits="1"/>
<line number="46" hits="1"/>
<line number="47" hits="1"/>
<line number="49" hits="1"/>
<line number="52" hits="1"/>
<line number="43" hits="1"/>
</lines>
</class>
</classes>
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/function_calling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -37,7 +37,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down
27 changes: 11 additions & 16 deletions docs/examples/hybrid-layer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"metadata": {},
"outputs": [],
"source": [
"!pip install -qU semantic-router==0.0.6"
"!pip install -qU semantic-router==0.0.11"
]
},
{
Expand All @@ -46,21 +46,9 @@
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"ename": "ImportError",
"evalue": "cannot import name 'Route' from 'semantic_router.schema' (/Users/jakit/customers/aurelio/semantic-router/.venv/lib/python3.11/site-packages/semantic_router/schema.py)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/Users/jakit/customers/aurelio/semantic-router/docs/examples/hybrid-layer.ipynb Cell 7\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/jakit/customers/aurelio/semantic-router/docs/examples/hybrid-layer.ipynb#X10sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msemantic_router\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mschema\u001b[39;00m \u001b[39mimport\u001b[39;00m Route\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/jakit/customers/aurelio/semantic-router/docs/examples/hybrid-layer.ipynb#X10sZmlsZQ%3D%3D?line=2'>3</a>\u001b[0m politics \u001b[39m=\u001b[39m Route(\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/jakit/customers/aurelio/semantic-router/docs/examples/hybrid-layer.ipynb#X10sZmlsZQ%3D%3D?line=3'>4</a>\u001b[0m name\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mpolitics\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/jakit/customers/aurelio/semantic-router/docs/examples/hybrid-layer.ipynb#X10sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m utterances\u001b[39m=\u001b[39m[\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/jakit/customers/aurelio/semantic-router/docs/examples/hybrid-layer.ipynb#X10sZmlsZQ%3D%3D?line=11'>12</a>\u001b[0m ],\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/jakit/customers/aurelio/semantic-router/docs/examples/hybrid-layer.ipynb#X10sZmlsZQ%3D%3D?line=12'>13</a>\u001b[0m )\n",
"\u001b[0;31mImportError\u001b[0m: cannot import name 'Route' from 'semantic_router.schema' (/Users/jakit/customers/aurelio/semantic-router/.venv/lib/python3.11/site-packages/semantic_router/schema.py)"
]
}
],
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantic_router.schema import Route\n",
"\n",
Expand All @@ -84,6 +72,13 @@
"Let's define another for good measure:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
38 changes: 35 additions & 3 deletions semantic_router/hybrid_layer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
from numpy.linalg import norm
from tqdm.auto import tqdm

from semantic_router.encoders import (
BaseEncoder,
Expand Down Expand Up @@ -35,8 +34,9 @@ def __init__(
# if routes list has been passed, we initialize index now
if routes:
# initialize index now
for route in tqdm(routes):
self._add_route(route=route)
# for route in tqdm(routes):
# self._add_route(route=route)
self._add_routes(routes)

def __call__(self, text: str) -> str | None:
results = self._query(text)
Expand Down Expand Up @@ -78,6 +78,38 @@ def _add_route(self, route: Route):
else:
self.sparse_index = np.concatenate([self.sparse_index, sparse_embeds])

def _add_routes(self, routes: list[Route]):
# create embeddings for all routes
logger.info("Creating embeddings for all routes...")
all_utterances = [
utterance for route in routes for utterance in route.utterances
]
dense_embeds = np.array(self.encoder(all_utterances))
sparse_embeds = np.array(self.sparse_encoder(all_utterances))

# create route array
route_names = [route.name for route in routes for _ in route.utterances]
route_array = np.array(route_names)
self.categories = (
np.concatenate([self.categories, route_array])
if self.categories is not None
else route_array
)

# create utterance array (the dense index)
self.index = (
np.concatenate([self.index, dense_embeds])
if self.index is not None
else dense_embeds
)

# create sparse utterance array
self.sparse_index = (
np.concatenate([self.sparse_index, sparse_embeds])
if self.sparse_index is not None
else sparse_embeds
)

def _query(self, text: str, top_k: int = 5):
"""Given some text, encodes and searches the index vector space to
retrieve the top_k most similar records.
Expand Down
9 changes: 0 additions & 9 deletions semantic_router/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,9 @@ def __init__(self):

def add_coloured_handler(logger):
formatter = CustomFormatter()

console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)

logging.basicConfig(
datefmt="%Y-%m-%d %H:%M:%S",
format="%(log_color)s%(asctime)s %(levelname)s %(name)s %(message)s",
force=True,
)

logger.addHandler(console_handler)

return logger


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_hybrid_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_initialization_different_encoders(self, cohere_encoder, openai_encoder)
def test_add_route(self, openai_encoder):
route_layer = HybridRouteLayer(encoder=openai_encoder)
route = Route(name="Route 3", utterances=["Yes", "No"])
route_layer.add(route)
route_layer._add_routes([route])
assert route_layer.index is not None and route_layer.categories is not None
assert len(route_layer.index) == 2
assert len(set(route_layer.categories)) == 1
Expand Down

0 comments on commit a75f260

Please sign in to comment.