nemoguardrails.embeddings.basic
Module Contents
Classes
Data
API
Bases: EmbeddingsIndex
Basic implementation of an embeddings index.
It uses the sentence-transformers/all-MiniLM-L6-v2 model to compute embeddings.
Exact cosine nearest-neighbor search is performed over a NumPy matrix of
L2-normalized embeddings, so search results are exact (no approximation).
Get the cache configuration.
Get the size of the embeddings.
Get the computed embeddings.
Get the current embedding index
Compute embeddings for a list of texts.
Parameters:
The list of texts to compute embeddings for.
Returns: List[List[float]]
List[List[float]]: The computed embeddings.
Initialize the model used for computing the embeddings.
Runs the current batch of embeddings.
Add a single item to the index.
Parameters:
The item to add to the index.
Add multiple items to the index at once.
Parameters:
The list of items to add to the index.
Builds the embeddings index.
Stores an L2-normalized float32 matrix of the computed embeddings. Because
rows are normalized, the dot product between a normalized query and a row
equals their cosine similarity. search ranks by this exact cosine value
and converts it to the previous Annoy-compatible score for thresholding.
Restore a previously persisted index from disk.
Persist the built index to disk as a NumPy .npy file.
Search the closest max_results items.
Parameters:
The text to search for.
The maximum number of results to return. Defaults to 20.
Returns: List[IndexItem]
List[IndexItem]: The closest items found.