Skip to main content

p-memory

English | 简体中文

An embedded memory library: durable memories, a knowledge graph, notes, and retrieval in one local directory, usable from Rust and Python.

p-memory embeds into your application; there is no separate service to run. Open a data directory from Rust or Python and it gives you the whole path — write, index, filter, recall — with no external database and nothing leaving your machine.

Quick start

use p_memory::{KnowledgeBase, MemoryInput, SearchRequest};

fn main() -> p_memory::Result<()> {
    let kb = KnowledgeBase::open("./data/app")?;
    kb.memories().upsert(MemoryInput::new("User prefers unsweetened tea"))?;
    let hits = kb.search(&SearchRequest { query: "unsweetened tea".into(), ..Default::default() })?;
    println!("{:?}", hits);
    kb.close()
}
from p_memory import KnowledgeBase

with KnowledgeBase("./data/app") as kb:
    kb.memories.upsert_by_judgment(judgment="User prefers unsweetened tea")
    print(kb.search("unsweetened tea")["hits"])

Features

  • Changing the embedding model doesn't break the database. Register a new embedding space for the new model; the old data stays where it is, with no full rebuild.
  • As small as possible. Text lives in normalized tables — one shared string dictionary, tag links as plain integer ids, nothing stored twice. For personal scale, keeping the store minimal is a deliberate goal.
  • Memory, graph, and notes in a single search. They share one index, so one query returns all three, fused with RRF.
  • Fast. For LLM memory, responsiveness comes first: local, single-file, no network round trips.
  • Vectorization and reranking live inside. The host registers an embedder (one model = one space) and a reranker, then hands over content and search terms; the library batches, truncates, retries, and degrades on its own.

Try the demo

One small set of memories and notes, an offline embedder and reranker, and one search — no keys, no network, reproducible. Vectors and reranking both happen inside the library:

cargo run --example minimal_demo
python -m p_memory.demo

Both print the hit count, which paths ran, and whether reranking truncated. The same fixture is the end-to-end acceptance sample for this interface.

Memory, the graph, and notes

Memory — holds both loose fragments and structured user profiles, and the two coexist. It also scales to a chat room of a thousand people: each person's memory stays its own, with no bleed between them.

The graph — entities, relations, and events. Find an entity by name, look at its neighborhood, or trace the shortest way two entities are connected.

Notes — documents chunked by paragraph; each chunk keeps only a line and character range into the note, not its own copy of the text. The searchable text lives in the full-text index.

Isolation — memories, the graph, and notes all support separation by domain and by agent. One store can serve several domains and several agents at once, each seeing only its own.

How they combine — the three are wired together: tag a memory and that tag can be an entity in the graph, so the memory and the entity point at the same person or thing and can be walked between without building a link by hand. Retrieval is undivided too — one query returns memories, the graph, and notes together.

Every host application uses its own data directory. p-memory ships the library and the import tooling; wiring it into P-ai, astrbot_plugin_angel_memory, or another host happens in that host's repository.

Rust

[dependencies]
p-memory = { path = "../p-memory" }
# Pin a version once the repository is pushed to your own Git remote.

Graph search is built in and needs no feature flags:

use p_memory::{KnowledgeBase, ReadFilter};

let kb = KnowledgeBase::open("./data/rust-app")?;
let filter = ReadFilter { namespace: "default".into(), scopes: vec!["public".into()], tags: vec![] };

let alice = kb.graph().resolve("Alice", &filter, 1)?[0].header.id;
let bob   = kb.graph().resolve("Bob",   &filter, 1)?[0].header.id;

let ring  = kb.graph().ego(alice, 2, &filter, 50)?;   // entities within two hops
let chain = kb.graph().path(alice, bob, &filter)?;    // shortest bridge between two entities

The Python binding exposes the same capabilities: ego / path / strongly_connected / component_count.

Python

CPython 3.10+. Windows and Linux wheels require no Rust; building from source needs Rust 1.88+ and a C/C++ toolchain.

# from the project root
python -m pip install .
# or install a prebuilt wheel
python -m pip install <path-to-wheel>
from p_memory import KnowledgeBase

