Skip to content

Commit

Permalink
Merge branch 'main' into azure-dyn-route-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescalam authored Jan 20, 2024
2 parents 0ecadd2 + c607aed commit c4f2ab4
Show file tree
Hide file tree
Showing 26 changed files with 262 additions and 130 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

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.


---

## Quickstart
Expand All @@ -25,7 +26,7 @@ To get started with _semantic-router_ we install it like so:
pip install -qU semantic-router
```

❗️ _If wanting to use local embeddings you can use `FastEmbedEncoder` (`pip install -qU "semantic-router[fastembed]`"). To use the `HybridRouteLayer` you must `pip install -qU "semantic-router[hybrid]"`._
❗️ _If wanting to use a fully local version of semantic router you can use `HuggingFaceEncoder` and `LlamaCppEncoder` (`pip install -qU "semantic-router[local]"`, see [here](https://github.com/aurelio-labs/semantic-router/blob/main/docs/05-local-execution.ipynb)). To use the `HybridRouteLayer` you must `pip install -qU "semantic-router[hybrid]"`._

We begin by defining a set of `Route` objects. These are the decision paths that the semantic router can decide to use, let's try two simple routes for now — one for talk on _politics_ and another for _chitchat_:

Expand Down
2 changes: 1 addition & 1 deletion docs/00-introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"metadata": {},
"outputs": [],
"source": [
"!pip install -qU semantic-router==0.0.16"
"!pip install -qU semantic-router==0.0.17"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/01-save-load-from-file.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"metadata": {},
"outputs": [],
"source": [
"!pip install -qU semantic-router==0.0.16"
"!pip install -qU semantic-router==0.0.17"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/02-dynamic-routes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"outputs": [],
"source": [
"!pip install -qU semantic-router==0.0.16"
"!pip install -qU semantic-router==0.0.17"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/03-basic-langchain-agent.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
],
"source": [
"!pip install -qU \\\n",
" semantic-router==0.0.16 \\\n",
" semantic-router==0.0.17 \\\n",
" langchain==0.0.352 \\\n",
" openai==1.6.1"
]
Expand Down
4 changes: 2 additions & 2 deletions docs/05-local-execution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"metadata": {},
"outputs": [],
"source": [
"!pip install -qU \"semantic-router[local]==0.0.16\""
"!pip install -qU \"semantic-router[local]==0.0.17\""
]
},
{
Expand Down Expand Up @@ -342,7 +342,7 @@
"from semantic_router import RouteLayer\n",
"\n",
"from llama_cpp import Llama\n",
"from semantic_router.llms import LlamaCppLLM\n",
"from semantic_router.llms.llamacpp import LlamaCppLLM\n",
"\n",
"enable_gpu = True # offload LLM layers to the GPU (must fit in memory)\n",
"\n",
Expand Down
299 changes: 212 additions & 87 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "semantic-router"
version = "0.0.16"
version = "0.0.17"
description = "Super fast semantic router for AI decision making"
authors = [
"James Briggs <james@aurelio.ai>",
Expand All @@ -16,7 +16,7 @@ packages = [{include = "semantic_router"}]

[tool.poetry.dependencies]
python = "^3.9"
pydantic = "^1.8.2"
pydantic = "^2.5.3"
openai = "^1.3.9"
cohere = "^4.32"
numpy = "^1.25.2"
Expand Down
2 changes: 2 additions & 0 deletions semantic_router/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
from semantic_router.route import Route

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

__version__ = "0.0.17"
2 changes: 1 addition & 1 deletion semantic_router/encoders/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from pydantic import BaseModel, Field
from pydantic.v1 import BaseModel, Field


class BaseEncoder(BaseModel):
Expand Down
6 changes: 4 additions & 2 deletions semantic_router/encoders/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def __init__(
try:
self.client = cohere.Client(cohere_api_key)
except Exception as e:
raise ValueError(f"Cohere API client failed to initialize. Error: {e}")
raise ValueError(
f"Cohere API client failed to initialize. Error: {e}"
) from e

def __call__(self, docs: List[str]) -> List[List[float]]:
if self.client is None:
Expand All @@ -34,4 +36,4 @@ def __call__(self, docs: List[str]) -> List[List[float]]:
embeds = self.client.embed(docs, input_type="search_query", model=self.name)
return embeds.embeddings
except Exception as e:
raise ValueError(f"Cohere API call failed. Error: {e}")
raise ValueError(f"Cohere API call failed. Error: {e}") from e
4 changes: 2 additions & 2 deletions semantic_router/encoders/fastembed.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, List, Optional

import numpy as np
from pydantic import PrivateAttr
from pydantic.v1 import PrivateAttr

from semantic_router.encoders import BaseEncoder

Expand Down Expand Up @@ -48,4 +48,4 @@ def __call__(self, docs: List[str]) -> List[List[float]]:
embeddings: List[List[float]] = [e.tolist() for e in embeds]
return embeddings
except Exception as e:
raise ValueError(f"FastEmbed embed failed. Error: {e}")
raise ValueError(f"FastEmbed embed failed. Error: {e}") from e
2 changes: 1 addition & 1 deletion semantic_router/encoders/huggingface.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any, List, Optional

from pydantic import PrivateAttr
from pydantic.v1 import PrivateAttr

from semantic_router.encoders import BaseEncoder

Expand Down
6 changes: 4 additions & 2 deletions semantic_router/encoders/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def __init__(
try:
self.client = openai.Client(api_key=api_key)
except Exception as e:
raise ValueError(f"OpenAI API client failed to initialize. Error: {e}")
raise ValueError(
f"OpenAI API client failed to initialize. Error: {e}"
) from e

def __call__(self, docs: List[str]) -> List[List[float]]:
if self.client is None:
Expand All @@ -49,7 +51,7 @@ def __call__(self, docs: List[str]) -> List[List[float]]:
logger.warning(f"Retrying in {2**j} seconds...")
except Exception as e:
logger.error(f"OpenAI API call failed. Error: {error_message}")
raise ValueError(f"OpenAI API call failed. Error: {e}")
raise ValueError(f"OpenAI API call failed. Error: {e}") from e

if (
not embeds
Expand Down
6 changes: 4 additions & 2 deletions semantic_router/encoders/zure.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def __init__(
# _strict_response_validation=True,
)
except Exception as e:
raise ValueError(f"OpenAI API client failed to initialize. Error: {e}")
raise ValueError(
f"OpenAI API client failed to initialize. Error: {e}"
) from e

def __call__(self, docs: List[str]) -> List[List[float]]:
if self.client is None:
Expand All @@ -100,7 +102,7 @@ def __call__(self, docs: List[str]) -> List[List[float]]:
logger.warning(f"Retrying in {2**j} seconds...")
except Exception as e:
logger.error(f"Azure OpenAI API call failed. Error: {error_message}")
raise ValueError(f"Azure OpenAI API call failed. Error: {e}")
raise ValueError(f"Azure OpenAI API call failed. Error: {e}") from e

if (
not embeds
Expand Down
10 changes: 1 addition & 9 deletions semantic_router/llms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
from semantic_router.llms.base import BaseLLM
from semantic_router.llms.cohere import CohereLLM
from semantic_router.llms.llamacpp import LlamaCppLLM
from semantic_router.llms.openai import OpenAILLM
from semantic_router.llms.openrouter import OpenRouterLLM
from semantic_router.llms.zure import AzureOpenAILLM

__all__ = [
"BaseLLM",
"OpenAILLM",
"OpenRouterLLM",
"CohereLLM",
"LlamaCppLLM",
"AzureOpenAILLM",
]
__all__ = ["BaseLLM", "OpenAILLM", "OpenRouterLLM", "CohereLLM"]
2 changes: 1 addition & 1 deletion semantic_router/llms/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from typing import Any, List, Optional

from pydantic import BaseModel
from pydantic.v1 import BaseModel

from semantic_router.schema import Message
from semantic_router.utils.logger import logger
Expand Down
6 changes: 4 additions & 2 deletions semantic_router/llms/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def __init__(
try:
self.client = cohere.Client(cohere_api_key)
except Exception as e:
raise ValueError(f"Cohere API client failed to initialize. Error: {e}")
raise ValueError(
f"Cohere API client failed to initialize. Error: {e}"
) from e

def __call__(self, messages: List[Message]) -> str:
if self.client is None:
Expand All @@ -43,4 +45,4 @@ def __call__(self, messages: List[Message]) -> str:
return output

except Exception as e:
raise ValueError(f"Cohere API call failed. Error: {e}")
raise ValueError(f"Cohere API call failed. Error: {e}") from e
6 changes: 4 additions & 2 deletions semantic_router/llms/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def __init__(
try:
self.client = openai.OpenAI(api_key=api_key)
except Exception as e:
raise ValueError(f"OpenAI API client failed to initialize. Error: {e}")
raise ValueError(
f"OpenAI API client failed to initialize. Error: {e}"
) from e
self.temperature = temperature
self.max_tokens = max_tokens

Expand All @@ -51,4 +53,4 @@ def __call__(self, messages: List[Message]) -> str:
return output
except Exception as e:
logger.error(f"LLM error: {e}")
raise Exception(f"LLM error: {e}")
raise Exception(f"LLM error: {e}") from e
6 changes: 4 additions & 2 deletions semantic_router/llms/openrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def __init__(
try:
self.client = openai.OpenAI(api_key=api_key, base_url=self.base_url)
except Exception as e:
raise ValueError(f"OpenRouter API client failed to initialize. Error: {e}")
raise ValueError(
f"OpenRouter API client failed to initialize. Error: {e}"
) from e
self.temperature = temperature
self.max_tokens = max_tokens

Expand All @@ -56,4 +58,4 @@ def __call__(self, messages: List[Message]) -> str:
return output
except Exception as e:
logger.error(f"LLM error: {e}")
raise Exception(f"LLM error: {e}")
raise Exception(f"LLM error: {e}") from e
2 changes: 1 addition & 1 deletion semantic_router/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from typing import Any, Callable, Dict, List, Optional, Union

from pydantic import BaseModel
from pydantic.v1 import BaseModel

from semantic_router.llms import BaseLLM
from semantic_router.schema import Message, RouteChoice
Expand Down
4 changes: 2 additions & 2 deletions semantic_router/schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from enum import Enum
from typing import Dict, List, Literal, Optional

from pydantic import BaseModel
from pydantic.dataclasses import dataclass
from pydantic.v1 import BaseModel
from pydantic.v1.dataclasses import dataclass

from semantic_router.encoders import (
BaseEncoder,
Expand Down
2 changes: 1 addition & 1 deletion semantic_router/utils/function_call.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
from typing import Any, Callable, Dict, List, Union

from pydantic import BaseModel
from pydantic.v1 import BaseModel

from semantic_router.llms import BaseLLM
from semantic_router.schema import Message, RouteChoice
Expand Down
4 changes: 2 additions & 2 deletions semantic_router/utils/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def llm(prompt: str) -> Optional[str]:
return output
except Exception as e:
logger.error(f"LLM error: {e}")
raise Exception(f"LLM error: {e}")
raise Exception(f"LLM error: {e}") from e


# TODO integrate async LLM function
Expand Down Expand Up @@ -62,4 +62,4 @@ def llm(prompt: str) -> Optional[str]:
# return output
# except Exception as e:
# logger.error(f"LLM error: {e}")
# raise Exception(f"LLM error: {e}")
# raise Exception(f"LLM error: {e}") from e
2 changes: 1 addition & 1 deletion tests/unit/llms/test_llm_llamacpp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from llama_cpp import Llama

from semantic_router.llms import LlamaCppLLM
from semantic_router.llms.llamacpp import LlamaCppLLM
from semantic_router.schema import Message


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from pydantic import ValidationError
from pydantic.v1 import ValidationError

from semantic_router.schema import (
CohereEncoder,
Expand Down

0 comments on commit c4f2ab4

Please sign in to comment.