semantic_router.schema
EncoderType Objects
The type of encoder.
EncoderInfo Objects
Information about an encoder.
RouteChoice Objects
A route choice typically output by the routers.
Message Objects
A message in a conversation, includes the role and content fields.
to_openai
Convert the message to an OpenAI-compatible format.
to_cohere
Convert the message to a Cohere-compatible format.
to_llamacpp
Convert the message to a LlamaCPP-compatible format.
to_mistral
Convert the message to a Mistral-compatible format.
__str__
Convert the message to a string.
ConfigParameter Objects
A configuration parameter for a route. Used for remote router metadata such as router hashes, sync locks, etc.
to_pinecone
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
An utterance in a conversation, includes the route, utterance, function schemas, metadata, and diff tag.
from_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
Convert an Utterance object to a tuple.
Returns:
Tuple
: A tuple containing (route, utterance, function schemas, metadata).
to_str
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
Synchronization modes for local (route layer) and remote (index) instances.
UtteranceDiff Objects
A list of Utterance objects that represent the differences between local and remote utterances.
from_utterances
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
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
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
Generates 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
The metric to use in vector-based similarity search indexes.
SparseEmbedding Objects
Sparse embedding interface. Primarily uses numpy operations for faster operations.
from_compact_array
Create a SparseEmbedding object from a compact array.
Arguments:
array
(np.ndarray
): A compact array.
Returns:
SparseEmbedding
: A SparseEmbedding object.
from_vector
Consumes an array of sparse vectors containing zero-values.
Arguments:
vector
(np.ndarray
): A sparse vector.
Returns:
SparseEmbedding
: A SparseEmbedding object.
from_aurelio
Create a SparseEmbedding object from an AurelioSparseEmbedding object.
Arguments:
embedding
(BM25SparseEmbedding
): An AurelioSparseEmbedding object.
Returns:
SparseEmbedding
: A SparseEmbedding object.
from_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
Create a SparseEmbedding object from a Pinecone dictionary.
Arguments:
sparse_dict
(dict
): A Pinecone dictionary.
Returns:
SparseEmbedding
: A SparseEmbedding object.
to_dict
Convert a SparseEmbedding object to a dictionary.
Returns:
dict
: A dictionary of sparse values.
to_pinecone
Convert a SparseEmbedding object to a Pinecone dictionary.
Returns:
dict
: A Pinecone dictionary.
items
Return a list of (index, value) tuples from the SparseEmbedding object.
Returns:
list
: A list of (index, value) tuples.