Stephen.Plaid (Stephen v1.0.0)
View SourcePLAID-style indexing for efficient ColBERT retrieval.
PLAID (Performance-optimized Late Interaction Driver) uses centroid-based candidate generation for faster retrieval:
- Cluster all document token embeddings into K centroids
- Build inverted lists: centroid -> [doc_ids with tokens near that centroid]
- At query time, find nearest centroids for query tokens
- Retrieve candidate docs from inverted lists
- Rerank candidates with full MaxSim scoring
This achieves sub-linear search time for large collections.
Summary
Functions
Adds a single document to the index.
Removes a document from the index.
Removes multiple documents from the index.
Returns all document IDs in the index.
Gets the embeddings for a document.
Checks if a document exists in the index.
Indexes documents into the PLAID index.
Loads a PLAID index from disk.
Creates a new PLAID index.
Saves the PLAID index to disk.
Searches the PLAID index for documents matching a query.
Returns the number of documents in the index.
Updates a document in the index by replacing its embeddings.
Types
@type doc_id() :: term()
@type t() :: %Stephen.Plaid{ centroids: Nx.Tensor.t() | nil, doc_count: non_neg_integer(), doc_embeddings: %{required(term()) => Nx.Tensor.t()}, embedding_dim: pos_integer(), inverted_index: %{required(non_neg_integer()) => MapSet.t()}, num_centroids: pos_integer() }
Functions
@spec add_document(t(), doc_id(), Nx.Tensor.t()) :: t()
Adds a single document to the index.
Removes a document from the index.
Arguments
plaid- PLAID indexdoc_id- The document ID to remove
Returns
Updated PLAID index, or the original index if doc_id not found.
Removes multiple documents from the index.
Arguments
plaid- PLAID indexdoc_ids- List of document IDs to remove
Returns
Updated PLAID index.
Returns all document IDs in the index.
@spec get_embeddings(t(), doc_id()) :: Nx.Tensor.t() | nil
Gets the embeddings for a document.
Checks if a document exists in the index.
@spec index_documents(t(), [{doc_id(), Nx.Tensor.t()}]) :: t()
Indexes documents into the PLAID index.
The first call will train centroids on the provided embeddings. Subsequent calls will use the existing centroids.
Arguments
plaid- PLAID indexdocuments- List of {doc_id, embeddings} tuples
Loads a PLAID index from disk.
Arguments
path- File path to load from
Returns
{:ok, plaid} or {:error, reason}
Creates a new PLAID index.
Options
:embedding_dim- Dimension of embeddings (required):num_centroids- Number of centroids for clustering (default: 1024)
Examples
plaid = Stephen.Plaid.new(embedding_dim: 128, num_centroids: 1024)
Saves the PLAID index to disk.
Arguments
plaid- PLAID index to savepath- File path to save to
@spec search(t(), Nx.Tensor.t(), keyword()) :: [%{doc_id: doc_id(), score: float()}]
Searches the PLAID index for documents matching a query.
Arguments
plaid- PLAID indexquery_embeddings- Query token embeddingsopts- Search options
Options
:top_k- Number of results to return (default: 10):nprobe- Number of centroids to probe per query token (default: 32)
Returns
List of %{doc_id: term(), score: float()} sorted by score descending.
@spec size(t()) :: non_neg_integer()
Returns the number of documents in the index.
@spec update(t(), doc_id(), Nx.Tensor.t()) :: t()
Updates a document in the index by replacing its embeddings.
This is equivalent to deleting and re-adding the document.
Arguments
plaid- PLAID indexdoc_id- The document ID to updateembeddings- New embeddings tensor
Returns
Updated PLAID index.