Async Memory
Asynchronous memory for Mem0
AsyncMemory
The AsyncMemory
class is a direct asynchronous interface to Mem0’s in-process memory operations. Unlike the memory, which interacts with an API, AsyncMemory
works directly with the underlying storage systems. This makes it ideal for applications where you want to embed Mem0 directly into your codebase.
Initialization
To use AsyncMemory
, import it from the mem0.memory
module:
Key Features
- Non-blocking Operations - All memory operations use
asyncio
to avoid blocking the event loop - Concurrent Processing - Parallel execution of vector store and graph operations
- Efficient Resource Utilization - Better handling of I/O bound operations
- Compatible with Async Frameworks - Seamless integration with FastAPI, aiohttp, and other async frameworks
Methods
All methods in AsyncMemory
have the same parameters as the synchronous Memory
class but are designed to be used with async/await
.
Create memories
Add a new memory asynchronously:
Retrieve memories
Retrieve memories related to a query:
List memories
List all memories for a user_id
, agent_id
, or run_id
:
Get specific memory
Retrieve a specific memory by its ID:
Update memory
Update an existing memory by ID:
Delete memory
Delete a specific memory by ID:
Delete all memories
Delete all memories for a specific user, agent, or run:
Note: At least one filter (user_id, agent_id, or run_id) is required when using delete_all.
Memory History
Get the history of changes for a specific memory:
Example: Concurrent Usage with Other APIs
AsyncMemory
can be effectively combined with other async operations. Here’s an example showing how to use it alongside OpenAI API calls in separate threads:
If you have any questions or need further assistance, please don’t hesitate to reach out: