Base class for indices using Pydantic’s BaseModel. This class outlines the expected interface for index classes. Actual method implementations should be provided in subclasses.
Add embeddings to the index.
This method should be implemented by subclasses.
Arguments:
embeddings
(List[List[float]]
): List of embeddings to add to the index.routes
(List[str]
): List of routes to add to the index.utterances
(List[str]
): List of utterances to add to the index.function_schemas
(Optional[List[Dict[str, Any]]]
): List of function schemas to add to the index.metadata_list
(List[Dict[str, Any]]
): List of metadata to add to the index.Add vectors to the index asynchronously.
This method should be implemented by subclasses.
Arguments:
embeddings
(List[List[float]]
): List of embeddings to add to the index.routes
(List[str]
): List of routes to add to the index.utterances
(List[str]
): List of utterances to add to the index.function_schemas
(Optional[List[Dict[str, Any]]]
): List of function schemas to add to the index.metadata_list
(List[Dict[str, Any]]
): List of metadata to add to the index.Gets a list of route and utterance objects currently stored in the
index, including additional metadata.
Arguments:
include_metadata
(bool
): Whether to include function schemas and metadata in
the returned Utterance objects.Returns:
List[Utterance]
: A list of Utterance objects.
Gets a list of route and utterance objects currently stored in the
index, including additional metadata.
Arguments:
include_metadata
(bool
): Whether to include function schemas and metadata in
the returned Utterance objects.Returns:
List[Utterance]
: A list of Utterance objects.
Gets a list of route objects currently stored in the index.
Returns:
List[Route]
: A list of Route objects.
Deletes route by route name.
This method should be implemented by subclasses.
Arguments:
route_name
(str
): Name of the route to delete.Asynchronously delete specified route from index if it exists. Returns the IDs
of the vectors deleted. This method should be implemented by subclasses.
Arguments:
route_name
(str
): Name of the route to delete.Returns:
list[str]
: List of IDs of the vectors deleted.
Returns an IndexConfig object with index details such as type, dimensions,
and total vector count. This method should be implemented by subclasses.
Returns:
IndexConfig
: An IndexConfig object.
Checks if the index is ready to be used.
This method should be implemented by subclasses.
Returns:
bool
: True if the index is ready, False otherwise.
Checks if the index is ready to be used asynchronously.
This method should be implemented by subclasses.
Returns:
bool
: True if the index is ready, False otherwise.
Search the index for the query_vector and return top_k results.
This method should be implemented by subclasses.
Arguments:
vector
(np.ndarray
): The vector to search for.top_k
(int
): The number of results to return.route_filter
(Optional[List[str]]
): The routes to filter the search by.sparse_vector
(dict[int, float] | SparseEmbedding | None
): The sparse vector to search for.Returns:
Tuple[np.ndarray, List[str]]
: A tuple containing the query vector and a list of route names.
Search the index for the query_vector and return top_k results.
This method should be implemented by subclasses.
Arguments:
vector
(np.ndarray
): The vector to search for.top_k
(int
): The number of results to return.route_filter
(Optional[List[str]]
): The routes to filter the search by.sparse_vector
(dict[int, float] | SparseEmbedding | None
): The sparse vector to search for.Returns:
Tuple[np.ndarray, List[str]]
: A tuple containing the query vector and a list of route names.
Asynchronously get a list of route and utterance objects currently stored in the index.
This method should be implemented by subclasses.
Raises:
NotImplementedError
: If the method is not implemented by the subclass.Returns:
list[tuple]
: A list of tuples, each containing a route name and an associated utterance.
Deletes all records from the index.
This method should be implemented by subclasses.
Raises:
NotImplementedError
: If the method is not implemented by the subclass.Deletes or resets the index.
This method should be implemented by subclasses.
Raises:
NotImplementedError
: If the method is not implemented by the subclass.Deletes or resets the index asynchronously. This method should be implemented by subclasses.
Lock/unlock the index for a given scope (if applicable). If index
already locked/unlocked, raises ValueError.
Arguments:
scope
(str | None
): The scope to lock.wait
(int
): The number of seconds to wait for the index to be unlocked, if
set to 0, will raise an error if index is already locked/unlocked.Returns:
ConfigParameter
: The config parameter that was locked.
Lock/unlock the index for a given scope (if applicable). If index already locked/unlocked, raises ValueError.
Returns the total number of vectors in the index. If the index is not initialized
returns 0.
Returns:
int
: The total number of vectors.
Async version of len. Returns the total number of vectors in the index.
Default implementation just calls the sync version.
Returns:
int
: The total number of vectors.
Parses metadata from index to extract route, utterance, function
schema and additional metadata.
Arguments:
metadata
(List[Dict[str, Any]]
): List of metadata dictionaries.Returns:
List[Tuple]
: A list of tuples, each containing route, utterance, function schema and additional metadata.