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¶
Structured knowledge in (subject, predicate, object) format. |
|
Long-term memory agent following LangChain patterns. |
|
Persistent storage for long-term memories. |
|
Individual memory entry with timestamp and metadata. |
Functions¶
|
Factory function to create long-term memory agent. |
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:
- 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:
- async add_conversation(messages)¶
Add conversation and extract memories.
- Parameters:
messages (list[langchain_core.messages.BaseMessage])
- Return type:
- classmethod as_memory_tool(user_id, **config_kwargs)¶
Create memory tool for ReactAgent integration.
- Parameters:
user_id (str)
- 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
- 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:
- 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:
- Return type:
- 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.
- 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:
- Return type:
- async agents.memory_v2.long_term_memory_agent.demo_long_term_memory()¶
Demo the long-term memory agent functionality.