EncoderBackedStore#

class langchain.storage.encoder_backed.EncoderBackedStore(store: BaseStore[str, Any], key_encoder: Callable[[K], str], value_serializer: Callable[[V], bytes], value_deserializer: Callable[[Any], V])[source]#

Wraps a store with key and value encoders/decoders.

Examples that uses JSON for encoding/decoding:

import json

def key_encoder(key: int) -> str:
    return json.dumps(key)

def value_serializer(value: float) -> str:
    return json.dumps(value)

def value_deserializer(serialized_value: str) -> float:
    return json.loads(serialized_value)

# Create an instance of the abstract store
abstract_store = MyCustomStore()

# Create an instance of the encoder-backed store
store = EncoderBackedStore(
    store=abstract_store,
    key_encoder=key_encoder,
    value_serializer=value_serializer,
    value_deserializer=value_deserializer
)

# Use the encoder-backed store methods
store.mset([(1, 3.14), (2, 2.718)])
values = store.mget([1, 2])  # Retrieves [3.14, 2.718]
store.mdelete([1, 2])  # Deletes the keys 1 and 2

Initialize an EncodedStore.

Methods

__init__(store,Β key_encoder,Β ...)

Initialize an EncodedStore.

amdelete(keys)

Delete the given keys and their associated values.

amget(keys)

Get the values associated with the given keys.

amset(key_value_pairs)

Set the values for the given keys.

ayield_keys(*[,Β prefix])

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.

Parameters:
  • store (BaseStore[str, Any]) –

  • key_encoder (Callable[[K], str]) –

  • value_serializer (Callable[[V], bytes]) –

  • value_deserializer (Callable[[Any], V]) –

__init__(store: BaseStore[str, Any], key_encoder: Callable[[K], str], value_serializer: Callable[[V], bytes], value_deserializer: Callable[[Any], V]) β†’ None[source]#

Initialize an EncodedStore.

Parameters:
  • store (BaseStore[str, Any]) –

  • key_encoder (Callable[[K], str]) –

  • value_serializer (Callable[[V], bytes]) –

  • value_deserializer (Callable[[Any], V]) –

Return type:

None

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

Delete the given keys and their associated values.

Parameters:

keys (Sequence[K]) –

Return type:

None

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

Get the values associated with the given keys.

Parameters:

keys (Sequence[K]) –

Return type:

List[V | None]

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

Set the values for the given keys.

Parameters:

key_value_pairs (Sequence[Tuple[K, V]]) –

Return type:

None

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

Get an iterator over keys that match the given prefix.

Parameters:

prefix (str | None) –

Return type:

AsyncIterator[K] | AsyncIterator[str]

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

Delete the given keys and their associated values.

Parameters:

keys (Sequence[K]) –

Return type:

None

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

Get the values associated with the given keys.

Parameters:

keys (Sequence[K]) –

Return type:

List[V | None]

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]]) –

Return type:

None

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

Get an iterator over keys that match the given prefix.

Parameters:

prefix (str | None) –

Return type:

Iterator[K] | Iterator[str]