BaseStore#

class langchain_core.stores.BaseStore[source]#

Abstract interface for a key-value store.

This is an interface that’s meant to abstract away the details of different key-value stores. It provides a simple interface for getting, setting, and deleting key-value pairs.

The basic methods are mget, mset, and mdelete for getting, setting, and deleting multiple key-value pairs at once. The yield_keys method is used to iterate over keys that match a given prefix.

The async versions of these methods are also provided, which are meant to be used in async contexts. The async methods are named with an a prefix, e.g., amget, amset, amdelete, and ayield_keys.

By default, the amget, amset, amdelete, and ayield_keys methods are implemented using the synchronous methods. If the store can natively support async operations, it should override these methods.

By design the methods only accept batches of keys and values, and not single keys or values. This is done to force user code to work with batches which will usually be more efficient by saving on round trips to the store.

Examples

from langchain.storage import BaseStore

class MyInMemoryStore(BaseStore[str, int]):

    def __init__(self):
        self.store = {}

    def mget(self, keys):
        return [self.store.get(key) for key in keys]

    def mset(self, key_value_pairs):
        for key, value in key_value_pairs:
            self.store[key] = value

    def mdelete(self, keys):
        for key in keys:
            if key in self.store:
                del self.store[key]

    def yield_keys(self, prefix=None):
        if prefix is None:
            yield from self.store.keys()
        else:
            for key in self.store.keys():
                if key.startswith(prefix):
                    yield key

Methods

amdelete(keys)

Async delete the given keys and their associated values.

amget(keys)

Async get the values associated with the given keys.

amset(key_value_pairs)

Async set the values for the given keys.

ayield_keys(*[,Β prefix])

Async get an iterator over keys that match the given prefix.

mdelete(keys)

Delete the given keys and their associated values.

mget(keys)

Get the values associated with the given keys.

mset(key_value_pairs)

Set the values for the given keys.

yield_keys(*[,Β prefix])

Get an iterator over keys that match the given prefix.

async amdelete(keys: Sequence[K]) β†’ None[source]#

Async delete the given keys and their associated values.

Parameters:

keys (Sequence[K]) – A sequence of keys to delete.

Return type:

None

async amget(keys: Sequence[K]) β†’ list[V | None][source]#

Async get the values associated with the given keys.

Parameters:

keys (Sequence[K]) – A sequence of keys.

Returns:

A sequence of optional values associated with the keys. If a key is not found, the corresponding value will be None.

Return type:

list[V | None]

async amset(key_value_pairs: Sequence[tuple[K, V]]) β†’ None[source]#

Async set the values for the given keys.

Parameters:

key_value_pairs (Sequence[Tuple[K, V]]) – A sequence of key-value pairs.

Return type:

None

async ayield_keys(*, prefix: str | None = None) β†’ AsyncIterator[K] | AsyncIterator[str][source]#

Async get an iterator over keys that match the given prefix.

Parameters:

prefix (str) – The prefix to match.

Yields:

Iterator[K | str] – An iterator over keys that match the given prefix. This method is allowed to return an iterator over either K or str depending on what makes more sense for the given store.

Return type:

AsyncIterator[K] | AsyncIterator[str]

abstract mdelete(keys: Sequence[K]) β†’ None[source]#

Delete the given keys and their associated values.

Parameters:

keys (Sequence[K]) – A sequence of keys to delete.

Return type:

None

abstract mget(keys: Sequence[K]) β†’ list[V | None][source]#

Get the values associated with the given keys.

Parameters:

keys (Sequence[K]) – A sequence of keys.

Returns:

A sequence of optional values associated with the keys. If a key is not found, the corresponding value will be None.

Return type:

list[V | None]

abstract mset(key_value_pairs: Sequence[tuple[K, V]]) β†’ None[source]#

Set the values for the given keys.

Parameters:

key_value_pairs (Sequence[Tuple[K, V]]) – A sequence of key-value pairs.

Return type:

None

abstract yield_keys(*, prefix: str | None = None) β†’ Iterator[K] | Iterator[str][source]#

Get an iterator over keys that match the given prefix.

Parameters:

prefix (str) – The prefix to match.

Yields:

Iterator[K | str] – An iterator over keys that match the given prefix. This method is allowed to return an iterator over either K or str depending on what makes more sense for the given store.

Return type:

Iterator[K] | Iterator[str]

Examples using BaseStore