Home / File/ _lc_store.py — langchain Source File

_lc_store.py — langchain Source File

Architecture documentation for _lc_store.py, a python file in the langchain codebase. 6 imports, 0 dependents.

File python LangChainCore ApiManagement 6 imports 7 functions

Entity Profile

Dependency Diagram

graph LR
  b62a282c_e004_7e47_33d5_aaacd87a9220["_lc_store.py"]
  2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"]
  b62a282c_e004_7e47_33d5_aaacd87a9220 --> 2bf6d401_816d_d011_3b05_a6114f55ff58
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  b62a282c_e004_7e47_33d5_aaacd87a9220 --> feec1ec4_6917_867b_d228_b134d0ff8099
  6a98b0a5_5607_0043_2e22_a46a464c2d62["langchain_core.documents"]
  b62a282c_e004_7e47_33d5_aaacd87a9220 --> 6a98b0a5_5607_0043_2e22_a46a464c2d62
  86d015d7_8a78_acc2_abd6_9cb22b0ae1aa["langchain_core.load"]
  b62a282c_e004_7e47_33d5_aaacd87a9220 --> 86d015d7_8a78_acc2_abd6_9cb22b0ae1aa
  18bf18ce_a804_12d4_efe2_700bc6c22630["langchain_core.stores"]
  b62a282c_e004_7e47_33d5_aaacd87a9220 --> 18bf18ce_a804_12d4_efe2_700bc6c22630
  5318c6c3_4741_6333_cafd_8a5d2f3712b0["langchain_classic.storage.encoder_backed"]
  b62a282c_e004_7e47_33d5_aaacd87a9220 --> 5318c6c3_4741_6333_cafd_8a5d2f3712b0
  style b62a282c_e004_7e47_33d5_aaacd87a9220 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Create a key-value store for any langchain serializable object."""

from collections.abc import Callable
from typing import Any

from langchain_core.documents import Document
from langchain_core.load import Serializable, dumps, loads
from langchain_core.stores import BaseStore, ByteStore

from langchain_classic.storage.encoder_backed import EncoderBackedStore


def _dump_as_bytes(obj: Serializable) -> bytes:
    """Return a bytes representation of a `Document`."""
    return dumps(obj).encode("utf-8")


def _dump_document_as_bytes(obj: Any) -> bytes:
    """Return a bytes representation of a `Document`."""
    if not isinstance(obj, Document):
        msg = "Expected a Document instance"
        raise TypeError(msg)
    return dumps(obj).encode("utf-8")


def _load_document_from_bytes(serialized: bytes) -> Document:
    """Return a document from a bytes representation."""
    obj = loads(serialized.decode("utf-8"))
    if not isinstance(obj, Document):
        msg = f"Expected a Document instance. Got {type(obj)}"
        raise TypeError(msg)
    return obj


def _load_from_bytes(serialized: bytes) -> Serializable:
    """Return a document from a bytes representation."""
    return loads(serialized.decode("utf-8"))


def _identity(x: str) -> str:
    """Return the same object."""
    return x


# PUBLIC API


def create_lc_store(
    store: ByteStore,
    *,
    key_encoder: Callable[[str], str] | None = None,
) -> BaseStore[str, Serializable]:
    """Create a store for LangChain serializable objects from a bytes store.

    Args:
        store: A bytes store to use as the underlying store.
        key_encoder: A function to encode keys; if `None` uses identity function.

    Returns:
        A key-value store for `Document` objects.
    """
    return EncoderBackedStore(
        store,
        key_encoder or _identity,
        _dump_as_bytes,
        _load_from_bytes,
    )


def create_kv_docstore(
    store: ByteStore,
    *,
    key_encoder: Callable[[str], str] | None = None,
) -> BaseStore[str, Document]:
    """Create a store for langchain `Document` objects from a bytes store.

    This store does run time type checking to ensure that the values are
    `Document` objects.

    Args:
        store: A bytes store to use as the underlying store.
        key_encoder: A function to encode keys; if `None`, uses identity function.

    Returns:
        A key-value store for `Document` objects.
    """
    return EncoderBackedStore(
        store,
        key_encoder or _identity,
        _dump_document_as_bytes,
        _load_document_from_bytes,
    )

Domain

Subdomains

Dependencies

  • collections.abc
  • langchain_classic.storage.encoder_backed
  • langchain_core.documents
  • langchain_core.load
  • langchain_core.stores
  • typing

Frequently Asked Questions

What does _lc_store.py do?
_lc_store.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, ApiManagement subdomain.
What functions are defined in _lc_store.py?
_lc_store.py defines 7 function(s): _dump_as_bytes, _dump_document_as_bytes, _identity, _load_document_from_bytes, _load_from_bytes, create_kv_docstore, create_lc_store.
What does _lc_store.py depend on?
_lc_store.py imports 6 module(s): collections.abc, langchain_classic.storage.encoder_backed, langchain_core.documents, langchain_core.load, langchain_core.stores, typing.
Where is _lc_store.py in the architecture?
_lc_store.py is located at libs/langchain/langchain_classic/storage/_lc_store.py (domain: LangChainCore, subdomain: ApiManagement, directory: libs/langchain/langchain_classic/storage).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free