with KnowledgeBase("./data/python-app") as kb:
    receipt = kb.memories.upsert_by_judgment(
        judgment="User prefers unsweetened tea", tags=["preference"]
    )
    print(receipt["value"]["id"])
    print(kb.search("unsweetened tea")["hits"])

Async hosts use async with AsyncKnowledgeBase(...) as kb, then await kb.memories.upsert(...) / await kb.search(...). Operations run on a worker thread and release the GIL; cancelling a wait cannot roll back a write that has already started.

Simple agent

# Model name and endpoint come from your provider. The API key is read from the environment only.
$env:OPENAI_BASE_URL = "http://localhost:8000/v1"
$env:P_MEMORY_MODEL = "your-tool-calling-model"
# Set OPENAI_API_KEY if the endpoint requires authentication.

p-memory chat --data ./data/agent --prompt "Remember: I like unsweetened tea" --trace
p-memory chat --data ./data/agent --prompt "What do I like to drink?" --trace
p-memory chat --data ./data/agent

Uses Chat Completions tools / tool_calls, with custom endpoint, model, timeout, round cap, read-only tool mode, and multi-tool replies. BM25 is the default; --embedding-model and --embedding-dimension enable semantic retrieval. Implementation: agent.py.

Importing existing data

p-memory import --source p_ai --source-id my-host `
  --database ./backup/memory_store.db --destination ./data/imported
# Review the report, then add --apply. The default is a dry run that creates no destination.

Supports importing snapshots from other memory systems; run p-memory import --help for the available sources. Original fields, lifecycle, scopes, and stable ID mappings are preserved; the source database is read-only. Graph lookup and note roots can be set explicitly. Old vector / FAISS / Tantivy caches are rebuilt; register an embedder afterward and run embeddings.sync to fill vectors — the import itself stays offline.

Documentation and testing

cargo test
python -m pytest

Apache-2.0. See LICENSE.

Release files for p-memory 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for p-memory 0.1.0
File
p_memory-0.1.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
p_memory-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details
p_memory-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details
p_memory-0.1.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details
p_memory-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details

Total release size: 21.3 MB

Release files / p_memory-0.1.0-cp310-abi3-win_amd64.whl

Download URL p_memory-0.1.0-cp310-abi3-win_amd64.whl
Size 4.2 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
2a128c6158246b315a3ac1554735580510a59e3df86ca7d6da4eb02731254da8
BLAKE2b-256 checksum
How to use checksums
7cdec4662fa656ffc9fa890eb0653760cf9644ec033983cecc77a142cbd16be2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / p_memory-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL p_memory-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 4.5 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
5df1838e560ad7c6d393eee3cbb509625e6eb14bbba269fc6db1f4e470fc91c3
BLAKE2b-256 checksum
How to use checksums
9d9867f0d53f117431e5bf740b6c8e97ec570d5bd6537b72678817a3c4093653
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / p_memory-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL p_memory-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 4.4 MB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
d34ac38dc536890bf6c9ccbd5a7f0ac4506db6e968a4b75ab62e295e6d59cee7
BLAKE2b-256 checksum
How to use checksums
a82a17efa0a4a67be3a6b4a4257d10efd741b8a8219b22c34200049af6b3b94f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / p_memory-0.1.0-cp310-abi3-macosx_11_0_arm64.whl

Download URL p_memory-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
Size 4.0 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
79fba184d80b59d351c4f687c4b83507c5d81788aa2afe369d36ec0d8ab39bdc
BLAKE2b-256 checksum
How to use checksums
12b8c064c9b6d967f727fc83bb5686ca794838bc611560c210067706432d2d9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / p_memory-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl

Download URL p_memory-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl
Size 4.3 MB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
a8a25efd32fb4391006ef4068e40f85dc48b585c21090f04d4c95ef65e466d55
BLAKE2b-256 checksum
How to use checksums
e3a373781f7e918ad3f32ce71f0d89a9165466627902fb814456dcc82af10533
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release history Release notifications | RSS feed

0.3.2

5 release files

0.3.1

5 release files

0.3.0

5 release files

0.2.3

5 release files

0.2.2

5 release files

0.2.1

5 release files

0.2.0

5 release files

This release

0.1.0 This release

5 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page