Skip to main content

A dual-layer memory system for AI agents with incremental clustering, graph-based retrieval, and conflict resolution.

Project description

m-memory — Dual-Layer Memory System for AI Agents

Python License Coverage

A dual-layer memory system for AI agents with incremental clustering, graph-based retrieval, and automatic conflict resolution.

Quick Start (5 minutes)

pip install m-memory
from memory_system.config import MemorySystemConfig
from memory_system.vector_store import NumpyVectorStore
from memory_system.graph_engine import NetworkXGraphStore
from memory_system.fake_llm import FakeLLMAdapter
from memory_system.retrieval import MemoryRetrievalEngineImpl

# 1. Create the engine
config = MemorySystemConfig()
config.embedding_dim = 8  # small dim for demo
engine = MemoryRetrievalEngineImpl(
    config=config,
    vector_store=NumpyVectorStore(dim=config.embedding_dim),
    graph_store=NetworkXGraphStore(),
    llm=FakeLLMAdapter(),
)

# 2. Ingest memories
id1 = engine.ingest("my cat", "My cat loves sleeping in the sun")
id2 = engine.ingest("my dog", "My dog enjoys running at the park")
id3 = engine.ingest("cat food", "I feed my cat premium dry food")

# 3. Search memories
result = engine.search("tell me about my cat")
for node, score in zip(result.nodes, result.scores):
    print(f"[{score:.3f}] {node.summary}: {node.content}")

Output example:

[0.812] cat food: I feed my cat premium dry food
[0.745] my cat: My cat loves sleeping in the sun

Core Concepts

Concept Description
MemoryNode One dialogue turn: A (summary for screening) + C (content for retrieval)
Bucket Dynamic cluster with a Medoid (representative node)
Medoid The node closest to all others in its bucket — used for coarse search
Cross-bucket Edge Soft link between related buckets — no physical duplication
Conflict Resolution Detects contradictions, marks old info as stale (not deleted)

Architecture

Query → [Layer 1: Bucket coarse screen] → [In-bucket fine search]
         → [Graph associative expansion] → [Layer 2: Conflict resolution]

See ARCHITECTURE_DESIGN.md for the full design spec.

API Reference

Semantic Mode (v0.3.0)

Replace NumpyVectorStore with LocalEmbeddingStore to activate the full two-layer semantic pipeline:

from memory_system.local_embedding import LocalEmbeddingStore

engine = MemoryRetrievalEngineImpl(
    config=config,
    vector_store=LocalEmbeddingStore(dim=384),  # all-MiniLM-L6-v2, semantic
    graph_store=NetworkXGraphStore(),
    llm=DeepSeekAdapter(),
)
# is_semantic() == True → _semantic_search() activates
# Layer 1: Medoid screening + graph expansion
# Layer 2: LLM conflict resolution + stale marking

Benchmarks (v7 — LLM-as-Judge on LoCoMo)

System LoCoMo (LLM-as-Judge)
Full-Context (upper bound) 87.5%
MIRIX 85.4%
m-memory 100% (48/48 sampled)
Zep 75.1%
Mem0 66.9%
A-Mem 48.4%

⚠️ 48 questions sampled from 1 conversation; full 1,986-question run pending. DeepSeek-chat judge (SOTA uses GPT-4o judge).

Production LLM

Replace FakeLLMAdapter with the DeepSeek adapter:

from memory_system.deepseek_llm import DeepSeekAdapter
from memory_system.embedding_store import OpenAIEmbeddingStore

# Keys from environment variables (never hardcode)
llm = DeepSeekAdapter()           # reads DEEPSEEK_API_KEY
store = OpenAIEmbeddingStore()    # reads OPENAI_API_KEY, real embeddings

engine = MemoryRetrievalEngineImpl(
    config=config,
    vector_store=store,        # semantic search
    graph_store=NetworkXGraphStore(),
    llm=llm,
)

When vector_store.is_semantic() == False (e.g., NumpyVectorStore), the engine automatically falls back to lexical keyword search.

Research Harness

This project includes a research testing protocol for academic experiments. Before running any Agent-driven tests, read these files in order:

# File Purpose
1 README.md Project overview + API (you are here)
2 AI_Memory_System_Testing_Protocol.md Harness engineering specs + anti-AIGC rules

⚠️ Agent Constraint: Any AI agent (including Reasonix Code) MUST read README.md before writing code or running tests. See Protocol §0.2.

Development

# Clone and install with dev dependencies
git clone https://github.com/RobinElysia/M-Memory.git
cd M-Memory
pip install -e ".[dev]"

# Run tests
pytest tests/

# Type check
mypy --strict memory_system/

# Lint
ruff check memory_system/

See CONTRIBUTING.md for the full development harness guide.

License

MIT — see pyproject.toml.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

m_memory-0.2.0.tar.gz (38.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

m_memory-0.2.0-py3-none-any.whl (39.1 kB view details)

Uploaded Python 3

File details

Details for the file m_memory-0.2.0.tar.gz.

File metadata

  • Download URL: m_memory-0.2.0.tar.gz
  • Upload date:
  • Size: 38.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.1

File hashes

Hashes for m_memory-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0661f0d5dc939f64357e0e01a06e9324e78b2caf31bee91d094fcb8d5c67b2a0
MD5 aa7b98cfd7a5d6011c57aae7884e0af9
BLAKE2b-256 4f6572ae2a12d93d420cb00747879ff1363c721a17d3aabe5a22be600e39d09e

See more details on using hashes here.

File details

Details for the file m_memory-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: m_memory-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 39.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.1

File hashes

Hashes for m_memory-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 076490bd9b32c52c41b78ebe458d87c8f2eea25a1f4b3413bba24fbaa94d3a99
MD5 65566639a1688fbfb6a6b6e778e383a0
BLAKE2b-256 8041da6c6a921d6c5b0546497b76f09d15b83de91a2b9867f5f332aab3266539

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page