1#![allow(clippy::non_canonical_partial_ord_impl)]
7#![allow(clippy::manual_is_multiple_of)]
8#![allow(clippy::manual_div_ceil)]
9#![allow(clippy::derivable_impls)]
10#![allow(clippy::needless_range_loop)]
11#![allow(clippy::unused_enumerate_index)]
12
13pub mod embedding;
14pub mod index;
15pub mod storage;
16pub mod vector;
17
18use thiserror::Error;
19
20pub type Result<T> = std::result::Result<T, RagError>;
22
23#[derive(Debug, Error)]
25pub enum RagError {
26 #[error("Vector dimension mismatch: expected {expected}, got {actual}")]
27 DimensionMismatch { expected: usize, actual: usize },
28
29 #[error("Index error: {0}")]
30 IndexError(String),
31
32 #[error("Embedding error: {0}")]
33 EmbeddingError(String),
34
35 #[error("Storage error: {0}")]
36 StorageError(String),
37
38 #[error("IO error: {0}")]
39 IoError(#[from] std::io::Error),
40
41 #[error("Serialization error: {0}")]
42 SerializationError(#[from] bincode::Error),
43
44 #[error("Compression error: {0}")]
45 CompressionError(#[from] storage::compression::CompressionError),
46}
47
48#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
50pub struct Document {
51 pub id: String,
52 pub content: String,
53 pub embedding: Vec<f32>,
54 pub metadata: Option<serde_json::Value>,
55}
56
57#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
59pub struct SearchResult {
60 pub id: String,
61 pub content: String,
62 pub score: f32,
63 pub metadata: Option<serde_json::Value>,
64}
65
66#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
68pub struct RagConfig {
69 pub embedding_dim: usize,
70 pub max_documents: usize,
71 pub index_type: IndexType,
72}
73
74#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
75pub enum IndexType {
76 Flat,
77 HNSW,
78}
79
80impl Default for RagConfig {
81 fn default() -> Self {
82 Self {
83 embedding_dim: 384, max_documents: 10_000,
85 index_type: IndexType::HNSW,
86 }
87 }
88}
89
90pub use vector::{cosine_similarity, dot_product, l2_distance, normalize};