Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescalam authored Oct 28, 2024
2 parents e3d6973 + 0874205 commit 37ec903
Show file tree
Hide file tree
Showing 16 changed files with 1,961 additions and 1,551 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lint lint_diff:
poetry run mypy $(PYTHON_FILES)

test:
poetry run pytest -vv -n 20 --cov=semantic_router --cov-report=term-missing --cov-report=xml
poetry run pytest -vv --cov=semantic_router --cov-report=term-missing --cov-report=xml

test_functional:
poetry run pytest -vv -n 20 tests/functional
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

Semantic Router is a superfast decision-making layer for your LLMs and agents. Rather than waiting for slow LLM generations to make tool-use decisions, we use the magic of semantic vector space to make those decisions — _routing_ our requests using _semantic_ meaning.

#### [Read the Docs](https://docs.aurelio.ai/semantic-router/index.html)

---

## Quickstart
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
project = "Semantic Router"
copyright = "2024, Aurelio AI"
author = "Aurelio AI"
release = "0.0.65"
release = "0.0.72"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down
8 changes: 7 additions & 1 deletion docs/source/route_layer/sync.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@ You can try this yourself by running the following:
rl = RouteLayer(encoder=encoder, routes=routes, index=pc_index)
When initializing the `PineconeIndex` object, we can specify the `sync` parameter.
When initializing the `PineconeIndex` object, we can specify the `sync` parameter.

Checking for Synchronization
----------------------------

To verify whether the local and remote instances are synchronized, you can use the `is_synced` method. This method checks if the routes, utterances, and associated metadata in the local instance match those stored in the remote index.
Consider that if the `sync` flag is not set (e.g. for indexes different from Pinecone), it raises an error. If the index supports sync feature and everything aligns, it returns `True`, indicating that the local and remote instances are synchronized, otherwise it returns `False`.
2,739 changes: 1,396 additions & 1,343 deletions poetry.lock

Large diffs are not rendered by default.

20 changes: 6 additions & 14 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
[tool.poetry]
name = "semantic-router"
version = "0.0.65"
version = "0.0.72"
description = "Super fast semantic router for AI decision making"
authors = [
"James Briggs <[email protected]>",
"Siraj Aizlewood <[email protected]>",
"Simonas Jakubonis <[email protected]>",
"Luca Mannini <[email protected]>",
"Bogdan Buduroiu <[email protected]>",
"Ismail Ashraq <[email protected]>",
"Daniel Griffin <[email protected]>",
"Tolga Arslan <[email protected]>"
]
authors = ["Aurelio AI <[email protected]>"]
readme = "README.md"
packages = [{include = "semantic_router"}]
license = "MIT"
Expand All @@ -20,7 +11,7 @@ license = "MIT"
python = ">=3.9,<3.13"
pydantic = "^2.5.3"
openai = ">=1.10.0,<2.0.0"
cohere = ">=5.00,<6.00"
cohere = {version = ">=5.9.4,<6.00", optional = true}
mistralai= {version = ">=0.0.12,<0.1.0", optional = true}
numpy = "^1.25.2"
colorlog = "^6.8.0"
Expand All @@ -31,7 +22,7 @@ transformers = {version = ">=4.36.2", optional = true}
tokenizers = {version = ">=0.19", optional = true}
llama-cpp-python = {version = ">=0.2.28,<0.2.86", optional = true}
colorama = "^0.4.6"
pinecone-client = {version=">=3.0.0,<4.0.0", optional = true}
pinecone = {version=">=5.0.0", optional = true}
regex = ">=2023.12.25"
torchvision = { version = ">=0.17.0,<0.18.0", optional = true}
pillow = { version = ">=10.2.0,<11.0.0", optional = true}
Expand All @@ -52,7 +43,7 @@ sphinxawesome-theme = {version = "^5.2.0", optional = true}
[tool.poetry.extras]
hybrid = ["pinecone-text"]
local = ["torch", "transformers", "tokenizers", "huggingface-hub", "llama-cpp-python"]
pinecone = ["pinecone-client"]
pinecone = ["pinecone"]
vision = ["torch", "torchvision", "transformers", "pillow"]
processing = ["matplotlib"]
mistralai = ["mistralai"]
Expand All @@ -63,6 +54,7 @@ bedrock = ["boto3", "botocore"]
postgres = ["psycopg2"]
fastembed = ["fastembed"]
docs = ["sphinx", "sphinxawesome-theme"]
cohere = ["cohere"]

