| Crates.io | oxirag |
| lib.rs | oxirag |
| version | 0.1.0 |
| created_at | 2026-01-24 07:08:56.524872+00 |
| updated_at | 2026-01-24 07:08:56.524872+00 |
| description | A four-layer RAG engine with SMT-based logic verification and knowledge graph support |
| homepage | https://github.com/cool-japan/oxirag |
| repository | https://github.com/cool-japan/oxirag |
| max_upload_size | |
| id | 2066327 |
| size | 2,273,269 |
A four-layer Retrieval-Augmented Generation (RAG) engine in Pure Rust with SMT-based logic verification and knowledge graph support.
Key Innovations:
OxiRAG provides a robust RAG pipeline with four specialized layers:
Query
│
├───────────────────────────────┐
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ Layer 1: Echo │ │ Layer 4: Graph │
│ (Vector Store) │ │ (GraphRAG) │
└────────┬────────┘ └────────┬────────┘
│ │
└───────────┬─────────────┘
▼
┌─────────────────────┐
│ Layer 2: Speculator │ ← Draft verification with SLM
│ (Draft Checker) │
└─────────┬───────────┘
│
▼
┌─────────────────┐
│ Layer 3: Judge │ ← Logic verification with SMT solver
│ (SMT Verifier) │
└────────┬────────┘
│
▼
Verified Answer
Layer 1 (Echo): Semantic search using vector embeddings
Layer 2 (Speculator): Draft verification
Layer 3 (Judge): SMT-based logic verification
Layer 4 (Graph): Knowledge graph-based retrieval (GraphRAG)
Prefix Caching: Context-aware KV cache management
Distillation: On-the-fly model distillation support
Cross-Platform: Native and WASM support
Add to your Cargo.toml:
[dependencies]
oxirag = "0.1"
| Feature | Description | Default |
|---|---|---|
native |
Enable native runtime with Tokio | Yes |
wasm |
Enable WASM bindings | No |
echo |
Enable Layer 1 (semantic search) | Yes |
speculator |
Enable Layer 2 with Candle SLM | No |
judge |
Enable Layer 3 with OxiZ SMT solver | No |
graphrag |
Enable Layer 4 (knowledge graph) | No |
prefix-cache |
Enable prefix caching for KV cache management | No |
distillation |
Enable distillation tracking and Q&A collection | No |
full |
Enable all features (native only) | No |
cuda |
Enable CUDA acceleration for Candle | No |
metal |
Enable Metal acceleration for Candle | No |
use oxirag::prelude::*;
#[tokio::main]
async fn main() -> Result<(), OxiRagError> {
// Create the Echo layer
let echo = EchoLayer::new(
MockEmbeddingProvider::new(384),
InMemoryVectorStore::new(384),
);
// Create the Speculator layer
let speculator = RuleBasedSpeculator::default();
// Create the Judge layer
let judge = JudgeImpl::new(
AdvancedClaimExtractor::new(),
MockSmtVerifier::default(),
JudgeConfig::default(),
);
// Build the pipeline
let mut pipeline = PipelineBuilder::new()
.with_echo(echo)
.with_speculator(speculator)
.with_judge(judge)
.build()?;
// Index documents
pipeline.index(Document::new("The capital of France is Paris.")).await?;
pipeline.index(Document::new("Paris is known for the Eiffel Tower.")).await?;
// Query the pipeline
let query = Query::new("What is the capital of France?");
let result = pipeline.process(query).await?;
println!("Answer: {}", result.final_answer);
println!("Confidence: {:.2}", result.confidence);
println!("Layers used: {:?}", result.layers_used);
Ok(())
}
The Echo layer handles semantic search:
// Configure embedding provider
let provider = MockEmbeddingProvider::new(384);
// Configure vector store
let store = InMemoryVectorStore::new(384)
.with_metric(SimilarityMetric::Cosine)
.with_max_capacity(10000);
// Create Echo layer
let echo = EchoLayer::new(provider, store);
The Speculator layer verifies draft answers:
// Rule-based (default)
let speculator = RuleBasedSpeculator::default();
// Or with custom config
let config = SpeculatorConfig {
accept_threshold: 0.9,
reject_threshold: 0.3,
..Default::default()
};
let speculator = RuleBasedSpeculator::new(config);
The Judge layer performs formal verification:
let judge = JudgeImpl::new(
AdvancedClaimExtractor::new(),
MockSmtVerifier::default(),
JudgeConfig {
max_claims: 10,
check_consistency: true,
..Default::default()
},
);
The Graph layer enables knowledge graph-based retrieval:
use oxirag::layer4_graph::*;
// Create extractors
let entity_extractor = PatternEntityExtractor::new();
let relationship_extractor = PatternRelationshipExtractor::new();
// Create graph store
let graph_store = InMemoryGraphStore::new();
// Build the GraphLayer
let mut graph_layer = GraphLayerBuilder::new()
.with_entity_extractor(entity_extractor)
.with_relationship_extractor(relationship_extractor)
.with_graph_store(graph_store)
.build()?;
// Index documents (extracts entities and relationships)
graph_layer.index_document(&Document::new("Rust was created by Mozilla.")).await?;
// Query the graph
let query = GraphQuery::new(vec!["Rust".to_string()])
.with_max_hops(2);
let paths = graph_layer.query(&query).await?;
// Find related entities
let related = graph_layer.find_related("Rust", 2).await?;
The Prefix Cache module enables efficient caching of processed document contexts:
use oxirag::prefix_cache::*;
// Create a prefix cache
let config = PrefixCacheConfig::default()
.with_max_entries(1000)
.with_default_ttl_secs(3600);
let mut cache = InMemoryPrefixCache::new(config);
// Generate a fingerprint for context
let generator = ContextFingerprintGenerator::new();
let fingerprint = generator.generate("This is a large document context...");
// Create and store a KV cache entry
let entry = KVCacheEntry::new(fingerprint.clone(), vec![0.1, 0.2, 0.3], 512);
cache.put(entry).await?;
// Retrieve cached entry
if let Some(cached) = cache.get(&fingerprint).await {
println!("Cache hit! Sequence length: {}", cached.sequence_length);
}
// Check cache statistics
let stats = cache.stats();
println!("Hits: {}, Misses: {}", stats.hits, stats.misses);
The Distillation module tracks query patterns for model fine-tuning:
use oxirag::distillation::*;
// Create a distillation tracker
let config = DistillationConfig::default()
.with_min_frequency_threshold(10)
.with_max_qa_pairs_per_pattern(100);
let mut tracker = InMemoryDistillationTracker::new(config);
// Track queries with their answers
tracker.track_query("What is Rust?", Some("Rust is a systems programming language."), 0.95).await?;
tracker.track_query("What is Rust?", Some("Rust focuses on safety and performance."), 0.92).await?;
// Get candidates ready for distillation
let candidates = tracker.get_candidates().await;
for candidate in candidates {
if candidate.ready_for_distillation {
println!("Pattern '{}' has {} Q&A pairs",
candidate.pattern.normalized_text,
candidate.qa_pairs.len());
}
}
// Export training examples for LoRA fine-tuning
let examples = tracker.export_training_examples().await;
Build for WASM:
wasm-pack build --target web --features wasm
Use in JavaScript:
import init, { WasmRagEngine } from './pkg/oxirag.js';
await init();
const engine = new WasmRagEngine(384);
await engine.index("doc-1", "The capital of France is Paris.");
const results = await engine.query("What is the capital of France?", 5);
let config = PipelineConfig {
fast_path_threshold: 0.95, // Skip layers if confidence is high
skip_verification_threshold: 0.9,
enable_fast_path: true,
max_retries: 3,
parallel_execution: false,
max_search_results: 5,
};
let config = OxiRagConfig::from_json(r#"{
"echo": { "dimension": 384 },
"pipeline": { "enable_fast_path": true }
}"#)?;
# Run all tests (1,500 tests)
cargo nextest run --all-features
# Run clippy (no warnings)
cargo clippy --all-features -- -D warnings
# Run rustdoc validation (no warnings)
RUSTDOCFLAGS="-D warnings" cargo doc --all-features --no-deps
# Run doc tests
cargo test --doc --all-features
cargo bench
| Metric | Value |
|---|---|
| Source Files | 91 Rust files |
| Lines of Code | 48,463 |
| Tests | 1,500 |
| Clippy Warnings | 0 |
| Rustdoc Warnings | 0 |
Licensed under either of:
at your option.
See TODO.md for detailed progress on each layer:
| Feature | Progress |
|---|---|
| Speculative RAG | 99% |
| Context-Aware Prefix Caching | 95% |
| On-the-fly Distillation | 85% |
| Hidden States Manipulation | 90% |
OxiRAG is part of the COOLJAPAN Pure Rust ecosystem:
Contributions are welcome! Please feel free to submit a Pull Request.
COOLJAPAN OU (Team Kitasan)