Home / File/ caches.py — langchain Source File

caches.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  e41c20be_5cd4_2191_1344_c6fbd9700956["caches.py"]
  cccbe73e_4644_7211_4d55_e8fb133a8014["abc"]
  e41c20be_5cd4_2191_1344_c6fbd9700956 --> cccbe73e_4644_7211_4d55_e8fb133a8014
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  e41c20be_5cd4_2191_1344_c6fbd9700956 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  e41c20be_5cd4_2191_1344_c6fbd9700956 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  e41c20be_5cd4_2191_1344_c6fbd9700956 --> 91721f45_4909_e489_8c1f_084f8bd87145
  ac2a9b92_4484_491e_1b48_ec85e71e1d58["langchain_core.outputs"]
  e41c20be_5cd4_2191_1344_c6fbd9700956 --> ac2a9b92_4484_491e_1b48_ec85e71e1d58
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  e41c20be_5cd4_2191_1344_c6fbd9700956 --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  style e41c20be_5cd4_2191_1344_c6fbd9700956 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Optional caching layer for language models.

Distinct from provider-based [prompt caching](https://docs.langchain.com/oss/python/langchain/models#prompt-caching).

!!! warning "Beta feature"

    This is a beta feature. Please be wary of deploying experimental code to production
    unless you've taken appropriate precautions.

A cache is useful for two reasons:

1. It can save you money by reducing the number of API calls you make to the LLM
    provider if you're often requesting the same completion multiple times.
2. It can speed up your application by reducing the number of API calls you make to the
    LLM provider.
"""

from __future__ import annotations

from abc import ABC, abstractmethod
from collections.abc import Sequence
from typing import Any

from typing_extensions import override

from langchain_core.outputs import Generation
from langchain_core.runnables import run_in_executor

RETURN_VAL_TYPE = Sequence[Generation]


class BaseCache(ABC):
    """Interface for a caching layer for LLMs and Chat models.

    The cache interface consists of the following methods:

    - lookup: Look up a value based on a prompt and `llm_string`.
    - update: Update the cache based on a prompt and `llm_string`.
    - clear: Clear the cache.

    In addition, the cache interface provides an async version of each method.

    The default implementation of the async methods is to run the synchronous
    method in an executor. It's recommended to override the async methods
    and provide async implementations to avoid unnecessary overhead.
    """

    @abstractmethod
    def lookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None:
        """Look up based on `prompt` and `llm_string`.

        A cache implementation is expected to generate a key from the 2-tuple
        of `prompt` and `llm_string` (e.g., by concatenating them with a delimiter).

        Args:
            prompt: A string representation of the prompt.

                In the case of a chat model, the prompt is a non-trivial
                serialization of the prompt into the language model.
            llm_string: A string representation of the LLM configuration.
// ... (213 more lines)

Subdomains

Dependencies

  • abc
  • collections.abc
  • langchain_core.outputs
  • langchain_core.runnables
  • typing
  • typing_extensions

Frequently Asked Questions

What does caches.py do?
caches.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What does caches.py depend on?
caches.py imports 6 module(s): abc, collections.abc, langchain_core.outputs, langchain_core.runnables, typing, typing_extensions.
Where is caches.py in the architecture?
caches.py is located at libs/core/langchain_core/caches.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/core/langchain_core).

Analyze Your Own Codebase

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

Try Supermodel Free