How hu-xiaobai/xmemory works
1. What this repository is
- src/ contains the reusable xMemory system: a hierarchical agent-memory pipeline that builds structured memory from dialogue and retrieves it later.
- evaluation/ contains the LoCoMo benchmark harness, dataset files, retrieval scripts, scoring scripts, and graph inspection utilities.
- The repo is therefore split into a core memory library and an experiment/evaluation layer.
2. Core domain objects
- The main data types are Message, Episode, and Semantic in src/models/.
- These represent a progression from raw dialogue turns to grouped episodes and then to higher-level semantic units/themes.
- The architecture is built around preserving intact units while adding hierarchy and searchable structure.
3. Memory construction pipeline
- src/core/memory_system.py is the main orchestrator for constructing memory from conversation streams.
- src/core/message_buffer.py holds incoming dialogue context.
- src/core/boundary_detector.py decides where coherent memory segments begin/end.
- src/generation/episode_generator.py, semantic_generator.py, and prediction_correction_engine.py turn buffered messages into structured memory objects, then refine them with LLM-driven correction.
- This is the “build” side of xMemory.
4. Hierarchical organization
- src/core/memory_hierarchy.py encodes the top-down organization that the paper describes: themes at the top, then semantic units, then episodes, then raw messages.
- src/core/lightweight_search_system.py and src/core/memory_system.py are the main runtime entry points that use this hierarchy during retrieval.
- The hierarchy is the key architectural boundary: retrieval is not flat top-k similarity, but guided by structure.
5. Search subsystem
- Search is factored into multiple retrieval backends under src/search/:
- bm25_search.py for lexical ranking
- vector_search.py for embedding-based similarity
- chroma_search.py for Chroma-backed vector lookup
- original_message_search.py and episode_original_message_search.py for direct access to source text
- unified_search.py to combine these signals
- This subsystem supports the adaptive hierarchical retrieval strategy used in evaluation.
6. Storage and persistence
- src/storage/ provides storage abstractions for episodes and semantic units.
- src/infrastructure/indices.py and repositories.py are the boundary between the memory logic and the persisted index structures.
- The README’s output layout implies persisted artifacts for episodes/, semantic/, themes/, graph/, and semantic-knn/, plus chroma_db/ for vector storage.
- In practice, construction produces both serialized memory artifacts and search indexes.
7. LLM and embedding dependencies
- src/utils/llm_client.py and llm_client_openllm.py abstract the LLM backend.
- src/utils/embedding_client.py abstracts embedding generation for semantic indexing and retrieval.
- src/services/providers.py likely centralizes model/provider selection so the rest of the system is backend-agnostic.
- The architecture is designed to work with different LLMs, but the README notes that config changes are needed when models or memory paths change.
8. Cross-cutting services
- src/services/cache.py handles reuse of intermediate results.
- src/services/event_bus.py supports internal event propagation across construction/retrieval stages.
- src/services/task_manager.py suggests batched/parallel execution during memory building or evaluation.
- src/services/metrics.py collects runtime or evaluation statistics.
- These are supporting services rather than business logic, but they affect throughput and observability.
9. Evaluation workflow
- evaluation/locomo/add.py builds memory for the LoCoMo dataset.
- evaluation/locomo/xMemory_search_framework.py runs retrieval/inference against the built memory.
- evaluation/locomo/evals.py and generate_scores.py post-process outputs into benchmark scores.
- evaluation/locomo/metrics/llm_judge.py indicates that at least part of evaluation uses LLM-based judging.
- This layer consumes the core library and produces benchmark artifacts like results JSON files.
10. Graph and analysis tooling
- evaluation/gexf_view.py is for visualizing the constructed memory graph.
- The presence of graph outputs and GEXF viewing indicates that the hierarchy is also represented as a navigable graph, not just a set of files.
- This is useful for debugging the split/merge behavior and the semantic organization of memory.
11. Configuration boundaries
- src/config.py and evaluation/locomo/config.json are the two main configuration touchpoints.
- environment.yml defines the conda environment and anchors the runtime setup.
- The repository expects the experiment scripts and core code to stay in sync on model names and storage paths.
12. End-to-end data flow
- Input begins as a dialogue dataset in evaluation/dataset/.
- Memory construction buffers messages, detects boundaries, generates episodes/semantics, and writes hierarchical indexes.
- Retrieval then traverses the hierarchy top-down, combining semantic, episodic, lexical, and vector search.
- Evaluation scripts consume retrieval outputs and compute final benchmark scores.
Main entry points: memory_system.py Memory system · xMemory_search_framework.py Eval search · utils LLM + embed · unified_search.py Unified search
Components 23
Core library
Reusable xMemory memory construction and retrieval components.
- Memory types · Message, Episode, and Semantic objects that carry the memory hierarchy.
src/models - Memory system · Main runtime that builds memory from conversation streams and serves retrieval entry points.
src/core/memory_system.py - Message buffer · Holds incoming dialogue context before segmentation.
src/core/message_buffer.py - Boundary detect · Finds coherent episode and semantic boundaries in the message stream.
src/core/boundary_detector.py - Episode gen · Turns buffered messages into episode-level memory units.
src/generation/episode_generator.py - Semantic gen · Lifts episodes into higher-level semantic units and themes.
src/generation/semantic_generator.py - Correction · LLM-driven refinement and correction of generated memory objects.
src/generation/prediction_correction_engine.py - Hierarchy · Encodes the theme → semantic → episode → message organization used during retrieval.
src/core/memory_hierarchy.py - Light search · Top-level retrieval path that traverses the hierarchy before searching.
src/core/lightweight_search_system.py - Unified search · Combines lexical, vector, and direct-message signals for adaptive retrieval.
src/search/unified_search.py - Search backends · Representative lexical, vector, Chroma, and source-message lookup implementations.
src/search - API facade · External-facing entry point for using the xMemory library programmatically.
src/api/facade.py
Support layer
Configuration, model providers, storage, infrastructure, and runtime services.
- Config · Repository-wide configuration for models, memory paths, and evaluation settings.
src/config.py - Providers · Chooses model and provider implementations used by the rest of the system.
src/services/providers.py - LLM + embed · LLM client and embedding client abstractions used for generation and search.
src/utils - Storage infra · Episode and semantic storage plus index/repository adapters for persisted artifacts.
src/storage - Runtime services · Cache, event bus, task manager, and metrics support for throughput and observability.
src/services
Evaluation layer
LoCoMo dataset ingestion, retrieval harness, scoring, and graph inspection.
- Build memory · Constructs xMemory artifacts for the LoCoMo benchmark dataset.
evaluation/locomo/add.py - Eval search · Runs retrieval and inference against built memories for LoCoMo.
evaluation/locomo/xMemory_search_framework.py - Score runs · Post-processes benchmark outputs and computes final scores.
evaluation/locomo/evals.py - LLM judge · LLM-based judging and metric utilities for benchmark evaluation.
evaluation/locomo/metrics/llm_judge.py - Graph view · Inspects the constructed memory graph and hierarchy structure.
evaluation/gexf_view.py - LoCoMo data · Input conversation dataset used to build and evaluate memory.
evaluation/dataset/locomo10.json
Connections 25
- LoCoMo data → Build memory: input · Benchmark conversations are ingested to build memory artifacts.
- Build memory → Memory system: build · Evaluation harness invokes the core memory constructor.
- Memory system → Message buffer: buffer · Incoming turns are accumulated before segmentation.
- Message buffer → Boundary detect: segment · Buffered dialogue is split into coherent units.
- Boundary detect → Episode gen: episodes · Detected boundaries drive episode construction.
- Episode gen → Semantic gen: lift · Episodes are summarized into semantic units and themes.
- Semantic gen → Correction: refine · Generated structures are corrected and normalized.
- Correction → Memory types: emit · Refined message, episode, and semantic objects are produced.
- Memory system → Hierarchy: organize · Constructed memory is arranged into the hierarchical representation.
- Hierarchy → Light search: guide · Hierarchy directs retrieval from top-level themes downward.
- Light search → Unified search: search · Hierarchy-aware retrieval delegates to combined search.
- Unified search → Search backends: backends · Unified retrieval merges lexical, vector, Chroma, and source-message lookup.
- Memory system → Storage infra: persist · Serialized memory artifacts and indexes are written through persistence adapters.
- Storage infra → Runtime services: support · Persistence and retrieval reuse shared services such as cache and metrics.
- Providers → LLM + embed: select · Provider selection controls which LLM and embedding implementations are used.
- LLM + embed → Episode gen: generate · LLM support drives episode and semantic generation.
- LLM + embed → Semantic gen: generate · Semantic lifting depends on model-backed generation.
- LLM + embed → Unified search: embed · Embedding support powers vector-based retrieval paths.
- API facade → Memory system: api · Public API delegates to the core orchestrator.
- Eval search → Light search: retrieve · Benchmark evaluation queries the hierarchy-aware search path.
- Eval search → Unified search: rank · Evaluation also exercises combined retrieval backends.
- Eval search → LLM judge: judge · Some benchmark outputs are evaluated with LLM-based judging.
- Eval search → Score runs: outputs · Retrieval results flow into scoring and aggregation.
- Score runs → LoCoMo data: compare · Scores are computed against the benchmark inputs and references.
- Graph view → Hierarchy: inspect · Graph tooling visualizes the constructed memory structure.