agents.memory_v2.long_term_memory_agent

Long-Term Memory Agent following LangChain patterns.

This implementation follows the LangChain long-term memory agent documentation: https://python.langchain.com/docs/versions/migrating_memory/long_term_memory_agent/

Key features: 1. Load memories first approach 2. Semantic memory retrieval across conversations 3. Text and structured knowledge storage 4. Time-weighted retrieval 5. ReactAgent tool integration

Architecture: - BaseRAGAgent for memory retrieval - SimpleRAGAgent for memory-enhanced responses - Memory extraction and storage pipeline - Cross-conversation persistence

Classes

KnowledgeTriple

Structured knowledge in (subject, predicate, object) format.

LongTermMemoryAgent

Long-term memory agent following LangChain patterns.

LongTermMemoryStore

Persistent storage for long-term memories.

MemoryEntry

Individual memory entry with timestamp and metadata.

Functions

create_long_term_memory_agent(user_id[, llm_config, ...])

Factory function to create long-term memory agent.

demo_long_term_memory()

Demo the long-term memory agent functionality.

Module Contents

class agents.memory_v2.long_term_memory_agent.KnowledgeTriple(/, **data)

Bases: pydantic.BaseModel

Structured knowledge in (subject, predicate, object) format.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

to_memory_entry(**kwargs)

Convert to MemoryEntry.

Return type:

MemoryEntry

class agents.memory_v2.long_term_memory_agent.LongTermMemoryAgent(user_id, llm_config=None, storage_path='./memory_store', embedding_model='sentence-transformers/all-mpnet-base-v2', vector_store_provider=VectorStoreProvider.FAISS, name='long_term_memory_agent')

Long-term memory agent following LangChain patterns.

This agent implements the “load memories first” approach: 1. Load relevant memories from storage 2. Use BaseRAGAgent for semantic memory retrieval 3. Enhanced response generation with memory context 4. Extract and store new memories from conversation

Examples

Basic usage:

agent = LongTermMemoryAgent(user_id="user123")
await agent.initialize()

# Memory-enhanced conversation
response = await agent.run("What do you remember about my work?")

With specific LLM config:

llm_config = AzureLLMConfig(deployment_name="gpt-4", ...)
agent = LongTermMemoryAgent(
    user_id="user123",
    llm_config=llm_config
)

Initialize long-term memory agent.

Parameters:
  • user_id (str)

  • llm_config (haive.core.models.llm.base.LLMConfig | None)

  • storage_path (str)

  • embedding_model (str)

  • vector_store_provider (haive.core.engine.vectorstore.VectorStoreProvider)

  • name (str)

async add_conversation(messages)

Add conversation and extract memories.

Parameters:

messages (list[langchain_core.messages.BaseMessage])

Return type:

list[MemoryEntry]

classmethod as_memory_tool(user_id, **config_kwargs)

Create memory tool for ReactAgent integration.

Parameters:

user_id (str)

get_memory_summary()

Get summary of stored memories.

Return type:

dict[str, Any]

async initialize()

Initialize memory retrieval and enhanced response agents.

Return type:

None

async run(query, extract_memories=True)

Run memory-enhanced conversation.

This implements the “load memories first” pattern: 1. Retrieve relevant memories using BaseRAGAgent 2. Generate enhanced response using SimpleRAGAgent with memory context 3. Extract and store new memories from the interaction

Parameters:
  • query (str)

  • extract_memories (bool)

Return type:

dict[str, Any]

class agents.memory_v2.long_term_memory_agent.LongTermMemoryStore(storage_path='./memory_store')

Persistent storage for long-term memories.

Initialize memory store.

Parameters:

storage_path (str)

add_knowledge_triple(triple, **kwargs)

Add knowledge triple as memory.

Parameters:

triple (KnowledgeTriple)

Return type:

MemoryEntry

add_memory(memory)

Add memory to store.

Parameters:

memory (MemoryEntry)

Return type:

None

get_memories(user_id=None, limit=None)

Get memories, optionally filtered by user.

Parameters:
  • user_id (str | None)

  • limit (int | None)

Return type:

list[MemoryEntry]

search_memories(query, user_id=None, limit=5)

Simple text search in memories.

Parameters:
  • query (str)

  • user_id (str | None)

  • limit (int)

Return type:

list[MemoryEntry]

class agents.memory_v2.long_term_memory_agent.MemoryEntry(/, **data)

Bases: pydantic.BaseModel

Individual memory entry with timestamp and metadata.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

mark_accessed(query=None, relevance=None)

Mark memory as accessed.

Parameters:
  • query (str | None)

  • relevance (float | None)

to_document()

Convert to LangChain Document for RAG.

Return type:

langchain_core.documents.Document

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

agents.memory_v2.long_term_memory_agent.create_long_term_memory_agent(user_id, llm_config=None, storage_path='./memory_store')

Factory function to create long-term memory agent.

Parameters:
  • user_id (str)

  • llm_config (haive.core.models.llm.base.LLMConfig | None)

  • storage_path (str)

Return type:

LongTermMemoryAgent

async agents.memory_v2.long_term_memory_agent.demo_long_term_memory()

Demo the long-term memory agent functionality.