Key-value stores
Overviewβ
LangChain provides a key-value store interface for storing and retrieving data.
LangChain includes a BaseStore
interface,
which allows for storage of arbitrary data. However, LangChain components that require KV-storage accept a
more specific BaseStore<string, Uint8Array>
instance that stores binary data (referred to as a ByteStore
), and internally take care of
encoding and decoding data for their specific needs.
This means that as a user, you only need to think about one type of store rather than different ones for different types of data.
Usageβ
The key-value store interface in LangChain is used primarily for:
Caching embeddings via CachedBackedEmbeddings to avoid recomputing embeddings for repeated queries or when re-indexing content.
As a simple Document persistence layer in some retrievers.
Please see these how-to guides for more information:
Interfaceβ
All BaseStore
s support the following interface. Note that the interface allows for modifying multiple key-value pairs at once:
mget(keys: string[]): Promise<(Uint8Array | undefined)[]>
: get the contents of multiple keys, returningundefined
if the key does not existmset(keyValuePairs: [string, Uint8Array][]): Promise<void>
: set the contents of multiple keysmdelete(keys: string[]): Promise<void>
: delete multiple keysyieldKeys(prefix?: string): AsyncIterator<string>
: yield all keys in the store, optionally filtering by a prefix
Integrationsβ
Please reference the stores integration page for a list of available key-value store integrations.