Skip to main content

Overview

BaseEmbeddingModel is the abstract base class that defines the interface for all embedding models in Remem. It provides a consistent API for encoding text into vector embeddings and computing query-document similarity scores.

Class Definition

from remem.embedding_model import BaseEmbeddingModel
Defined in: src/remem/embedding_model/base.py:178

Attributes

global_config
BaseConfig
Global configuration object containing system-wide settings
embedding_model_name
str
Name of the embedding model (e.g., “nvidia/NV-Embed-v2”, “text-embedding-3-large”)
embedding_config
EmbeddingConfig
Model-specific configuration parameters
embedding_dim
int
Dimensionality of the embedding vectors (set by subclass)

Methods

__init__

def __init__(self, global_config: Optional[BaseConfig] = None) -> None
Initializes the base embedding model. Parameters:
global_config
BaseConfig
default:"None"
Global configuration object. If None, uses default BaseConfig instance.
Example:
from remem.utils.config_utils import BaseConfig
from remem.embedding_model import BaseEmbeddingModel

config = BaseConfig()
model = BaseEmbeddingModel(global_config=config)

batch_encode

def batch_encode(self, texts: List[str], **kwargs) -> np.ndarray
Encodes a batch of text strings into embeddings. Must be implemented by subclasses. Parameters:
texts
List[str]
required
List of text strings to encode
**kwargs
dict
Additional model-specific parameters:
  • instruction: Optional instruction prefix for the embeddings
  • batch_size: Number of texts to process at once
  • max_length: Maximum sequence length
Returns:
embeddings
np.ndarray
2D numpy array of shape (n_texts, embedding_dim)
Raises:
  • NotImplementedError: This method must be implemented by subclasses

get_query_doc_scores

def get_query_doc_scores(self, query_vec: np.ndarray, doc_vecs: np.ndarray) -> np.ndarray
Computes similarity scores between a query vector and document vectors using dot product. Parameters:
query_vec
np.ndarray
required
Query embedding vector of shape (embedding_dim,)
doc_vecs
np.ndarray
required
Document embedding matrix of shape (n_docs, embedding_dim)
Returns:
scores
np.ndarray
Array of similarity scores of shape (n_docs,)
Example:
import numpy as np

# Get embeddings
query_emb = model.batch_encode(["What is machine learning?"])[0]
doc_embs = model.batch_encode([
    "Machine learning is a subset of AI",
    "Python is a programming language"
])

# Compute similarity scores
scores = model.get_query_doc_scores(query_emb, doc_embs)
print(scores)  # [0.85, 0.32]

EmbeddingConfig

EmbeddingConfig is a flexible configuration class that stores model-specific parameters. Defined in: src/remem/embedding_model/base.py:14

Methods

from_dict

@classmethod
def from_dict(cls, config_dict: Dict[str, Any]) -> "EmbeddingConfig"
Creates an EmbeddingConfig instance from a dictionary. Parameters:
config_dict
Dict[str, Any]
required
Dictionary containing configuration parameters
Returns:
config
EmbeddingConfig
New EmbeddingConfig instance

to_dict

def to_dict(self) -> Dict[str, Any]
Exports the configuration as a JSON-serializable dictionary. Returns:
config_dict
Dict[str, Any]
Dictionary representation of the configuration

batch_upsert

def batch_upsert(self, updates: Dict[str, Any]) -> None
Updates or adds multiple configuration parameters at once. Parameters:
updates
Dict[str, Any]
required
Dictionary of parameters to update or add
Example:
from remem.embedding_model import EmbeddingConfig

config = EmbeddingConfig()
config.batch_upsert({
    "batch_size": 32,
    "max_length": 512,
    "norm": True
})

print(config.batch_size)  # 32
print(config.to_dict())   # {"batch_size": 32, "max_length": 512, "norm": True}

Caching Utilities

make_cache_embed

def make_cache_embed(encode_func, cache_file_name, device)
Creates a caching wrapper for embedding functions using SQLite. This decorator caches embeddings to avoid recomputing them for the same inputs. Defined in: src/remem/embedding_model/base.py:103 Parameters:
encode_func
callable
required
The encoding function to wrap
cache_file_name
str
required
Path to SQLite cache database file
device
str
required
Device to place cached embeddings on (e.g., “cuda”, “cpu”)
Returns:
wrapper
callable
Wrapped function that uses caching

See Also

Build docs developers (and LLMs) love