Home / Function/ save_context() — langchain Function Reference

save_context() — langchain Function Reference

Architecture documentation for the save_context() function in entity.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  f048cf72_7d11_089f_89e3_f73dbf79fd42["save_context()"]
  f6985512_c22e_0ae9_c629_acb233df5408["ConversationEntityMemory"]
  f048cf72_7d11_089f_89e3_f73dbf79fd42 -->|defined in| f6985512_c22e_0ae9_c629_acb233df5408
  6560dbd6_8588_9d8f_d467_ab24a097ad9c["get()"]
  f048cf72_7d11_089f_89e3_f73dbf79fd42 -->|calls| 6560dbd6_8588_9d8f_d467_ab24a097ad9c
  b8645598_170e_3bc8_8508_c397ac0ae837["set()"]
  f048cf72_7d11_089f_89e3_f73dbf79fd42 -->|calls| b8645598_170e_3bc8_8508_c397ac0ae837
  style f048cf72_7d11_089f_89e3_f73dbf79fd42 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/memory/entity.py lines 567–605

    def save_context(self, inputs: dict[str, Any], outputs: dict[str, str]) -> None:
        """Save context from this conversation history to the entity store.

        Generates a summary for each entity in the entity cache by prompting
        the model, and saves these summaries to the entity store.
        """
        super().save_context(inputs, outputs)

        if self.input_key is None:
            prompt_input_key = get_prompt_input_key(inputs, self.memory_variables)
        else:
            prompt_input_key = self.input_key

        # Extract an arbitrary window of the last message pairs from
        # the chat history, where the hyperparameter k is the
        # number of message pairs:
        buffer_string = get_buffer_string(
            self.buffer[-self.k * 2 :],
            human_prefix=self.human_prefix,
            ai_prefix=self.ai_prefix,
        )

        input_data = inputs[prompt_input_key]

        # Create an LLMChain for predicting entity summarization from the context
        chain = LLMChain(llm=self.llm, prompt=self.entity_summarization_prompt)

        # Generate new summaries for entities and save them in the entity store
        for entity in self.entity_cache:
            # Get existing summary if it exists
            existing_summary = self.entity_store.get(entity, "")
            output = chain.predict(
                summary=existing_summary,
                entity=entity,
                history=buffer_string,
                input=input_data,
            )
            # Save the updated summary to the entity store
            self.entity_store.set(entity, output.strip())

Domain

Subdomains

Calls

Frequently Asked Questions

What does save_context() do?
save_context() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/memory/entity.py.
Where is save_context() defined?
save_context() is defined in libs/langchain/langchain_classic/memory/entity.py at line 567.
What does save_context() call?
save_context() calls 2 function(s): get, set.

Analyze Your Own Codebase

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

Try Supermodel Free