Skip to content

Retrieval Evaluation

memorylayer-eval measures how well MemoryLayer retrieves. It boots the real service stack in-process, ingests a corpus, runs every query through the real recall() pipeline, and scores the results against ground truth. Any change to ranking shows up in the numbers, whether it is fusion, boosts, channels, or rerankers. The same command runs in MemoryLayer’s CI as a required retrieval-regression gate.

It installs with the server:

Terminal window
pip install memorylayer-server
memorylayer-eval run # score only; always exits 0
memorylayer-eval gate # score, then fail (exit 1) if any threshold is missed

You can also run it as python -m memorylayer_server.eval.

With no arguments, it uses a small fixture corpus bundled in the wheel (15 documents plus a qrels file) and the deterministic hash embedding provider. That makes it fast and fully offline.

Metrics

MetricMeaning
recall@kShare of each query’s relevant memories found in the top k (mean over queries)
precision@kShare of the top k that are relevant
MRRMean reciprocal rank of the first relevant result
nDCG@kRank-discounted gain (uses graded relevance when the qrels provide grades)
first-relevant hit rateShare of queries with at least one relevant memory in the top k
expected-top1 hit rateShare of queries whose expected_top1 memory ranked first
latencyMean recall latency in milliseconds

The Gate

gate passes only if every metric meets its floor:

ThresholdDefaultOverride
mean recall@k0.80--min-recall
first-relevant hit rate0.80--min-first-relevant
expected-top1 hit rate0.60--min-expected-top1
k5--k

The defaults are tuned for the bundled fixture. Set your own floors when you gate on a different corpus. Add --json for machine-readable output in CI.

Options

FlagDefaultDescription
--corpus / --qrelsbundled fixtureYour own corpus and ground truth
--k5Cutoff for the @k metrics
--embedding-providerhashAny server embedding provider, such as embed_server or openai
--dimensionsprovider defaultEmbedding dimensions
--embed-server-urlhttp://localhost:61051 for embed_serverEmbed server peer
--hybrid / --no-hybridserver defaultForce keyword + vector fusion on or off
--rerankernonenone, rrf, llm, or hyde
--storage-backendsqlitesqlite, postgresql, or in_memory
--postgres-url—Connection URL when the backend is postgresql
--data-dirfresh temp dirWhere the run stores its data
--clean-ingestoffBatch-embed and insert directly, skipping write-path enrichment
--jsonoffJSON output

For example, to compare two embedding setups on the same data:

Terminal window
memorylayer-eval run --corpus my_corpus.jsonl --qrels my_qrels.json --k 10 \
--embedding-provider embed_server --dimensions 384 --json > embed.json
memorylayer-eval run --corpus my_corpus.jsonl --qrels my_qrels.json --k 10 --json > hash.json

Data Format

Corpus is JSONL. Each row is one memory, identified by a key:

{"key": "fastapi-web", "content": "FastAPI is a Python framework for building APIs.", "tags": ["python", "web"]}

Qrels is JSON. Each query lists the corpus keys that are relevant, and optionally the one that should rank first, plus graded relevance:

{
"schema_version": 1,
"queries": [
{"query_id": "q1", "query": "python web framework for building APIs",
"relevant": ["fastapi-web", "python-backend"], "expected_top1": "fastapi-web"}
]
}

Public Benchmarks

The memorylayer-core-python/benchmarks/ directory in the source repository has converters that turn public datasets into this format. They are standalone scripts, not part of the installed CLI. Each reads raw data from benchmarks/.cache/raw/ and writes corpus.jsonl and qrels.json under benchmarks/.cache/<name>/. For HotpotQA and LoCoMo, download the raw file yourself; each script’s docstring gives the URL.

ScriptDatasetNotes
hotpotqa_convert.pyHotpotQAMulti-hop QA (--questions, default 200; needs pyarrow)
locomo_convert.pyLoCoMoLong-term conversational memory. Evidence turns are the relevant set. Adversarial questions are excluded unless you pass --include-adversarial.
longmemeval_convert.pyLongMemEvalLong-horizon chat memory (--questions 75, --max-sessions 60)
gammacorpus_convert.pyGammaCorpusPulled from the Hugging Face datasets server (--rows 2000, --queries 400)
build_subset.pyany converted corpusDraws a reproducible subset (--size 500, --seed 13)
download_sources.pyGammaCorpus, LongMemEvalFetches raw source files into benchmarks/.cache/raw/
Terminal window
python benchmarks/locomo_convert.py --questions 200
memorylayer-eval run \
--corpus benchmarks/.cache/locomo/corpus.jsonl \
--qrels benchmarks/.cache/locomo/qrels.json \
--k 5 --embedding-provider embed_server --dimensions 384

Use these runs to validate a change before you flip one of the retrieval channel flags in production. Several channels are off by default until they pass exactly this kind of gate.