Home / Function/ _make_default_key_encoder() — langchain Function Reference

_make_default_key_encoder() — langchain Function Reference

Architecture documentation for the _make_default_key_encoder() function in cache.py from the langchain codebase.

Function python LangChainCore Runnables calls 2 called by 1

Entity Profile

Dependency Diagram

graph TD
  4b1f75e8_3a36_4d2e_f5d9_04dbd0255a60["_make_default_key_encoder()"]
  43da8c3d_b3f8_9614_dc29_d2a6402cffbd["cache.py"]
  4b1f75e8_3a36_4d2e_f5d9_04dbd0255a60 -->|defined in| 43da8c3d_b3f8_9614_dc29_d2a6402cffbd
  fc5a90e3_3529_5688_86a8_34cee618454e["from_bytes_store()"]
  fc5a90e3_3529_5688_86a8_34cee618454e -->|calls| 4b1f75e8_3a36_4d2e_f5d9_04dbd0255a60
  2f5d18b0_8768_802d_369c_3d55a4392881["_warn_about_sha1_encoder()"]
  4b1f75e8_3a36_4d2e_f5d9_04dbd0255a60 -->|calls| 2f5d18b0_8768_802d_369c_3d55a4392881
  99053033_7939_d7bd_a0d9_537c144d34a0["_sha1_hash_to_uuid()"]
  4b1f75e8_3a36_4d2e_f5d9_04dbd0255a60 -->|calls| 99053033_7939_d7bd_a0d9_537c144d34a0
  style 4b1f75e8_3a36_4d2e_f5d9_04dbd0255a60 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/embeddings/cache.py lines 43–73

def _make_default_key_encoder(namespace: str, algorithm: str) -> Callable[[str], str]:
    """Create a default key encoder function.

    Args:
        namespace: Prefix that segregates keys from different embedding models.
        algorithm:
           * `'sha1'` - fast but not collision-resistant
           * `'blake2b'` - cryptographically strong, faster than SHA-1
           * `'sha256'` - cryptographically strong, slower than SHA-1
           * `'sha512'` - cryptographically strong, slower than SHA-1

    Returns:
        A function that encodes a key using the specified algorithm.
    """
    if algorithm == "sha1":
        _warn_about_sha1_encoder()

    def _key_encoder(key: str) -> str:
        """Encode a key using the specified algorithm."""
        if algorithm == "sha1":
            return f"{namespace}{_sha1_hash_to_uuid(key)}"
        if algorithm == "blake2b":
            return f"{namespace}{hashlib.blake2b(key.encode('utf-8')).hexdigest()}"
        if algorithm == "sha256":
            return f"{namespace}{hashlib.sha256(key.encode('utf-8')).hexdigest()}"
        if algorithm == "sha512":
            return f"{namespace}{hashlib.sha512(key.encode('utf-8')).hexdigest()}"
        msg = f"Unsupported algorithm: {algorithm}"
        raise ValueError(msg)

    return _key_encoder

Domain

Subdomains

Called By

Frequently Asked Questions

What does _make_default_key_encoder() do?
_make_default_key_encoder() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/embeddings/cache.py.
Where is _make_default_key_encoder() defined?
_make_default_key_encoder() is defined in libs/langchain/langchain_classic/embeddings/cache.py at line 43.
What does _make_default_key_encoder() call?
_make_default_key_encoder() calls 2 function(s): _sha1_hash_to_uuid, _warn_about_sha1_encoder.
What calls _make_default_key_encoder()?
_make_default_key_encoder() is called by 1 function(s): from_bytes_store.

Analyze Your Own Codebase

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

Try Supermodel Free