Prism is a multimodal semantic indexing and decoding system. It maps text, images, audio, video, and documents into shared semantic geometry, organizes that geometry into graph communities, and translates it back into grounded human-readable labels or summaries.
A runnable MVP of the six-stage hybrid retrieval/generation blueprint:
- deterministic or OpenAI embeddings;
- SVD PCA compression and percentile reconstruction-error gating;
- BM25 isolation for structural outliers;
- cosine kNN graph, deterministic community detection, and centroids;
- continuous-vector-to-LLM prefix projection;
- routed retrieval, drafting, and grounded verification.
Gemini Embedding 2 is the default production embedding backend, using 768-dimensional MRL output and asymmetric question-answering/document prefixes. A signed feature hashing backend remains available for offline tests. Drafting defaults to an extractive baseline; a Hugging Face causal-LM adapter is included.
python -m venv .venv
. .venv/bin/activate # Windows: .venv\Scripts\Activate.ps1
pip install -e '.[dev,gemini]'
# GOOGLE_API_KEY must be available to the Google Gen AI SDK.
gvd build examples/corpus.jsonl .index --pca-dimensions 4 --outlier-percentile 90
gvd stats .index
gvd query .index "How are vector prefixes mapped into a language model?"
pytestNo system Python installation is required when uv is available:
$env:GOOGLE_API_KEY = "your-key"
uv run --extra dev --extra gemini gvd build examples/corpus.jsonl .index `
--pca-dimensions 4 --outlier-percentile 90
uv run --extra gemini gvd query .index "How are vectors projected into the model?"
uv run --extra dev --extra gemini pytest -qDo not commit the API key. For persistent local configuration, use your preferred secret manager or a user-scoped environment variable outside the repository.
Corpus embedding uses four concurrent requests by default. Set
GVD_EMBED_WORKERS=1 for strict serial quotas. For large offline ingestion jobs,
prefer Gemini's asynchronous Batch API for its throughput and pricing.
For an offline smoke test, add --embedding-provider hashing --embedding-dimensions 384 to gvd build.
The query prefix defaults to task: question answering | query: .... Override it
with --embedding-task-name "code retrieval" (or another Gemini-supported task)
when building the index; the choice is persisted with the index.
Input is JSONL with a required text, optional id, and optional metadata:
{"id":"doc-1","text":"Document body","metadata":{"source":"manual"}}documents -> embed -> PCA -> reconstruction residual
| |
<= threshold > threshold
| |
cosine kNN graph BM25 index
|
communities + centroids
|
query -> sparse-route gate OR nearest centroid -> evidence -> drafter -> verifier
GraphVectorDrafter.fit() builds both paths. At query time, a sufficiently strong
BM25 match against the isolated set wins; otherwise the query is projected through
the fitted PCA model, routed to its nearest community centroids, and globally
reranked across their members. --community-probes controls the recall/latency
tradeoff. The default probes up to 32 communities because narrow routing proved
too brittle on long books; smaller values are an explicit latency optimization.
Sparse outlier routing is opt-in: residual outliers must also carry
metadata.is_outlier=true or matching metadata.outlier_terms. PCA novelty alone
cannot hijack normal queries.
Install the ML extra:
pip install -e '.[ml]'build_torch_projection(input_dim, hidden_dim, prefix_tokens) returns the Phase-3
MLP. Its output shape is [batch, prefix_tokens, hidden_dim]. The
HuggingFaceVectorDrafter concatenates that output before prompt token embeddings.
training.projection_training_step implements frozen-core cross-entropy training;
after projection warm-up, apply PEFT LoRA/QLoRA to selected attention layers.
For a real training run, pairs should contain the exact fitted PCA coordinate and a grounded target text. Split by source/document—not randomly by chunk—to prevent leakage. Version the embedding model, PCA matrix, graph, tokenizer, adapter, and corpus together; their coordinate systems are coupled.
Evaluate the performance of a built GVD index against a dataset of labeled queries using the CLI:
gvd evaluate <path-to-index> <path-to-labeled-queries.jsonl> --output report.jsonThe queries file must be in JSONL format, where each line specifies a query string and its list of relevant ground-truth document IDs:
{"query": "How does PCA compress embeddings?", "relevant_doc_ids": ["pca", "residual"]}The evaluation report is printed as a JSON object containing the following metric groups:
- End-to-End Query Retrieval: Calculates
Recall@1,Recall@3,Recall@5,MRR, andnDCG@5.- Honest Interpretation: Measures how successfully the pipeline finds relevant documents. Too few community probes can cap recall; exhaustive fan-out is appropriate for small corpora, while large indexes must tune the probe count against latency.
- Community Clustering Quality: Computes
Purity,Normalized Mutual Information (NMI), andAdjusted Rand Index (ARI)against documenttopicmetadata labels.- Honest Interpretation: Measures how well unsupervised community boundaries align with human-defined topics. High purity shows cohesive clusters, but low NMI/ARI is common and does not automatically translate to poor search relevance.
- Clustering Stability: Computes the mean pairwise
ARIof community partitions generated across 5 different random seeds.- Honest Interpretation: Evaluates if the community partitions are robust. If the stability ARI is low (e.g. < 0.6), the graph representation contains loose, overlapping, or "fuzzy" community boundaries.
- PCA Cosine Retrieval Ablation: Compares the metrics (
Recall,MRR,nDCG) and top-5 document overlap (Jaccard) of retrieval in raw embedding space versus PCA-compressed space.- Honest Interpretation: Quantifies the search quality lost to SVD compression. A low top-5 Jaccard overlap or a large drop in Recall/nDCG indicates that
pca_dimensionsis set too low and is discarding critical semantic variance.
- Honest Interpretation: Quantifies the search quality lost to SVD compression. A low top-5 Jaccard overlap or a large drop in Recall/nDCG indicates that
- Outlier Gating Performance: Calculates
Precision,Recall, andF1-scoreof PCA outlier detection against ground-truthis_outliermetadata labels.- Honest Interpretation: Evaluates the reconstruction-error threshold. Because SVD residuals reflect linear reconstructions, the gate may flag highly unique or longer documents as outliers even if they are semantically normal.
Simulate speculative verification speeds and token acceptance rates on on-policy target outputs using the CLI:
gvd draft-evaluate <path-to-on-policy-corpus.jsonl> --output report.jsonThe corpus file must contain JSONL records, each providing a prompt and the generated target text:
{"prompt": "The quick brown", "target_text": "fox jumps over the lazy dog."}The command deterministically splits records into train/test sets, fits a backoff token n-gram model on the training target outputs, and runs verification sweeps over a range of draft lengths
- Mean accepted tokens/verify: The average number of drafted tokens accepted by the target model per step.
- Tokens/verify: The average progress made per verification step (including the target model's correction token).
- Accepted fraction: The total proportion of drafted tokens that were successfully accepted.
- Records may include
source_idorsession_id; related records are grouped on the same side of the deterministic train/test split to prevent leakage. - Exact target-token preservation: Ensures the simulated speculative output matches the target text exactly.
-
Best K: The draft length
$K$ that maximizes throughput (Tokens/verify).
Generate on-policy target model completions for speculative evaluation using the CLI:
gvd generate-on-policy <path-to-index> <path-to-queries.jsonl> <path-to-output-on-policy.jsonl> \
[--model <model-name>] [--api-base <api-base-url>] [--timeout 180] \
[--max-evidence-chars 24000] [--max-tokens 1024] [--resume]- Target Endpoint: Configured via the
--api-baseflag or theGVD_TARGET_API_BASEenvironment variable (default:http://localhost:22343/v1). - Target Model: Configured via the
--modelflag or theGVD_TARGET_MODELenvironment variable (default:target-model). - Authorization: Configured via the
GVD_TARGET_API_KEYenvironment variable. API keys are handled securely and never stored or logged. - Labeled
relevant_doc_idsare used as gold evidence by default, avoiding retrieval noise and embedding API credentials during target distillation. Pass--retrieve-evidencewhen intentionally generating from production retrieval. - Timeout: Per-completion timeout in seconds; local first-token latency can be high while the model warms up.
- Evidence limit: Bounds prompts so retrieval context plus generation fits the target's context window.
- Resume: Appends only unfinished
source_idrecords, preserving completed target calls across interruptions.
The generator runs GVD queries to retrieve relevant context documents, builds ordinary RAG prompts, executes greedy deterministic target completions (temperature = 0), and writes output records matching the {source_id, prompt, target_text, target_model, evidence_ids} schema directly.
The reproducible ingestion script downloads William James Sidis's The Animate and the Inanimate and Project Gutenberg ebook 1184, Alexandre Dumas's The Count of Monte Cristo, then produces chapter-bounded overlapping JSONL chunks:
pip install -e '.[books]'
python scripts/ingest_books.py
gvd build data/processed/corpus.jsonl data/processed/books-index \
--pca-dimensions 64 --outlier-percentile 99 --community-probes 32
gvd evaluate data/processed/books-index data/processed/labeled_queries.jsonl \
--output data/processed/books-evaluation.jsonEvery record preserves title, author, chapter, chunk index, work ID, and source URL. The included labels test retrieval, not factual correctness of either work.
Train the vector-to-text alignment projection adapter on GVD index documents using the CLI:
gvd train-vector-drafter <path-to-index> <path-to-output-dir> \
[--model-name <hf-model>] [--epochs 3] [--batch-size 4] \
[--lr 1e-4] [--gradient-accumulation-steps 1] [--prefix-tokens 8] \
[--seed 42] [--mock-lm]- Data Filtering: Exclusively filters and trains on The Count of Monte Cristo documents from the index (
metadata.work_id == dumas-monte-cristoormetadata.title == "The Count of Monte Cristo"). - Leakage Prevention: Deterministically splits documents into train/validation sets grouped by
chaptervalue, preventing adjacent overlapping chunks from leaking across splits. - Model Freezing: Freezes the target Hugging Face Causal LM parameters completely and only optimizes the
DenseAlignmentProjectionmodule. - Completion-Only Causal Loss: Standard causal loss calculated only over the target sequence tokens (prefix coordinates and padding are masked using
-100label values). - Checkpoint & Resume: Supports saving state checkpoint files (
projection_checkpoint.pt) and resuming training metrics smoothly if interrupted. - Mock Mode: Pass the
--mock-lmflag to run the entire training sequence offline using a small, simulated PyTorch model and tokenizer without downloading any Hugging Face models.
Evaluate the best checkpoint on deterministic held-out chapters before deployment:
python scripts/evaluate_vector_drafter.py \
data/processed/books-index data/processed/dumas-vector-drafter-final \
data/processed/dumas-vector-drafter-evaluation.jsonThe evaluator records generated samples, lexical overlap, repetition, and a fail-closed quality gate. A checkpoint that fails the gate must not replace the grounded extractive drafter.
After producing a safe embedding NPZ with crossmodal-evaluate, measure geometry,
neighborhood, semantic-decoding, quantization, noise, PCA, and native MRL fidelity:
gvd fidelity-evaluate data/multimodal-cifar10/fixtures.json \
data/processed/cifar10-embeddings.npz \
data/processed/cifar10-fidelity.jsonThe split is stratified within every label/modality group. PCA is fitted only on the training portion. Reports include Wilson intervals, cosine distortion, top-1/5/10 neighbor retention, prediction agreement, residual outlier rates, float16/int8 quantization, and Gaussian-noise stress curves.
See the Lume integration analysis for the boundary between Prism's research harness and Lume's Rust retrieval/runtime layer.
The 10K fidelity report documents the scalable blocked evaluation, corpus provenance, measured results, and limitations.
- PCA outliers are linear-subspace residuals, not necessarily semantic anomalies. Tune the percentile on held-out routing data, and consider a minimum outlier count.
- The built-in graph label propagation is dependency-free, not Leiden. For large corpora, replace the exact similarity matrix with FAISS/HNSW and Leiden/Infomap.
- A centroid alone loses substantial information. The drafter receives centroid coordinates plus retrieved member evidence; do not generate from a centroid alone.
- The local verifier is a conservative lexical check, not factual proof. A target LLM can perform grounded critique, but cannot guarantee zero hallucinations.
- True speculative decoding requires compatible draft/target tokenizers and an inference engine that verifies token probabilities. Text post-validation is a different mechanism and does not deliver speculative-decoding latency claims.
- Saved indexes use JSON + NPZ and rebuild the deterministic graph on load. Never load untrusted model weights without reviewing their serialization format.
embeddings.py: Gemini Embedding 2 retrieval prefixes, MRL dimensions, local fallbackpca.py: compression, inverse reconstruction, residual thresholdsparse.py: isolated BM25 indexgraph.py: kNN topology, communities, centroids, dense searchevaluation.py: end-to-end retrieval metrics, clustering purity/NMI/ARI, seed stability, PCA ablation, and outlier prec/rec/F1speculative.py: backoff n-gram drafter and speculative verification simulation sweepgenerator.py: on-policy target corpus generation via GVD query retrieval and OpenAI completionstrain_cli.py: chapter-split dataset preparation and frozen-LM alignment projection training loopdrafter.py: extractive baseline and PyTorch/Hugging Face vector-prefix adaptertraining.py: frozen-core projection training primitiveverifier.py: grounded acceptance/fallback contractpipeline.py: build, route, query, save, and loadcli.py: command-line orchestration