langchain.storage.encoder_backed.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.

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.

__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.

mdelete(keys: Sequence[K]) None[source]¶

Delete the given keys and their associated values.

mget(keys: Sequence[K]) List[Optional[V]][source]¶

Get the values associated with the given keys.

mset(key_value_pairs: Sequence[Tuple[K, V]]) None[source]¶

Set the values for the given keys.

yield_keys(*, prefix: Optional[str] = None) Union[Iterator[K], Iterator[str]][source]¶

Get an iterator over keys that match the given prefix.