rexis_rag/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![warn(missing_docs)]
4#![deny(rustdoc::broken_intra_doc_links)]
5#![warn(clippy::all)]
6#![warn(clippy::pedantic)]
7#![allow(clippy::module_name_repetitions)]
8#![allow(clippy::too_many_arguments)]
9#![allow(clippy::must_use_candidate)]
10#![allow(dead_code)]
11
12//! # RRAG - Enterprise-Grade Rust RAG Framework
13//!
14//! [![Crates.io](https://img.shields.io/crates/v/rrag.svg)](https://crates.io/crates/rrag)
15//! [![Documentation](https://docs.rs/rrag/badge.svg)](https://docs.rs/rrag)
16//! [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
17//! [![Build Status](https://img.shields.io/github/actions/workflow/status/yourusername/rrag/ci.yml?branch=main)](https://github.com/yourusername/rrag/actions)
18//!
19//! **RRAG** (Rust RAG) is a comprehensive, high-performance framework for building
20//! production-ready Retrieval-Augmented Generation (RAG) applications in Rust.
21//!
22//! Designed for enterprise deployments requiring extreme performance, reliability, and
23//! observability, RRAG provides everything needed to build, deploy, and maintain
24//! sophisticated RAG systems at scale.
25//!
26//! ## πŸš€ Key Features
27//!
28//! - **πŸ”₯ Native Performance**: Zero-cost abstractions with compile-time optimizations
29//! - **πŸ›‘οΈ Memory Safe**: Leverage Rust's ownership system for bulletproof memory management
30//! - **⚑ Async First**: Built on Tokio for high-concurrency workloads
31//! - **🎯 Type Safe**: Compile-time guarantees prevent runtime errors
32//! - **πŸ”Œ Pluggable**: Modular architecture with swappable components
33//! - **🌊 Streaming**: Real-time token streaming with async iterators
34//! - **πŸ“Š Observable**: Built-in metrics, tracing, and health checks
35//!
36//! ## πŸ—οΈ Architecture Overview
37//!
38//! RRAG follows a modular, pipeline-based architecture that maximizes performance
39//! while maintaining flexibility:
40//!
41//! ```text
42//! β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
43//! β”‚   Documents     │───▢│   Processing    │───▢│   Vector Store  β”‚
44//! β”‚   (Input)       β”‚    β”‚   Pipeline      β”‚    β”‚   (Storage)     β”‚
45//! β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
46//!                                 β”‚
47//!                                 β–Ό
48//! β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
49//! β”‚   Responses     │◀───│     Agent       │◀───│    Retriever    β”‚
50//! β”‚   (Output)      β”‚    β”‚   (rsllm)       β”‚    β”‚   (Search)      β”‚
51//! β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
52//! ```
53//!
54//! ## πŸš€ Quick Start
55//!
56//! ### Basic RAG System
57//!
58//! ```rust
59//! use rrag::prelude::*;
60//!
61//! #[tokio::main]
62//! async fn main() -> RragResult<()> {
63//!     // Create a RAG system
64//!     let rag = RragSystemBuilder::new()
65//!         .with_name("My RAG System")
66//!         .with_environment("development")
67//!         .build()
68//!         .await?;
69//!     
70//!     // Add documents
71//!     let doc = Document::new("Rust is a systems programming language...")
72//!         .with_metadata("source", "documentation".into())
73//!         .with_content_hash();
74//!     
75//!     rag.process_document(doc).await?;
76//!     
77//!     // Search for relevant content
78//!     let results = rag.search("What is Rust?".to_string(), Some(5)).await?;
79//!     tracing::debug!("Found {} results", results.total_results);
80//!     
81//!     Ok(())
82//! }
83//! ```
84//!
85//! ### Advanced Agent with Tools
86//!
87//! ```rust
88//! use rrag::prelude::*;
89//!
90//! #[tokio::main]
91//! async fn main() -> RragResult<()> {
92//!     // Create an agent with tools
93//!     let mut agent = AgentBuilder::new()
94//!         .with_model("gpt-4")
95//!         .with_temperature(0.7)
96//!         .with_streaming(true)
97//!         .build()
98//!         .await?;
99//!     
100//!     // Register tools
101//!     agent.register_tool(Calculator::new())?;
102//!     # #[cfg(feature = "http")]
103//!     agent.register_tool(HttpTool::new())?;
104//!     
105//!     // Chat with memory
106//!     let memory = ConversationBufferMemory::new(100);
107//!     let response = agent.chat_with_memory(
108//!         "Calculate 15 * 23",
109//!         &memory
110//!     ).await?;
111//!     
112//!     tracing::debug!("Agent: {}", response.text);
113//!     
114//!     Ok(())
115//! }
116//! ```
117//!
118//! ## πŸ“¦ Feature Flags
119//!
120//! RRAG supports multiple feature flags for flexible deployments:
121//!
122//! - `default`: Core functionality with HTTP and concurrency support
123//! - `rsllm-client`: Integration with rsllm for LLM operations
124//! - `http`: HTTP client support for external services
125//! - `concurrent`: Advanced concurrency features with DashMap
126//! - `observability`: Metrics, monitoring, and alerting
127//! - `security`: Authentication, authorization, and security features
128//! - `security-full`: Complete security suite with 2FA and WebAuthn
129//!
130//! ```toml
131//! [dependencies]
132//! rrag = { version = "0.1", features = ["rsllm-client", "observability", "security"] }
133//! ```
134
135//!
136//! ## πŸ—οΈ Module Organization
137//!
138//! RRAG is organized into focused modules, each handling specific aspects of RAG functionality:
139//!
140//! ### Core Modules
141//!
142//! - [`error`]: Comprehensive error handling with structured error types
143//! - [`document`]: Document processing, chunking, and metadata management
144//! - [`embeddings`]: Multi-provider embedding generation and management
145//! - [`storage`]: Pluggable storage backends for documents and vectors
146//! - [`memory`]: Conversation memory and context management
147//! - [`agent`]: LLM agents with tool calling and streaming support
148//! - [`pipeline`]: Composable processing pipelines
149//! - [`system`]: High-level system orchestration and lifecycle management
150//!
151//! ### Advanced Modules
152//!
153//! - [`retrieval_core`]: Core retrieval interfaces and implementations
154//! - [`retrieval_enhanced`]: Advanced hybrid retrieval with BM25 and semantic search
155//! - [`reranking`]: Result reranking and relevance scoring
156//! - [`evaluation`]: Framework for evaluating RAG system performance
157//! - [`caching`]: Intelligent caching strategies for performance optimization
158//! - [`graph_retrieval`]: Knowledge graph-based retrieval and reasoning
159//! - [`incremental`]: Incremental indexing for large-scale document updates
160//! - [`observability`]: Comprehensive monitoring, metrics, and alerting
161//! - [`tools`]: Built-in and extensible tool implementations
162//! - [`streaming`]: Real-time streaming response handling
163//! - [`query`]: Query processing and optimization
164//! - [`multimodal`]: Multi-modal content processing support
165
166// Core modules - foundational components
167pub mod agent;
168pub mod document;
169pub mod embeddings;
170pub mod error;
171pub mod memory;
172pub mod pipeline;
173pub mod retrieval_core;
174pub mod storage;
175pub mod storage_legacy;
176pub mod streaming;
177pub mod system;
178pub mod tools;
179
180// Enhanced retrieval capabilities
181#[path = "retrieval/mod.rs"]
182pub mod retrieval_enhanced;
183
184// Query processing and optimization
185pub mod query;
186
187// Advanced reranking algorithms
188pub mod reranking;
189
190// Evaluation and benchmarking framework
191pub mod evaluation;
192
193// Intelligent caching layer
194pub mod caching;
195
196// Multi-modal content support
197pub mod multimodal;
198
199// Graph-based knowledge retrieval
200pub mod graph_retrieval;
201
202// Incremental indexing system
203pub mod incremental;
204
205// Observability and monitoring
206pub mod observability;
207
208// Re-exports for convenience
209pub use agent::{
210    Agent, AgentBuilder, AgentConfig, ConversationMemory, ConversationMode, ToolExecutor,
211};
212pub use document::{ChunkingStrategy, Document, DocumentChunk, DocumentChunker, Metadata};
213pub use embeddings::{
214    Embedding, EmbeddingBatch, EmbeddingProvider, EmbeddingRequest, EmbeddingService,
215    LocalEmbeddingProvider, MockEmbeddingService, OpenAIEmbeddingProvider,
216};
217pub use error::{ErrorSeverity, RragError, RragResult};
218pub use memory::{
219    ConversationBufferMemory, ConversationMessage, ConversationSummaryMemory,
220    ConversationTokenBufferMemory, Memory, MemoryService, MessageRole,
221};
222pub use pipeline::{
223    DocumentChunkingStep, EmbeddingStep, Pipeline, PipelineConfig, PipelineContext, PipelineData,
224    PipelineStep, RagPipelineBuilder, RetrievalStep, TextOperation, TextPreprocessingStep,
225};
226pub use retrieval_core::{
227    InMemoryRetriever, RetrievalService, Retriever, SearchAlgorithm, SearchConfig, SearchQuery,
228    SearchResult,
229};
230pub use retrieval_enhanced::{
231    BM25Config, BM25Retriever, FusionStrategy, HybridConfig, HybridRetriever, RankFusion,
232    ReciprocalRankFusion, SemanticConfig, SemanticRetriever, TokenizerType, WeightedFusion,
233};
234pub use storage::{
235    FileStorage, InMemoryStorage, Storage, StorageEntry, StorageKey, StorageQuery, StorageService,
236};
237pub use streaming::{StreamToken, StreamingResponse, TokenStreamBuilder, TokenType};
238pub use system::{
239    ChatResponse, HealthCheckResult, ProcessingResult, RragSystem, RragSystemBuilder,
240    RragSystemConfig, SearchResponse, SystemMetrics,
241};
242#[cfg(feature = "http")]
243pub use tools::HttpTool;
244// Tools module is kept for backward compatibility but agents use rexis_llm::tools now
245pub use tools::{Calculator, EchoTool, Tool, ToolResult};
246
247// rsllm re-exports when feature is enabled
248#[cfg(feature = "rexis-llm-client")]
249pub use rexis_llm;
250
251// Graph retrieval re-exports
252pub use graph_retrieval::{
253    EdgeType, Entity, EntityExtractor, EntityType, ExpansionResult, ExpansionStrategy,
254    GraphAlgorithms, GraphBuildConfig, GraphConfig, GraphConfigBuilder, GraphEdge, GraphIndex,
255    GraphMetrics, GraphNode, GraphQuery, GraphQueryResult, GraphRetrievalBuilder,
256    GraphRetrievalConfig, GraphRetriever, GraphStorage, KnowledgeGraph, NodeType, PageRankConfig,
257    PathFindingConfig, QueryExpander, RelationType, Relationship, TraversalConfig,
258};
259
260// Incremental indexing re-exports
261pub use incremental::{
262    AlertConfig, BatchConfig, BatchExecutor, BatchOperation, BatchProcessingStats, BatchProcessor,
263    BatchResult, ChangeDetectionConfig, ChangeDetector, ChangeResult, ChangeType,
264    ConflictResolution, ConsistencyReport, ContentDelta, DocumentChange, DocumentVersion,
265    EmbeddingUpdate, HealthMetrics, IncrementalIndexManager, IncrementalIndexingService,
266    IncrementalMetrics, IncrementalServiceBuilder, IncrementalServiceConfig, IndexManagerConfig,
267    IndexOperation, IndexUpdate, IndexUpdateStrategy, IndexingStats, IntegrityChecker,
268    IntegrityConfig, IntegrityError, MetricsCollector, MonitoringConfig, OperationLog,
269    PerformanceTracker, QueueManager, RecoveryResult, RollbackConfig, RollbackManager,
270    RollbackOperation, RollbackPoint, UpdateResult, ValidationResult, VectorBatch, VectorOperation,
271    VectorUpdateConfig, VectorUpdateManager, VersionConflict, VersionHistory, VersionManager,
272    VersionResolution, VersioningConfig,
273};
274
275// Observability re-exports
276pub use observability::{
277    AlertCondition, AlertManager, AlertNotification, AlertRule, AlertSeverity, BottleneckAnalysis,
278    ComponentStatus, DashboardConfig, DashboardServer, DataRetention, ExportFormat, ExportManager,
279    HealthChecker, HealthReport, HistoricalAnalyzer, LogAggregator, LogEntry, LogFilter, LogLevel,
280    LogQuery, Metric, MetricType, MetricValue, MetricsCollector as ObsMetricsCollector,
281    MetricsExporter, MetricsRegistry, ObservabilityBuilder, ObservabilityConfig,
282    ObservabilitySystem, PerformanceMonitor, PerformanceReport, ProfileData, Profiler,
283    RealtimeMetrics, ReportGenerator, RetentionPolicy, SearchAnalyzer, SystemMonitor,
284    UserActivityTracker, WebSocketManager,
285};
286
287/// Prelude module for convenient imports
288///
289/// This module re-exports the most commonly used types and traits from RRAG,
290/// making it easy to get started with a single import:
291///
292/// ```rust
293/// use rrag::prelude::*;
294/// ```
295///
296/// The prelude includes:
297/// - Core system components ([`RragSystem`], [`RragSystemBuilder`])
298/// - Document processing ([`Document`], [`DocumentChunk`], [`ChunkingStrategy`])
299/// - Error handling ([`RragError`], [`RragResult`])
300/// - Agents and tools ([`Agent`], [`Tool`], [`Calculator`])
301/// - Memory management ([`Memory`], [`ConversationBufferMemory`])
302/// - Streaming support ([`StreamingResponse`], [`StreamToken`])
303/// - Pipeline processing ([`Pipeline`], [`RagPipelineBuilder`])
304/// - Search functionality ([`SearchResult`], [`SearchQuery`])
305/// - Advanced features (incremental indexing, observability)
306///
307/// ## Feature-Gated Exports
308///
309/// Some exports are only available with specific features:
310/// - `rsllm-client`: [`rsllm`] integration
311/// - `http`: HTTP-based tools like [`HttpTool`]
312///
313/// ## External Dependencies
314///
315/// Common external crates are also re-exported for convenience:
316/// - [`tokio`]: Async runtime
317/// - [`serde`]: Serialization traits
318/// - [`futures`]: Stream processing
319/// - [`async_trait`]: Async trait support
320pub mod prelude {
321    //! Convenient re-exports for common RRAG functionality
322    //!
323    //! Import everything you need with:
324    //! ```rust
325    //! use rrag::prelude::*;
326    //! ```
327
328    // System components
329    pub use crate::{
330        ChatResponse, HealthCheckResult, ProcessingResult, RragSystem, RragSystemBuilder,
331        RragSystemConfig, SearchResponse, SystemMetrics,
332    };
333
334    // Core types and error handling
335    pub use crate::{
336        ChunkingStrategy, Document, DocumentChunk, DocumentChunker, Embedding, EmbeddingProvider,
337        EmbeddingService, ErrorSeverity, Metadata, RragError, RragResult,
338    };
339
340    // Service interfaces
341    pub use crate::{
342        InMemoryRetriever, MemoryService, RetrievalService, SearchConfig, SearchQuery,
343        SearchResult, StorageService,
344    };
345
346    // Agents and tools
347    pub use crate::{
348        Agent, AgentBuilder, AgentConfig, ConversationMemory, ConversationMode, ToolExecutor,
349    };
350
351    // HTTP tools when feature is enabled
352    #[cfg(feature = "http")]
353    pub use crate::HttpTool;
354
355    // Memory and conversations
356    pub use crate::{
357        ConversationBufferMemory, ConversationMessage, ConversationSummaryMemory,
358        ConversationTokenBufferMemory, Memory, MessageRole,
359    };
360
361    // Streaming support
362    pub use crate::{StreamToken, StreamingResponse, TokenStreamBuilder, TokenType};
363
364    // Pipeline processing
365    pub use crate::{
366        DocumentChunkingStep, EmbeddingStep, Pipeline, PipelineContext, PipelineData, PipelineStep,
367        RagPipelineBuilder, RetrievalStep, TextOperation, TextPreprocessingStep,
368    };
369
370    // Enhanced retrieval
371    pub use crate::{
372        BM25Retriever, FusionStrategy, HybridConfig, HybridRetriever, RankFusion,
373        ReciprocalRankFusion, SemanticRetriever,
374    };
375
376    // Incremental indexing
377    pub use crate::{
378        BatchProcessor, ChangeDetector, ChangeResult, ChangeType, IncrementalIndexManager,
379        IncrementalIndexingService, IncrementalServiceBuilder, IntegrityChecker, MetricsCollector,
380        RollbackManager, VectorUpdateManager, VersionManager,
381    };
382
383    // Graph retrieval
384    pub use crate::{
385        EntityExtractor, GraphEdge, GraphNode, GraphRetrievalBuilder, GraphRetriever,
386        KnowledgeGraph, QueryExpander,
387    };
388
389    // Observability
390    pub use crate::{AlertManager, HealthChecker, MetricsRegistry, ObservabilitySystem, Profiler};
391
392    // External dependencies commonly used with RRAG
393    pub use async_trait::async_trait;
394    pub use futures::{Stream, StreamExt};
395    pub use serde::{Deserialize, Serialize};
396    pub use tokio;
397
398    // rsllm integration when feature is enabled
399    #[cfg(feature = "rexis-llm-client")]
400    pub use rexis_llm;
401}
402
403/// Framework constants and metadata
404///
405/// These constants provide version and identification information for the RRAG framework.
406
407/// Current version of the RRAG framework
408///
409/// This version is automatically synchronized with the version in `Cargo.toml`.
410/// Use this for version checking, compatibility testing, or reporting.
411///
412/// # Example
413/// ```rust
414/// use rrag::VERSION;
415/// tracing::debug!("RRAG Framework version: {}", VERSION);
416/// ```
417pub const VERSION: &str = env!("CARGO_PKG_VERSION");
418
419/// Framework name identifier
420///
421/// The canonical name of the framework, used for logging, metrics,
422/// and system identification.
423pub const NAME: &str = "RRAG";
424
425/// Framework description
426///
427/// A brief description of the framework's purpose and capabilities.
428pub const DESCRIPTION: &str =
429    "Rust RAG Framework - High-performance Retrieval-Augmented Generation";
430
431/// Framework repository URL
432///
433/// The primary repository location for source code, issues, and documentation.
434pub const REPOSITORY: &str = "https://github.com/levalhq/rrag";
435
436/// Framework license
437///
438/// The software license under which RRAG is distributed.
439pub const LICENSE: &str = "MIT";
440
441/// Minimum supported Rust version (MSRV)
442///
443/// The minimum version of Rust required to compile and use RRAG.
444pub const MSRV: &str = "1.70.0";