Add long-term-memory module: file, vector and summary memory on one Spring AI agent. AutoMemoryTools and AutoMemoryToolsAdvisor, fact extraction with a privacy gate into PostgreSQL and pgvector, VectorStoreChatMemoryAdvisor, a versioned summary, updating, forgetting, isolation, expiry, recall with all-MiniLM-L6-v2 and token cost. Scripted ChatModel; no provider or local LLM called. Spring Boot 4.2.0, Spring AI 2.1.0, Java 25.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4RzcCS5JCAgcxLz7T
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
# long-term-memory
|
||||
|
||||
Long-term memory for a Spring AI agent, three ways, on the same travel-desk agent: **file memory**
|
||||
(`AutoMemoryTools` / `AutoMemoryToolsAdvisor` from spring-ai-agent-utils), **vector memory** (facts
|
||||
extracted with `entity()`, kept in a PostgreSQL table and indexed in pgvector) and **summary memory**
|
||||
(one rolling profile per user). **The chat model is a hand-written `ScriptedChatModel`, not a language
|
||||
model.** The embeddings (all-MiniLM-L6-v2, in-process), PostgreSQL, pgvector, the files and Spring AI
|
||||
are real. Nothing here measures how well a real model decides what to remember.
|
||||
|
||||
Companion code for [Long-Term Memory for Spring AI Agents](https://ankurm.com/spring-ai-long-term-memory-file-vector-summary/)
|
||||
on [ankurm.com](https://ankurm.com). Short-term (session) memory is the [`session-memory/`](../session-memory) module.
|
||||
|
||||
## Versions
|
||||
|
||||
| Component | Version |
|
||||
|---|---|
|
||||
| Spring Boot / Spring AI | 4.2.0 / 2.1.0 |
|
||||
| spring-ai-agent-utils | 0.14.0 |
|
||||
| Java | 25 (Temurin 25.0.5.1) |
|
||||
| PostgreSQL / pgvector | 17.12 / 0.8.7 (`pgvector/pgvector:pg17`) |
|
||||
| Embeddings | all-MiniLM-L6-v2, 384 dimensions, ONNX |
|
||||
|
||||
## Run it
|
||||
|
||||
```bash
|
||||
export JAVA_HOME=/path/to/jdk-25
|
||||
./scripts/services-up.sh # Docker: PostgreSQL + pgvector on 127.0.0.1:5522
|
||||
./scripts/run-all.sh # 12 tests, regenerate output/
|
||||
```
|
||||
|
||||
No API key. The first run downloads the embedding model (about 90 MB) from Hugging Face. Without
|
||||
PostgreSQL the tests that need it are skipped and only `output/01`-`04` are rewritten. File tests use
|
||||
`/tmp/ltm-memory/`. `output/04` is timing dependent; token counts are cl100k_base estimates.
|
||||
|
||||
## Code
|
||||
|
||||
| File | What it is |
|
||||
|---|---|
|
||||
| [`Agents.java`](src/main/java/com/ankurm/ltm/Agents.java) | The one agent; memory is a per-request advisor |
|
||||
| [`FactExtractor.java`](src/main/java/com/ankurm/ltm/FactExtractor.java) | Write path: `entity()` into UPSERT / FORGET changes |
|
||||
| [`PrivacyGate.java`](src/main/java/com/ankurm/ltm/PrivacyGate.java) | Category allow-list and a few secret shapes, in code |
|
||||
| [`JdbcFactStore.java`](src/main/java/com/ankurm/ltm/JdbcFactStore.java) | System of record: upsert, expiry, forget, erase user |
|
||||
| [`FactIndex.java`](src/main/java/com/ankurm/ltm/FactIndex.java) | pgvector index with ids derived from (user, key) |
|
||||
| [`MemoryBlockAdvisor.java`](src/main/java/com/ankurm/ltm/MemoryBlockAdvisor.java) | Read path: recalled text into the system message |
|
||||
| [`SummaryStore.java`](src/main/java/com/ankurm/ltm/SummaryStore.java), [`SummaryUpdater.java`](src/main/java/com/ankurm/ltm/SummaryUpdater.java) | Summary memory with a version column |
|
||||
| [`PromptCost.java`](src/main/java/com/ankurm/ltm/PromptCost.java) | Token estimate of a recorded `Prompt` |
|
||||
|
||||
## Output
|
||||
|
||||
| File | Test |
|
||||
|---|---|
|
||||
| `01`-`04` | [`FileMemoryTest`](src/test/java/com/ankurm/ltm/FileMemoryTest.java) |
|
||||
| `05`-`10` | [`FactMemoryTest`](src/test/java/com/ankurm/ltm/FactMemoryTest.java) |
|
||||
| `11`-`13` | [`SummaryAndCostTest`](src/test/java/com/ankurm/ltm/SummaryAndCostTest.java) |
|
||||
|
||||
## Not covered
|
||||
|
||||
A live LLM; `AutoDreamService` consolidation (present in agent-utils 0.14.0, not run); encryption at
|
||||
rest; multi-node file storage; hosted memory services.
|
||||
Reference in New Issue
Block a user