02. CacheBackedEmbeddings
Embeddings can be stored or temporarily cached to avoid recalculation.
Caching Embeddings CacheBackedEmbeddings Can be done using Cache support embedder is a wrapper around the embedder caching embeddings to the key-value repository. The text is hashed and this hash is used as a key in the cache.
CacheBackedEmbeddings The main support method for initializing from_bytes_store is. This receives the following parameters:
underlying_embeddings: Embedder used for embedding.document_embedding_cache: To cache document embeddingByteStoreOne of.namespace: (Optional, default"") Namespace used for document cache. This namespace is used to avoid collisions with other caches. For example, set the name of the embedding model used.
caution : To avoid collisions when the same text is embedded using different embedding models namespace It is important to set parameters.
Embedding in LocalFileStore (permanent storage)
First, let's take a look at the example of using a local file system to store embedding and using the FAISS vector store.
Copy
from langchain.storage import LocalFileStore
from langchain_openai import OpenAIEmbeddings
from langchain.embeddings import CacheBackedEmbeddings
# OpenAI Setting up default embeddings using embeddings
embedding = OpenAIEmbeddings()
# Setting up local file storage
store = LocalFileStore("./cache/")
# Generating embeddings that support cache
cached_embedder = CacheBackedEmbeddings.from_bytes_store(
underlying_embeddings=embedding,
document_embedding_cache=store,
namespace=embedding.model, # Generate cache-enabled embeddings using base embeddings and storage
)Copy
Copy
Load documents, divide them into chunks, embed each chunk and load them into vector repositories.
Copy
Copy
Copy
When I try to re-create the vector repository, it is processed much faster because I don't need to recalculate the embedding.
Copy
Copy
InmemoryByteStore Use (non-permanent)
InmemoryByteStore Use (non-permanent)Different ByteStore To use CacheBackedEmbeddings When generating ByteStore If you use.
Below, it is non-permanent InMemoryByteStore Shows an example that uses to create the same cached embedding object.
Copy
Last updated