xz-rag
Multi-channel Retrieval-Augmented Generation engine for the 小竹 AI ecosystem.
Build composable RAG pipelines with multiple retrieval channels, RRF fusion, query preprocessing (HYDE), optional reranking, and LLM generation.
Architecture
xz-rag coordinates a pluggable retrieval pipeline:
Query → [Preprocessing] → Channel Executors → [Normalize] → [RRF Fusion] → [Rerank] → [Context Build] → [Generate]
│
┌──────────────────┼──────────────────┐
Semantic BM25 Metadata Graph
(vector search) (full-text via (structured (knowledge
tantivy) filters) graph)
Channels
| Channel | Feature | Backend | Purpose |
|---|---|---|---|
| Semantic | default | any Embedder + SemanticSearch impl |
Dense vector similarity search |
| BM25 | bm25 |
tantivy | Sparse keyword retrieval |
| Metadata | default | any MetadataStore impl |
Structured field filtering |
| Graph | default | any KnowledgeGraphSearch impl |
Entity-relation traversal |
Pipeline stages
- Query preprocessing -- HYDE (hypothetical document generation) or query expansion, both powered by
xz-provider(featurehyde). - Multi-channel retrieval -- each channel runs independently, configured with per-channel weight and
top_k. - Score normalization -- min-max normalization equalizes scores across heterogeneous channels.
- RRF fusion -- reciprocal rank fusion (
RRFusion) merges results by rank position, smoothing out score distribution differences. - Reranking -- optional cross-encoder reranking via
xz-rerank(featurererank) with Cohere/Jina support. - Context assembly -- token-budget-aware context window with configurable citation formatting.
- Generation -- optional LLM response via
xz-provider(featurellm-generation), with streaming support.
Features
| Feature | Default | Description |
|---|---|---|
bm25 |
no | BM25 full-text search via tantivy |
rerank |
no | Cross-encoder reranking via xz-rerank |
hyde |
no | HYDE query expansion and query variation generation |
llm-generation |
no | LLM integration via xz-provider for retrieve_and_generate |
caching |
no | In-memory result caching via moka |
query-expansion |
no | Alias for hyde (same provider dep) |
Usage
Basic retrieval with DefaultRagEngine
use Arc;
use ;
// Provide your embedder and vector store implementations
let embedder: = new;
let store: = new;
let engine = builder
.embedder
.semantic_store
.pipeline
.build;
let request = builder
.channels
.top_k
.build;
let result = engine.retrieve.await?;
println!;
With HYDE and reranking
Enable features in Cargo.toml:
[]
= { = "0.1", = ["hyde", "rerank", "llm-generation"] }
use ;
let request = builder
.channels
.query_preprocessing
.top_k
.build;
// engine built with .reranker(...) and .provider(...)
let response = engine.retrieve_and_generate.await?;
println!;
YAML/JSON configuration
engine:
name: "my-rag"
channels:
semantic:
weight: 0.5
top_k: 10
min_score: 0.1
bm25:
weight: 0.3
top_k: 8
metadata:
weight: 0.2
top_k: 5
fusion:
algorithm: "rrf"
rrf_k: 60
normalize_scores: true
reranking:
enabled: true
context:
max_context_tokens: 4096
citation_format: "numeric"
Load via RagConfig::from_file() and build the engine:
use RagConfig;
let config: RagConfig = from_str?;
let engine = config.build_engine?;
Streaming generation
use StreamExt;
use RagStreamEvent;
let mut stream = engine.retrieve_and_generate_stream.await?;
while let Some = stream.next.await
Modules
engine--DefaultRagEngineand its builderchannels-- per-retriever channel executors (semantic, bm25, metadata, graph)pipeline-- channel orchestration, RRF fusion, score normalizationcontext-- token budget management and citation formattingpreprocessing-- HYDE and query expansiongeneration-- LLM response generation viaxz-providerindexing-- document chunking (fixed, recursive, separator-based)types-- shared types (configs, requests, responses, chunk metadata)cache-- optional in-memory result cachingerror-- typed RAG errors
Crate features
All optional components are feature-gated. The default crate is lightweight with only semantic and metadata channels. Turn on bm25, rerank, hyde, llm-generation, or caching as needed.
License
MIT OR Apache-2.0