Home / File/ combined.py — langchain Source File

combined.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  3f814af4_7da9_9b8f_172b_acdde6f561b9["combined.py"]
  0c635125_6987_b8b3_7ff7_d60249aecde7["warnings"]
  3f814af4_7da9_9b8f_172b_acdde6f561b9 --> 0c635125_6987_b8b3_7ff7_d60249aecde7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  3f814af4_7da9_9b8f_172b_acdde6f561b9 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  3f814af4_7da9_9b8f_172b_acdde6f561b9 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  6bcbe1b6_8195_eae9_29e5_8e80e91eca64["langchain_classic.base_memory"]
  3f814af4_7da9_9b8f_172b_acdde6f561b9 --> 6bcbe1b6_8195_eae9_29e5_8e80e91eca64
  34f6662f_3e50_5bf7_54f1_05e82f591554["langchain_classic.memory.chat_memory"]
  3f814af4_7da9_9b8f_172b_acdde6f561b9 --> 34f6662f_3e50_5bf7_54f1_05e82f591554
  style 3f814af4_7da9_9b8f_172b_acdde6f561b9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import warnings
from typing import Any

from pydantic import field_validator

from langchain_classic.base_memory import BaseMemory
from langchain_classic.memory.chat_memory import BaseChatMemory


class CombinedMemory(BaseMemory):
    """Combining multiple memories' data together."""

    memories: list[BaseMemory]
    """For tracking all the memories that should be accessed."""

    @field_validator("memories")
    @classmethod
    def _check_repeated_memory_variable(
        cls,
        value: list[BaseMemory],
    ) -> list[BaseMemory]:
        all_variables: set[str] = set()
        for val in value:
            overlap = all_variables.intersection(val.memory_variables)
            if overlap:
                msg = (
                    f"The same variables {overlap} are found in multiple"
                    "memory object, which is not allowed by CombinedMemory."
                )
                raise ValueError(msg)
            all_variables |= set(val.memory_variables)

        return value

    @field_validator("memories")
    @classmethod
    def check_input_key(cls, value: list[BaseMemory]) -> list[BaseMemory]:
        """Check that if memories are of type BaseChatMemory that input keys exist."""
        for val in value:
            if isinstance(val, BaseChatMemory) and val.input_key is None:
                warnings.warn(
                    "When using CombinedMemory, "
                    "input keys should be so the input is known. "
                    f" Was not set on {val}",
                    stacklevel=5,
                )
        return value

    @property
    def memory_variables(self) -> list[str]:
        """All the memory variables that this instance provides."""
        """Collected from the all the linked memories."""

        memory_variables = []

        for memory in self.memories:
            memory_variables.extend(memory.memory_variables)

        return memory_variables

    def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, str]:
        """Load all vars from sub-memories."""
        memory_data: dict[str, Any] = {}

        # Collect vars from all sub-memories
        for memory in self.memories:
            data = memory.load_memory_variables(inputs)
            for key, value in data.items():
                if key in memory_data:
                    msg = f"The variable {key} is repeated in the CombinedMemory."
                    raise ValueError(msg)
                memory_data[key] = value

        return memory_data

    def save_context(self, inputs: dict[str, Any], outputs: dict[str, str]) -> None:
        """Save context from this session for every memory."""
        # Save context for all sub-memories
        for memory in self.memories:
            memory.save_context(inputs, outputs)

    def clear(self) -> None:
        """Clear context from this session for every memory."""
        for memory in self.memories:
            memory.clear()

Subdomains

Classes

Dependencies

  • langchain_classic.base_memory
  • langchain_classic.memory.chat_memory
  • pydantic
  • typing
  • warnings

Frequently Asked Questions

What does combined.py do?
combined.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What does combined.py depend on?
combined.py imports 5 module(s): langchain_classic.base_memory, langchain_classic.memory.chat_memory, pydantic, typing, warnings.
Where is combined.py in the architecture?
combined.py is located at libs/langchain/langchain_classic/memory/combined.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/langchain/langchain_classic/memory).

Analyze Your Own Codebase

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

Try Supermodel Free