CacheBackedEmbeddings#

class langchain.embeddings.cache.CacheBackedEmbeddings(underlying_embeddings: Embeddings, document_embedding_store: BaseStore[str, List[float]], *, batch_size: int | None = None, query_embedding_store: BaseStore[str, List[float]] | None = None)[source]#

Interface for caching results from embedding models.

The interface allows works with any store that implements the abstract store interface accepting keys of type str and values of list of floats.

If need be, the interface can be extended to accept other implementations of the value serializer and deserializer, as well as the key encoder.

Note that by default only document embeddings are cached. To cache query embeddings too, pass in a query_embedding_store to constructor.

Examples

Initialize the embedder.

Parameters:
  • underlying_embeddings (Embeddings) – the embedder to use for computing embeddings.

  • document_embedding_store (BaseStore[str, List[float]]) – The store to use for caching document embeddings.

  • batch_size (Optional[int]) – The number of documents to embed between store updates.

  • query_embedding_store (Optional[BaseStore[str, List[float]]]) – The store to use for caching query embeddings. If None, query embeddings are not cached.

Methods

__init__(underlying_embeddings, ...[, ...])

Initialize the embedder.

aembed_documents(texts)

Embed a list of texts.

aembed_query(text)

Embed query text.

embed_documents(texts)

Embed a list of texts.

embed_query(text)

Embed query text.

from_bytes_store(underlying_embeddings, ...)

On-ramp that adds the necessary serialization and encoding to the store.

__init__(underlying_embeddings: Embeddings, document_embedding_store: BaseStore[str, List[float]], *, batch_size: int | None = None, query_embedding_store: BaseStore[str, List[float]] | None = None) None[source]#

Initialize the embedder.

Parameters:
  • underlying_embeddings (Embeddings) – the embedder to use for computing embeddings.

  • document_embedding_store (BaseStore[str, List[float]]) – The store to use for caching document embeddings.

  • batch_size (int | None) – The number of documents to embed between store updates.

  • query_embedding_store (BaseStore[str, List[float]] | None) – The store to use for caching query embeddings. If None, query embeddings are not cached.

Return type:

None

async aembed_documents(texts: List[str]) List[List[float]][source]#

Embed a list of texts.

The method first checks the cache for the embeddings. If the embeddings are not found, the method uses the underlying embedder to embed the documents and stores the results in the cache.

Parameters:

texts (List[str]) – A list of texts to embed.

Returns:

A list of embeddings for the given texts.

Return type:

List[List[float]]

async aembed_query(text: str) List[float][source]#

Embed query text.

By default, this method does not cache queries. To enable caching, set the cache_query parameter to True when initializing the embedder.

Parameters:

text (str) – The text to embed.

Returns:

The embedding for the given text.

Return type:

List[float]

embed_documents(texts: List[str]) List[List[float]][source]#

Embed a list of texts.

The method first checks the cache for the embeddings. If the embeddings are not found, the method uses the underlying embedder to embed the documents and stores the results in the cache.

Parameters:

texts (List[str]) – A list of texts to embed.

Returns:

A list of embeddings for the given texts.

Return type:

List[List[float]]

embed_query(text: str) List[float][source]#

Embed query text.

By default, this method does not cache queries. To enable caching, set the cache_query parameter to True when initializing the embedder.

Parameters:

text (str) – The text to embed.

Returns:

The embedding for the given text.

Return type:

List[float]

classmethod from_bytes_store(underlying_embeddings: Embeddings, document_embedding_cache: BaseStore[str, bytes], *, namespace: str = '', batch_size: int | None = None, query_embedding_cache: bool | BaseStore[str, bytes] = False) CacheBackedEmbeddings[source]#

On-ramp that adds the necessary serialization and encoding to the store.

Parameters:
  • underlying_embeddings (Embeddings) – The embedder to use for embedding.

  • document_embedding_cache (BaseStore[str, bytes]) – The cache to use for storing document embeddings.

  • *

  • namespace (str) –

  • batch_size (int | None) –

  • query_embedding_cache (bool | BaseStore[str, bytes]) –

Return type:

CacheBackedEmbeddings

:param : :param namespace: The namespace to use for document cache.

This namespace is used to avoid collisions with other caches. For example, set it to the name of the embedding model used.

Parameters:
  • batch_size (int | None) – The number of documents to embed between store updates.

  • query_embedding_cache (bool | BaseStore[str, bytes]) – The cache to use for storing query embeddings. True to use the same cache as document embeddings. False to not cache query embeddings.

  • underlying_embeddings (Embeddings) –

  • document_embedding_cache (BaseStore[str, bytes]) –

  • namespace (str) –

Return type:

CacheBackedEmbeddings

Examples using CacheBackedEmbeddings