Embeddings¶
Factory¶
memory_reuse.embeddings.create_embedder ¶
Create an :class:EmbeddingProvider from a cache configuration.
The provider is selected by :attr:CacheConfig.embedding_provider and
configured with :attr:CacheConfig.embedding_model (when set). Concrete
provider modules are imported lazily so their optional dependencies are
only required when that provider is actually selected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
CacheConfig
|
The cache configuration. Its |
required |
Returns:
| Type | Description |
|---|---|
EmbeddingProvider
|
A concrete :class: |
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If |
EmbeddingProvider (interface)¶
memory_reuse.embeddings.base.EmbeddingProvider ¶
Bases: ABC
Interface that all embedding providers must implement.
The :attr:identity string namespaces stored vectors so embeddings from
different providers or models are never compared, and :attr:dimension
lets callers validate vector shape before a similarity search.
Implementors should raise
:class:~memory_reuse.exceptions.EmbeddingProviderError (with an install
hint) when their optional dependency is missing, and should keep
:attr:identity stable for a given provider+model pairing.
identity
abstractmethod
property
¶
Return the stable "provider:model" identity string.
This value namespaces stored vectors so that embeddings produced by
different providers or models are never compared against one another
(for example "openai:text-embedding-3-small").
Returns:
| Type | Description |
|---|---|
str
|
The provider+model identity string. |
dimension
abstractmethod
property
¶
Return the dimensionality of the produced embedding vectors.
Returns:
| Type | Description |
|---|---|
int
|
The number of floats in each embedding vector. |
embed
abstractmethod
async
¶
Return the embedding vector for a single piece of text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
The embedding vector as a list of floats of length |
list[float]
|
attr: |
embed_batch
async
¶
Return embedding vectors for multiple texts.
The default implementation calls :meth:embed sequentially. Providers
that support a native batch call should override this for efficiency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
The texts to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
A list of embedding vectors, one per input text, in the same |
list[list[float]]
|
order as |
LocalEmbedder¶
memory_reuse.embeddings.local.LocalEmbedder ¶
Bases: EmbeddingProvider
Embedding provider backed by a local sentence-transformers model.
The heavy sentence-transformers dependency and the model itself are
loaded lazily on first use, so constructing a :class:LocalEmbedder is
cheap and importing this module has no import-time side effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str | None
|
The sentence-transformers model name to load. |
None
|
Raises:
| Type | Description |
|---|---|
EmbeddingProviderError
|
On first use, if |
identity
property
¶
Return the stable "local:<model>" identity string.
Returns:
| Type | Description |
|---|---|
str
|
The provider+model identity, e.g. |
dimension
property
¶
Return the dimensionality of the produced embedding vectors.
Loads the model on first access to query its true output dimension.
Returns:
| Type | Description |
|---|---|
int
|
The number of floats in each embedding vector. |
Raises:
| Type | Description |
|---|---|
EmbeddingProviderError
|
If |
embed
async
¶
Return the embedding vector for a single piece of text.
The synchronous, potentially CPU-bound model call is run in a worker thread so it does not block the event loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
The embedding vector as a list of floats of length |
list[float]
|
attr: |
Raises:
| Type | Description |
|---|---|
EmbeddingProviderError
|
If |
embed_batch
async
¶
Return embedding vectors for multiple texts in a single model call.
sentence-transformers encodes batches efficiently, so this overrides the default sequential implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
The texts to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
A list of embedding vectors, one per input text, in the same order |
list[list[float]]
|
as |
Raises:
| Type | Description |
|---|---|
EmbeddingProviderError
|
If |
OpenAIEmbedder¶
memory_reuse.embeddings.openai.OpenAIEmbedder ¶
Bases: EmbeddingProvider
Embedding provider backed by OpenAI's hosted embeddings API.
The openai dependency and the API client are created lazily on first
use, so constructing an :class:OpenAIEmbedder is cheap and importing this
module has no import-time side effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str | None
|
The OpenAI embedding model name. |
None
|
api_key
|
str | None
|
An explicit API key. |
None
|
Raises:
| Type | Description |
|---|---|
EmbeddingProviderError
|
On first use, if |
identity
property
¶
Return the stable "openai:<model>" identity string.
Returns:
| Type | Description |
|---|---|
str
|
The provider+model identity, e.g. |
dimension
property
¶
Return the dimensionality of the produced embedding vectors.
The value is looked up from a table of known OpenAI embedding models, so no network call is made. Unknown models fall back to the default model's dimension.
Returns:
| Type | Description |
|---|---|
int
|
The number of floats in each embedding vector. |
embed
async
¶
Return the embedding vector for a single piece of text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
The embedding vector as a list of floats of length |
list[float]
|
attr: |
Raises:
| Type | Description |
|---|---|
EmbeddingProviderError
|
If |
embed_batch
async
¶
Return embedding vectors for multiple texts in a single API call.
The OpenAI embeddings API accepts a list of inputs, so this overrides the default sequential implementation to reduce the number of requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
The texts to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
A list of embedding vectors, one per input text, in the same order |
list[list[float]]
|
as |
Raises:
| Type | Description |
|---|---|
EmbeddingProviderError
|
If |
LiteLLMEmbedder¶
memory_reuse.embeddings.litellm.LiteLLMEmbedder ¶
Bases: EmbeddingProvider
Embedding provider backed by LiteLLM's uniform embedding API.
LiteLLM routes to whichever backend the model string names, so a single
provider class covers AWS Bedrock, OpenAI, Cohere, and every other
LiteLLM-supported embedding model. The heavy litellm dependency is
imported lazily on first use, so constructing a :class:LiteLLMEmbedder is
cheap and importing this module has no import-time side effects.
Because the embedding dimension depends on the underlying model (and is only
known reliably after a call), :attr:dimension is discovered from the first
embedding response and cached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str | None
|
The LiteLLM model string, e.g. |
None
|
Raises:
| Type | Description |
|---|---|
EmbeddingProviderError
|
On first use, if |
identity
property
¶
Return the stable "litellm:<model>" identity string.
Returns:
| Type | Description |
|---|---|
str
|
The provider+model identity, e.g. |
str
|
|
dimension
property
¶
Return the dimensionality of the produced embedding vectors.
LiteLLM abstracts over many models whose dimensionality differs and is
not known without a call, so the dimension is discovered lazily from the
first :meth:embed / :meth:embed_batch response and cached. Calling
this before any embedding has been produced raises
:class:EmbeddingProviderError.
Returns:
| Type | Description |
|---|---|
int
|
The number of floats in each embedding vector. |
Raises:
| Type | Description |
|---|---|
EmbeddingProviderError
|
If no embedding has been produced yet, so the dimension is not yet known. |
embed
async
¶
Return the embedding vector for a single piece of text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
The embedding vector as a list of floats of length |
list[float]
|
attr: |
Raises:
| Type | Description |
|---|---|
EmbeddingProviderError
|
If |
embed_batch
async
¶
Return embedding vectors for multiple texts in a single API call.
LiteLLM's embedding API accepts a list of inputs, so this overrides the default sequential implementation to reduce the number of requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
The texts to embed. |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
A list of embedding vectors, one per input text, in the same order |
list[list[float]]
|
as |
Raises:
| Type | Description |
|---|---|
EmbeddingProviderError
|
If |