Skip to content

Commit

Permalink
langchain[minor]: Add async methods to EncoderBackedStore (langchain-…
Browse files Browse the repository at this point in the history
…ai#19597)

Co-authored-by: Eugene Yurtsev <[email protected]>
  • Loading branch information
cbornet and eyurtsev authored Mar 27, 2024
1 parent 929ed65 commit 9954c6a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions libs/langchain/langchain/storage/encoder_backed.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import (
Any,
AsyncIterator,
Callable,
Iterator,
List,
Expand Down Expand Up @@ -73,6 +74,15 @@ def mget(self, keys: Sequence[K]) -> List[Optional[V]]:
for value in values
]

async def amget(self, keys: Sequence[K]) -> List[Optional[V]]:
"""Get the values associated with the given keys."""
encoded_keys: List[str] = [self.key_encoder(key) for key in keys]
values = await self.store.amget(encoded_keys)
return [
self.value_deserializer(value) if value is not None else value
for value in values
]

def mset(self, key_value_pairs: Sequence[Tuple[K, V]]) -> None:
"""Set the values for the given keys."""
encoded_pairs = [
Expand All @@ -81,15 +91,37 @@ def mset(self, key_value_pairs: Sequence[Tuple[K, V]]) -> None:
]
self.store.mset(encoded_pairs)

async def amset(self, key_value_pairs: Sequence[Tuple[K, V]]) -> None:
"""Set the values for the given keys."""
encoded_pairs = [
(self.key_encoder(key), self.value_serializer(value))
for key, value in key_value_pairs
]
await self.store.amset(encoded_pairs)

def mdelete(self, keys: Sequence[K]) -> None:
"""Delete the given keys and their associated values."""
encoded_keys = [self.key_encoder(key) for key in keys]
self.store.mdelete(encoded_keys)

async def amdelete(self, keys: Sequence[K]) -> None:
"""Delete the given keys and their associated values."""
encoded_keys = [self.key_encoder(key) for key in keys]
await self.store.amdelete(encoded_keys)

def yield_keys(
self, *, prefix: Optional[str] = None
) -> Union[Iterator[K], Iterator[str]]:
"""Get an iterator over keys that match the given prefix."""
# For the time being this does not return K, but str
# it's for debugging purposes. Should fix this.
yield from self.store.yield_keys(prefix=prefix)

async def ayield_keys(
self, *, prefix: Optional[str] = None
) -> Union[AsyncIterator[K], AsyncIterator[str]]:
"""Get an iterator over keys that match the given prefix."""
# For the time being this does not return K, but str
# it's for debugging purposes. Should fix this.
async for key in self.store.ayield_keys(prefix=prefix):
yield key

0 comments on commit 9954c6a

Please sign in to comment.