agents.memory_v2.message_document_converter¶
Message to Document Converter for Memory V2 System.
This module converts conversation messages into timestamped documents following LangChain patterns for long-term memory agents and graph construction.
Based on: https://python.langchain.com/docs/versions/migrating_memory/long_term_memory_agent/
Classes¶
Batch processor for converting entire conversations to documents. |
|
Converts conversation messages into timestamped documents for memory storage. |
|
Enhanced metadata for message-based documents. |
|
Document with enhanced timestamp and metadata for memory retrieval. |
Functions¶
|
Create searchable index of documents. |
|
Extract documents from specific timeframe. |
|
Sort documents by relevance and recency. |
Module Contents¶
- class agents.memory_v2.message_document_converter.ConversationDocumentBatch(conversation_id=None, user_id=None)¶
Batch processor for converting entire conversations to documents.
Initialize batch processor.
- process_conversation(messages, include_summary=True, chunk_size=5)¶
Process entire conversation into documents.
- Parameters:
- Returns:
List of all generated documents
- Return type:
- class agents.memory_v2.message_document_converter.MessageDocumentConverter(conversation_id=None, user_id=None, session_id=None)¶
Converts conversation messages into timestamped documents for memory storage.
Initialize converter with context.
- convert_message(message)¶
Convert single message to timestamped document.
- Parameters:
message (langchain_core.messages.BaseMessage) – LangChain message to convert
- Returns:
TimestampedDocument with rich metadata
- Return type:
- convert_messages(messages)¶
Convert multiple messages to timestamped documents.
- Parameters:
messages (list[langchain_core.messages.BaseMessage]) – List of messages to convert
- Returns:
List of TimestampedDocuments
- Return type:
- create_conversation_summary_document(messages, summary)¶
Create summary document from conversation.
- Parameters:
- Returns:
TimestampedDocument containing the summary
- Return type:
- create_memory_document(memory_item)¶
Convert memory item to timestamped document.
- Parameters:
memory_item (agents.memory_v2.memory_state_original.EnhancedMemoryItem) – Memory item to convert
- Returns:
TimestampedDocument for memory storage
- Return type:
- class agents.memory_v2.message_document_converter.MessageMetadata(/, **data)¶
Bases:
pydantic.BaseModel
Enhanced metadata for message-based documents.
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)
- class agents.memory_v2.message_document_converter.TimestampedDocument(page_content, metadata=None)¶
Bases:
langchain_core.documents.Document
Document with enhanced timestamp and metadata for memory retrieval.
Initialize with enhanced metadata.
- property timestamp: datetime.datetime¶
Get timestamp as datetime object.
- Return type:
- agents.memory_v2.message_document_converter.create_document_index(documents)¶
Create searchable index of documents.
- Parameters:
documents (list[TimestampedDocument]) – Documents to index
- Returns:
Index with metadata and statistics
- Return type:
- agents.memory_v2.message_document_converter.extract_documents_by_timeframe(documents, hours_back=24)¶
Extract documents from specific timeframe.
- Parameters:
documents (list[TimestampedDocument]) – List of timestamped documents
hours_back (float) – How many hours back to include
- Returns:
Filtered list of documents
- Return type:
- agents.memory_v2.message_document_converter.sort_documents_by_relevance_and_time(documents, time_weight=0.3, recency_decay=0.1)¶
Sort documents by relevance and recency.
- Parameters:
documents (list[TimestampedDocument]) – Documents to sort
time_weight (float) – Weight given to recency (0.0 to 1.0)
recency_decay (float) – How quickly relevance decays with time
- Returns:
Sorted documents (most relevant first)
- Return type: