Metadata-Version: 2.4
Name: hippokalman
Version: 0.1.1
Summary: Zero-LLM-write hippocampal reconsolidation memory (HippoMem & HippoKalman) for LLMs, AI agents (MCP server), and native PyTorch Transformers.
Author: Ju-un (Julian) Park
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/whitesm2000/hippomem
Project-URL: Repository, https://github.com/whitesm2000/hippomem
Project-URL: Issues, https://github.com/whitesm2000/hippomem/issues
Project-URL: Paper, https://doi.org/10.5281/zenodo.24705838
Keywords: llm,memory,rag,mcp,hippocampus,proactive-interference,pytorch,kalman
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Provides-Extra: embeddings
Requires-Dist: sentence-transformers>=3.0.0; extra == "embeddings"
Provides-Extra: bm25
Requires-Dist: rank-bm25>=0.2.2; extra == "bm25"
Provides-Extra: torch
Requires-Dist: torch>=2.2.0; extra == "torch"
Provides-Extra: all
Requires-Dist: sentence-transformers>=3.0.0; extra == "all"
Requires-Dist: rank-bm25>=0.2.2; extra == "all"
Requires-Dist: torch>=2.2.0; extra == "all"
Dynamic: license-file

# HippoMem

[![PyPI version](https://img.shields.io/pypi/v/hippokalman.svg)](https://pypi.org/project/hippokalman/)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.24705838.svg)](https://doi.org/10.5281/zenodo.24705838)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

**Zero-LLM-write hippocampal reconsolidation memory for LLMs, AI agents, and native PyTorch Transformers.**

Standard vector RAG stores are append-only: when facts change over time, stale records accumulate and rival current truths. LLM-write memory pipelines (Mem0/Zep style) run an LLM call on every turn—adding latency, token cost, privacy exposure, and extraction errors.

**HippoMem** translates four mechanisms of the mammalian hippocampus—**Dentate Gyrus (DG) pattern separation**, **CA3 attractor-targeted Kalman labile reconsolidation**, **straight-through unit-sphere projection**, and **event-boundary segmentation**—into:
1. A drop-in Python library + SQLite-persisted **MCP server** (`hippomem-mcp`) that resolves conflicts and tracks paraphrased updates in `< 0.1 ms` with **zero LLM write calls**.
2. A strictly causal native PyTorch Transformer memory layer (`DGCA3HippoKalmanBlock`).

---

## Headline Benchmarks (`bf16` H100)

| Benchmark | Flat-RAG | Hybrid-RRF / Full-Ctx | Mem0-Style (7B LLM/turn) | **HippoMem (Ours, 0 LLM writes)** |
|---|---:|---:|---:|---:|
| **Standard `PI-LLM`** (`U=40`, Llama-3.1-8B) | 2.0% | 70.0% (Full-Ctx, 20.8K ch) | — | **100.0%** (`0.0%`g, **20.7x** fewer ch) |
| **Standard `PI-LLM`** (`U=40`, Qwen2.5-7B) | 2.0% | 86.0% (Full-Ctx) | 3.3% (`19.6 ms/ep`) | **93.3–98.0%** (**`0.017 ms/ep`**, **1,155x faster**) |
| **Paraphrased `PI-LLM`** (`U=40`, Llama-3.1-8B) | 0.0% | 33.3% (Full-Ctx, 9.1K ch) | — | **83.3%** (`91.7%` mean, **10.3x** fewer ch) |
| **`LongMemEval-S` All-500** (Llama-3.1-8B, cross-judged) | 48.6% | 53.0% (Hybrid-RRF) | — | **54.2%** (`77.8%` KU, **+7.0pp**) |
| **`LongMemEval-S` KU-78** (Qwen2.5-7B) | 63.9% | 69.4% (Hybrid-RRF) | 29.2% (`12.6 ms/ep`, 8.1M tok) | **70.8%** (**`0.064 ms/ep`**, **198x faster**) |

---

## Installation

```bash
pip install hippokalman                 # core numpy + SQLite + MCP server
pip install "hippokalman[embeddings]"   # + local sentence-transformers (BAAI/bge-small-en-v1.5)
pip install "hippokalman[all]"          # + rank-bm25 + PyTorch native layer
```

*(Both `import hippokalman` and `import hippomem`, as well as `hippokalman-mcp` and `hippomem-mcp`, are supported.)*

---

## 5-Line Python Quickstart

```python
from hippomem import HippoMem, SentenceTransformerEmbedder

mem = HippoMem(
    db_path="./agent_memory.db",
    embedder=SentenceTransformerEmbedder("BAAI/bge-small-en-v1.5"),
    theta_rec=0.78,
)

mem.add("Alice's primary production database is PostgreSQL.", session_id="s1", timestamp="2026-01-10")
mem.add("Update: Alice migrated her primary database to ClickHouse.", session_id="s2", timestamp="2026-03-14")

hits = mem.search("What is Alice's current primary database?", top_k=3)
print(hits[0]["text_for_llm"])
# [CURRENT MEMORY | 2026-03-14] Update: Alice migrated her primary database to ClickHouse.
#   [SUPERSEDED PRIOR | 2026-01-10] Alice's primary production database is PostgreSQL.
```

---

## Paper & Benchmark Reproduction

- **Paper PDF:** [`paper/main.pdf`](paper/main.pdf) (11 pages, TMLR format; LaTeX source in [`paper/main.tex`](paper/main.tex))
- **Benchmarks & Exact JSON Results:** [`benchmarks/README.md`](benchmarks/README.md) (`benchmarks/results/` + `benchmarks/scripts/`)

---

## MCP Memory Server Setup (Claude Desktop / Cursor / Antigravity)

Add `hippomem-mcp` as a local stdio MCP server (runs 100% locally, zero cloud LLM write calls):

```json
{
  "mcpServers": {
    "hippomem": {
      "command": "hippomem-mcp",
      "args": [
        "--db", "~/.hippomem/memory.db",
        "--model", "BAAI/bge-small-en-v1.5",
        "--theta-rec", "0.78"
      ]
    }
  }
}
```

Exposed MCP tools: `memory_add`, `memory_search`, `memory_history`, `memory_delete`, `memory_stats`.

---

## Native PyTorch Layer (`DGCA3HippoKalmanBlock`)

```python
import torch
from hippomem.torch_layer import DGCA3HippoKalmanBlock

layer = DGCA3HippoKalmanBlock(d_model=512, n_heads=8, chunk_size=8, n_slots=32)
x = torch.randn(2, 256, 512)
y = layer(x)  # strictly causal (max_diff = 0.00e+00), non-expansive ST unit-sphere BPTT
```

---

## Citation

```bibtex
@article{Park2026HippoMem,
  author = {Ju-un (Julian) Park},
  title  = {Hippocampal Reconsolidation Memory: Defeating Proactive Interference Under Paraphrase Drift in External and Native Neural Language Models},
  year   = {2026},
  doi    = {10.5281/zenodo.24705838},
  url    = {https://github.com/whitesm2000/hippomem}
}
```

## License

Apache License 2.0 — see `LICENSE`.
