Home / Class/ ConversationSummaryMemory Class — langchain Architecture

ConversationSummaryMemory Class — langchain Architecture

Architecture documentation for the ConversationSummaryMemory class in summary.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  896d889a_d228_6c16_6cdd_5799f6e5f307["ConversationSummaryMemory"]
  48fb025a_9570_1ddd_d527_ec72c1ae6a6f["BaseChatMemory"]
  896d889a_d228_6c16_6cdd_5799f6e5f307 -->|extends| 48fb025a_9570_1ddd_d527_ec72c1ae6a6f
  b59972c7_2658_48b0_f600_7b1f592481af["SummarizerMixin"]
  896d889a_d228_6c16_6cdd_5799f6e5f307 -->|extends| b59972c7_2658_48b0_f600_7b1f592481af
  3460fa37_4fa8_1998_2c3c_01d1d3814b73["summary.py"]
  896d889a_d228_6c16_6cdd_5799f6e5f307 -->|defined in| 3460fa37_4fa8_1998_2c3c_01d1d3814b73
  1f012672_a4f9_fcdb_ebce_4907d85c70cf["from_messages()"]
  896d889a_d228_6c16_6cdd_5799f6e5f307 -->|method| 1f012672_a4f9_fcdb_ebce_4907d85c70cf
  d29d0078_0b52_b72c_2fa6_716dc6730fc8["memory_variables()"]
  896d889a_d228_6c16_6cdd_5799f6e5f307 -->|method| d29d0078_0b52_b72c_2fa6_716dc6730fc8
  17276d55_ac4c_a701_5da2_da1fe7791643["load_memory_variables()"]
  896d889a_d228_6c16_6cdd_5799f6e5f307 -->|method| 17276d55_ac4c_a701_5da2_da1fe7791643
  dfcad673_e3be_4c35_cabe_c563aa9e1ab2["validate_prompt_input_variables()"]
  896d889a_d228_6c16_6cdd_5799f6e5f307 -->|method| dfcad673_e3be_4c35_cabe_c563aa9e1ab2
  a3263faf_0e13_7f5d_2353_c3f26cefc183["save_context()"]
  896d889a_d228_6c16_6cdd_5799f6e5f307 -->|method| a3263faf_0e13_7f5d_2353_c3f26cefc183
  79074a26_98af_82c7_6abb_086ab544fb81["clear()"]
  896d889a_d228_6c16_6cdd_5799f6e5f307 -->|method| 79074a26_98af_82c7_6abb_086ab544fb81

Relationship Graph

Source Code

libs/langchain/langchain_classic/memory/summary.py lines 91–168

class ConversationSummaryMemory(BaseChatMemory, SummarizerMixin):
    """Continually summarizes the conversation history.

    The summary is updated after each conversation turn.
    The implementations returns a summary of the conversation history which
    can be used to provide context to the model.
    """

    buffer: str = ""
    memory_key: str = "history"

    @classmethod
    def from_messages(
        cls,
        llm: BaseLanguageModel,
        chat_memory: BaseChatMessageHistory,
        *,
        summarize_step: int = 2,
        **kwargs: Any,
    ) -> ConversationSummaryMemory:
        """Create a ConversationSummaryMemory from a list of messages.

        Args:
            llm: The language model to use for summarization.
            chat_memory: The chat history to summarize.
            summarize_step: Number of messages to summarize at a time.
            **kwargs: Additional keyword arguments to pass to the class.

        Returns:
            An instance of ConversationSummaryMemory with the summarized history.
        """
        obj = cls(llm=llm, chat_memory=chat_memory, **kwargs)
        for i in range(0, len(obj.chat_memory.messages), summarize_step):
            obj.buffer = obj.predict_new_summary(
                obj.chat_memory.messages[i : i + summarize_step],
                obj.buffer,
            )
        return obj

    @property
    def memory_variables(self) -> list[str]:
        """Will always return list of memory variables."""
        return [self.memory_key]

    @override
    def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, Any]:
        """Return history buffer."""
        if self.return_messages:
            buffer: Any = [self.summary_message_cls(content=self.buffer)]
        else:
            buffer = self.buffer
        return {self.memory_key: buffer}

    @pre_init
    def validate_prompt_input_variables(cls, values: dict) -> dict:
        """Validate that prompt input variables are consistent."""
        prompt_variables = values["prompt"].input_variables
        expected_keys = {"summary", "new_lines"}
        if expected_keys != set(prompt_variables):
            msg = (
                "Got unexpected prompt input variables. The prompt expects "
                f"{prompt_variables}, but it should have {expected_keys}."
            )
            raise ValueError(msg)
        return values

    def save_context(self, inputs: dict[str, Any], outputs: dict[str, str]) -> None:
        """Save context from this conversation to buffer."""
        super().save_context(inputs, outputs)
        self.buffer = self.predict_new_summary(
            self.chat_memory.messages[-2:],
            self.buffer,
        )

    def clear(self) -> None:
        """Clear memory contents."""
        super().clear()
        self.buffer = ""

Frequently Asked Questions

What is the ConversationSummaryMemory class?
ConversationSummaryMemory is a class in the langchain codebase, defined in libs/langchain/langchain_classic/memory/summary.py.
Where is ConversationSummaryMemory defined?
ConversationSummaryMemory is defined in libs/langchain/langchain_classic/memory/summary.py at line 91.
What does ConversationSummaryMemory extend?
ConversationSummaryMemory extends BaseChatMemory, SummarizerMixin.

Analyze Your Own Codebase

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

Try Supermodel Free