LocalFileStore#

class langchain.storage.file_system.LocalFileStore(root_path: str | Path, *, chmod_file: int | None = None, chmod_dir: int | None = None, update_atime: bool = False)[source]#

BaseStore interface that works on the local file system.

Examples

Create a LocalFileStore instance and perform operations on it:

from langchain.storage import LocalFileStore

# Instantiate the LocalFileStore with the root path
file_store = LocalFileStore("/path/to/root")

# Set values for keys
file_store.mset([("key1", b"value1"), ("key2", b"value2")])

# Get values for keys
values = file_store.mget(["key1", "key2"])  # Returns [b"value1", b"value2"]

# Delete keys
file_store.mdelete(["key1"])

# Iterate over keys
for key in file_store.yield_keys():
    print(key)  # noqa: T201

Implement the BaseStore interface for the local file system.

Parameters:
  • root_path (Union[str, Path]) – The root path of the file store. All keys are interpreted as paths relative to this root.

  • chmod_file (int | None) – (optional, defaults to None) If specified, sets permissions for newly created files, overriding the current umask if needed.

  • chmod_dir (int | None) – (optional, defaults to None) If specified, sets permissions for newly created dirs, overriding the current umask if needed.

  • update_atime (bool) – (optional, defaults to False) If True, updates the filesystem access time (but not the modified time) when a file is read. This allows MRU/LRU cache policies to be implemented for filesystems where access time updates are disabled.

Methods

__init__(root_path,Β *[,Β chmod_file,Β ...])

Implement the BaseStore interface for the local file system.

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.

__init__(root_path: str | Path, *, chmod_file: int | None = None, chmod_dir: int | None = None, update_atime: bool = False) β†’ None[source]#

Implement the BaseStore interface for the local file system.

Parameters:
  • root_path (Union[str, Path]) – The root path of the file store. All keys are interpreted as paths relative to this root.

  • chmod_file (int | None) – (optional, defaults to None) If specified, sets permissions for newly created files, overriding the current umask if needed.

  • chmod_dir (int | None) – (optional, defaults to None) If specified, sets permissions for newly created dirs, overriding the current umask if needed.

  • update_atime (bool) – (optional, defaults to False) If True, updates the filesystem access time (but not the modified time) when a file is read. This allows MRU/LRU cache policies to be implemented for filesystems where access time updates are disabled.

Return type:

None

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

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]#

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#

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]#

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]

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

Delete the given keys and their associated values.

Parameters:

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

Returns:

None

Return type:

None

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

Get the values associated with the given keys.

Parameters:

keys (Sequence[str]) – 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[bytes | None]

mset(key_value_pairs: Sequence[Tuple[str, bytes]]) β†’ None[source]#

Set the values for the given keys.

Parameters:

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

Returns:

None

Return type:

None

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

Get an iterator over keys that match the given prefix.

Parameters:

prefix (Optional[str]) – The prefix to match.

Returns:

An iterator over keys that match the given prefix.

Return type:

Iterator[str]

Examples using LocalFileStore