Repository navigation
Finish WorldModelRAG: Neo4j backend, 10k-doc demo, hybrid retrieval benchmark - #823
Conversation
…hybrid benchmark Closes the three remaining acceptance criteria from issue #735: a real Neo4j/Memgraph GraphBackend (mirrors KuzuWorldGraphBackend's write-through pattern), a 10k-document end-to-end demo backed by a deterministic synthetic causal-chain corpus, and a hybrid-vs-vector-only retrieval benchmark showing hybrid beats vector-only hit-rate by >=20% (hard-enforced in CI), reproducing >99% hit@5 vs 0% for vector-only at full scale. Co-Authored-By: Claude Sonnet 5 <[email protected]>
- Neo4jWorldGraphBackend.add_event no longer double-persists: the in-memory parent's add_event already calls the overridden upsert_entity/upsert_relation (which persist), so the extra _persist_entity/_persist_document calls were redundant network round trips. - graph_backend="neo4j"/"memgraph" now honor NEO4J_URI/NEO4J_USERNAME/ NEO4J_PASSWORD env vars instead of a hardcoded bolt://localhost:7687 with default credentials that silently targets the wrong endpoint outside local dev. - Added WorldModelRAG.close() forwarding to the graph backend (e.g. releases the Neo4j driver), using the same getattr-callable pattern as delete_by_metadata. - benchmarks/world_model_retrieval_bench.py now runs the hybrid and vector-only retrieval calls concurrently via asyncio.gather instead of sequential awaits. - Added a regression test guarding that both "neo4j" and "memgraph" route to Neo4jWorldGraphBackend (previously only exercised via Neo4jWorldGraphBackend's own testcontainers test, not through WorldModelRAG's string-based wiring). - Updated docs/world-model-rag.md, which still claimed neo4j/memgraph/ kuzu all "raise clear runtime errors" -- stale since kuzu already worked and now neo4j/memgraph do too. Co-Authored-By: Claude Sonnet 5 <[email protected]>
|
WIP |
…tegies Only the default "hybrid" (RRF) strategy had test coverage; add a test asserting graph_first/vector_first actually change result ordering. Co-Authored-By: Claude Sonnet 5 <[email protected]>
Co-Authored-By: Claude Sonnet 5 <[email protected]>
…x dropped resolver - ci.yml never ran benchmarks/test_world_model_retrieval_bench.py: the hard-enforced hybrid-vs-vector-only benchmark test only lived in benchmarks.yml, which triggers on tag push/nightly/manual dispatch, not pull_request. Add a dedicated CI job so the gate actually blocks PRs. - _make_graph_backend's "kuzu" branch dropped the caller's entity_resolver (pre-existing, sitting right next to the correctly-wired neo4j/memgraph branch this PR added). - Delete Neo4jWorldGraphBackend.add_event: a no-op override identical to the inherited behavior via MRO (InMemoryWorldGraphBackend.add_event already dispatches through the overridden upsert_entity/upsert_relation). - Close leaked Neo4j driver handles in test_neo4j_and_memgraph_strings_route_to_neo4j_backend. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…memory mirror Neo4jWorldGraphBackend.query_subgraph/to_mermaid previously read from the inherited in-memory dict, so picking Neo4j bought neither durability nor multi-process reads. Port the bounded-hop traversal pattern from Neo4jPropertyGraphBackend.traverse() so reads hit Neo4j directly; writes still go through the in-memory mirror first for seed/name-index lookup. Kuzu is left as-is per maintainer discussion on issue #735.
AmitoVrito
left a comment
There was a problem hiding this comment.
Review — solves #735, with one follow-up filed
Reviewed the diff against the issue's acceptance criteria and the surrounding code.
Verdict: approving. All four acceptance criteria are satisfied:
- ✅ 10k-doc end-to-end demo — deterministic, runs fully offline (
HeuristicWorldModelExtractor+HashingEmbeddings). - ✅ Hybrid beats vector-only by ≥20% — CI-enforced via
test_world_model_retrieval_bench.py. The synthetic corpus isolates graph-only-reachable tail docs, so it's a structural gap (fair, though best-case — the win is demonstrated with the regex extractor, not the LLM pipeline). - ✅ Backends —
in_memory+kuzu(now with roundtrip coverage) + newNeo4jWorldGraphBackend. Note the Neo4j testcontainers test skips without Docker, so CI coverage of the live Cypher path is effectively nil. - ✅ Time-travel
as_of— pre-existing onmain, confirmed working for in-memory.
Nice touches: real fakes (no MagicMock), deterministic seeds, a hard CI regression gate, and backfilled Kuzu coverage.
One correctness issue, tracked as a follow-up (not blocking merge): Neo4jWorldGraphBackend.query_subgraph filters min_confidence/as_of only on the edge result, not on the node/document path traversal (WORLD_RELATES*0..hops has no WHERE). In-memory prunes traversal by edge validity (world_model.py:631), so Neo4j will return extra nodes/documents for time-travel and confidence-filtered queries — a behavioral divergence between backends. Filed separately; fix + regression test incoming.
Merging now; the Neo4j parity fix follows in its own PR.
…ce/as_of (#826) Neo4jWorldGraphBackend.query_subgraph applied min_confidence/as_of only to the returned edges, so nodes — and their source documents — reachable solely through low-confidence or time-expired edges leaked into results, diverging from InMemoryWorldGraphBackend on confidence-filtered and time-travel (as_of) queries. Prune the node/document traversal with the same per-path validity predicate the in-memory BFS uses, and record relation-provenance documents against both edge endpoints (MENTIONS) so document parity holds too. Verified for parity against a live Neo4j via testcontainers; regression test fails on the pre-fix code. Also documents the WorldModelRAG Neo4j/Memgraph backend (#823) in the changelog. Closes #826. Co-authored-by: Abhay Krishna <[email protected]>
|
Thanks @Abhay-Mmmm, merged. Solid work on the Neo4j backend and the benchmark gate. 🙌 |
Summary
Closes the three remaining acceptance criteria on #735 (the core
WorldModelRAGfacade, extractors, resolver, hybrid retriever, bitemporal queries, causal linker, andin_memory/kuzubackends were already built and tested):Neo4jWorldGraphBackend(InMemoryWorldGraphBackend)mirrors the existingKuzuWorldGraphBackendwrite-through pattern (writes persisted to Neo4j, reads served from the in-memory mirror). Wired"neo4j"/"memgraph"into_make_graph_backend(both speak Bolt, same driver), added a.neo4j()classmethod, and exported it from the package__init__s.neo4j>=5.0was already declared under thegraphextra — no new dependency.examples/world_model_corpus.pydeterministically generates causal two-hop chains (Person → Product → Incident) split across separate documents, extracted via the existing zero-costHeuristicWorldModelExtractor.examples/world_model_rag.pynow ingests 10k real documents (runs in ~1s) with realHashingEmbeddings, and uses a real LLM whenOPENAI_API_KEY/ANTHROPIC_API_KEYis set.benchmarks/world_model_retrieval_bench.pycomparesHybridWorldModelRetrieveragainst a true vector-only baseline on the synthetic chain question set: hybrid hit@5 = 99.4% vs vector-only = 0.0% at full 10k-doc / 500-question scale.benchmarks/test_world_model_retrieval_bench.pyhard-enforces a ≥20 percentage-point hit-rate/MRR gap in CI viamake bench.Also closed a pre-existing gap:
KuzuWorldGraphBackendhad zero test coverage despite being fully implemented — addedtest_kuzu_backend_roundtrip.Test plan
pytest tests/retrieval/test_world_model.py -v— 17 existing + 2 new tests pass, Neo4j/Kuzu roundtrip tests skip cleanly without Docker/testcontainers (same convention as the existingtest_property_graph.pyNeo4j test)pytest tests/retrieval/ -v— full 224-test retrieval suite passesneo4j/kuzu/Docker are available locallypython examples/world_model_rag.py— 10k-doc demo runs end-to-end in ~1.2s, correctly reaches causal chain targets via graph traversalpython benchmarks/world_model_retrieval_bench.py --n-docs 10000— reproduces the ≥20% improvement claim at full scalepytest benchmarks/test_world_model_retrieval_bench.py -c benchmarks/pytest.ini— CI-enforced regression check passesruff checkclean on all changed/new files🤖 Generated with Claude Code