semantic_router.schema
EncoderType Objects
class EncoderType(Enum)The type of encoder.
EncoderInfo Objects
class EncoderInfo(BaseModel)Information about an encoder.
RouteChoice Objects
class RouteChoice(BaseModel)A route choice typically output by the routers.
Message Objects
class Message(BaseModel)A message in a conversation, includes the role and content fields.
to_openai
def to_openai()Convert the message to an OpenAI-compatible format.
to_cohere
def to_cohere()Convert the message to a Cohere-compatible format.
to_llamacpp
def to_llamacpp()Convert the message to a LlamaCPP-compatible format.
to_mistral
def to_mistral()Convert the message to a Mistral-compatible format.
to_voyage
def to_voyage()Convert the message to a Voyage-compatible format.
to_jina
def to_jina()Convert the message to a Jina-compatible format.
__str__
def __str__()Convert the message to a string.
ConfigParameter Objects
class ConfigParameter(BaseModel)A configuration parameter for a route. Used for remote router metadata such as router hashes, sync locks, etc.
to_pinecone
def to_pinecone(dimensions: int)Convert the configuration parameter to a Pinecone-compatible format. Should
be used when upserting configuration parameters to a separate config namespace within your Pinecone index.
Arguments:
dimensions(int): The dimensions of the Pinecone index.
Returns:
dict: A Pinecone-compatible configuration parameter.
Utterance Objects
class Utterance(BaseModel)An utterance in a conversation, includes the route, utterance, function schemas, metadata, and diff tag.
from_tuple
@classmethod
def from_tuple(cls, tuple_obj: Tuple)Create an Utterance object from a tuple. The tuple must contain
route and utterance as the first two elements. Then optionally function schemas and metadata as the third and fourth elements respectively. If this order is not followed an invalid Utterance object will be returned.
Arguments:
tuple_obj(Tuple): A tuple containing route, utterance, function schemas and metadata.
Returns:
Utterance: An Utterance object.
to_tuple
def to_tuple()Convert an Utterance object to a tuple.
Returns:
Tuple: A tuple containing (route, utterance, function schemas, metadata).
to_str
def to_str(include_metadata: bool = False)Convert an Utterance object to a string. Used for comparisons during sync
check operations.
Arguments:
include_metadata(bool): Whether to include metadata in the string.
Returns:
str: A string representation of the Utterance object.
SyncMode Objects
class SyncMode(Enum)Synchronization modes for local (route layer) and remote (index) instances.
UtteranceDiff Objects
class UtteranceDiff(BaseModel)A list of Utterance objects that represent the differences between local and remote utterances.
from_utterances
@classmethod
def from_utterances(cls, local_utterances: List[Utterance],
remote_utterances: List[Utterance])Create a UtteranceDiff object from two lists of Utterance objects.
Arguments:
local_utterances(List[Utterance]): A list of Utterance objects.remote_utterances(List[Utterance]): A list of Utterance objects.
to_utterance_str
def to_utterance_str(include_metadata: bool = False) -> List[str]Outputs the utterance diff as a list of diff strings. Returns a list
of strings showing what is different in the remote when compared to the local. For example:
[" route1: utterance1", " route1: utterance2", "- route2: utterance3", "- route2: utterance4"]
Tells us that the remote is missing "route2: utterance3" and "route2: utterance4", which do exist locally. If we see:
[" route1: utterance1", " route1: utterance2", "+ route2: utterance3", "+ route2: utterance4"]
This diff tells us that the remote has "route2: utterance3" and "route2: utterance4", which do not exist locally.
Arguments:
include_metadata(bool): Whether to include metadata in the string.
Returns:
List[str]: A list of diff strings.
get_tag
def get_tag(diff_tag: str) -> List[Utterance]Get all utterances with a given diff tag.
Arguments:
diff_tag(str): The diff tag to filter by. Must be one of "+", "-", or " ".
Returns:
List[Utterance]: A list of Utterance objects.
get_sync_strategy
def get_sync_strategy(sync_mode: str) -> dictGenerates the optimal synchronization plan for local and remote instances.
Arguments:
sync_mode(str): The mode to sync the routes with the remote index.
Returns:
dict: A dictionary describing the synchronization strategy.
Metric Objects
class Metric(Enum)The metric to use in vector-based similarity search indexes.
SparseEmbedding Objects
class SparseEmbedding(BaseModel)Sparse embedding interface. Primarily uses numpy operations for faster operations.
from_compact_array
@classmethod
def from_compact_array(cls, array: np.ndarray)Create a SparseEmbedding object from a compact array.
Arguments:
array(np.ndarray): A compact array.
Returns:
SparseEmbedding: A SparseEmbedding object.
from_vector
@classmethod
def from_vector(cls, vector: np.ndarray)Consumes an array of sparse vectors containing zero-values.
Arguments:
vector(np.ndarray): A sparse vector.
Returns:
SparseEmbedding: A SparseEmbedding object.
from_aurelio
@classmethod
def from_aurelio(cls, embedding: BM25SparseEmbedding)Create a SparseEmbedding object from an AurelioSparseEmbedding object.
Arguments:
embedding(BM25SparseEmbedding): An AurelioSparseEmbedding object.
Returns:
SparseEmbedding: A SparseEmbedding object.
from_dict
@classmethod
def from_dict(cls, sparse_dict: dict)Create a SparseEmbedding object from a dictionary.
Arguments:
sparse_dict(dict): A dictionary of sparse values.
Returns:
SparseEmbedding: A SparseEmbedding object.
from_pinecone_dict
@classmethod
def from_pinecone_dict(cls, sparse_dict: dict)Create a SparseEmbedding object from a Pinecone dictionary.
Arguments:
sparse_dict(dict): A Pinecone dictionary.
Returns:
SparseEmbedding: A SparseEmbedding object.
to_dict
def to_dict()Convert a SparseEmbedding object to a dictionary.
Returns:
dict: A dictionary of sparse values.
to_pinecone
def to_pinecone()Convert a SparseEmbedding object to a Pinecone dictionary.
Returns:
dict: A Pinecone dictionary.
items
def items()Return a list of (index, value) tuples from the SparseEmbedding object.
Returns:
list: A list of (index, value) tuples.