[tool.poetry.group.dev.dependencies]
ipykernel = "^6.25.0"
Expand Down
2 changes: 1 addition & 1 deletion semantic_router/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

__all__ = ["RouteLayer", "HybridRouteLayer", "Route", "LayerConfig"]

__version__ = "0.0.65"
__version__ = "0.0.72"
38 changes: 30 additions & 8 deletions semantic_router/encoders/cohere.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import os
from typing import List, Optional
from typing import Any, List, Optional

import cohere
from cohere.types.embed_response import EmbedResponse_EmbeddingsByType
from pydantic.v1 import PrivateAttr

from semantic_router.encoders import BaseEncoder
from semantic_router.utils.defaults import EncoderDefault


class CohereEncoder(BaseEncoder):
client: Optional[cohere.Client] = None
_client: Any = PrivateAttr()
_embed_type: Any = PrivateAttr()
type: str = "cohere"
input_type: Optional[str] = "search_query"

Expand All @@ -28,25 +28,47 @@ def __init__(
input_type=input_type, # type: ignore
)
self.input_type = input_type
self._client = self._initialize_client(cohere_api_key)

def _initialize_client(self, cohere_api_key: Optional[str] = None):
"""Initializes the Cohere client.
:param cohere_api_key: The API key for the Cohere client, can also
be set via the COHERE_API_KEY environment variable.
:return: An instance of the Cohere client.
"""
try:
import cohere
from cohere.types.embed_response import EmbeddingsByTypeEmbedResponse

self._embed_type = EmbeddingsByTypeEmbedResponse
except ImportError:
raise ImportError(
"Please install Cohere to use CohereEncoder. "
"You can install it with: "
"`pip install 'semantic-router[cohere]'`"
)
cohere_api_key = cohere_api_key or os.getenv("COHERE_API_KEY")
if cohere_api_key is None:
raise ValueError("Cohere API key cannot be 'None'.")
try:
self.client = cohere.Client(cohere_api_key)
client = cohere.Client(cohere_api_key)
except Exception as e:
raise ValueError(
f"Cohere API client failed to initialize. Error: {e}"
) from e
return client

def __call__(self, docs: List[str]) -> List[List[float]]:
if self.client is None:
if self._client is None:
raise ValueError("Cohere client is not initialized.")
try:
embeds = self.client.embed(
embeds = self._client.embed(
texts=docs, input_type=self.input_type, model=self.name
)
# Check for unsupported type.
if isinstance(embeds, EmbedResponse_EmbeddingsByType):
if isinstance(embeds, self._embed_type):
raise NotImplementedError(
"Handling of EmbedByTypeResponseEmbeddings is not implemented."
)
Expand Down
13 changes: 13 additions & 0 deletions semantic_router/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ def delete_index(self):
"""
raise NotImplementedError("This method should be implemented by subclasses.")

def is_synced(
self,
local_route_names: List[str],
local_utterances_list: List[str],
local_function_schemas_list: List[Dict[str, Any]],
local_metadata_list: List[Dict[str, Any]],
) -> bool:
"""
Checks whether local and remote index are synchronized.
This method should be implemented by subclasses.
"""
raise NotImplementedError("This method should be implemented by subclasses.")

def _sync_index(
self,
local_route_names: List[str],
Expand Down
Loading

0 comments on commit 37ec903

Please sign in to comment.