reflex/embedding/sinter/
config.rs1use std::path::PathBuf;
2
3use crate::embedding::error::EmbeddingError;
4
5pub const SINTER_EMBEDDING_DIM: usize = crate::constants::DEFAULT_EMBEDDING_DIM;
7
8pub const SINTER_MAX_SEQ_LEN: usize = crate::constants::DEFAULT_MAX_SEQ_LEN;
10
11#[derive(Debug, Clone)]
12pub struct SinterConfig {
14 pub model_path: PathBuf,
16 pub tokenizer_path: PathBuf,
18 pub max_seq_len: usize,
20 pub embedding_dim: usize,
22 pub testing_stub: bool,
24}
25
26impl Default for SinterConfig {
27 fn default() -> Self {
28 Self {
29 model_path: PathBuf::new(),
30 tokenizer_path: PathBuf::new(),
31 max_seq_len: SINTER_MAX_SEQ_LEN,
32 embedding_dim: SINTER_EMBEDDING_DIM,
33 testing_stub: false,
34 }
35 }
36}
37
38impl SinterConfig {
39 pub const ENV_MODEL_PATH: &'static str = "REFLEX_MODEL_PATH";
41 pub const ENV_TOKENIZER_PATH: &'static str = "REFLEX_TOKENIZER_PATH";
43
44 pub fn from_env() -> Result<Self, EmbeddingError> {
46 let model_path = std::env::var(Self::ENV_MODEL_PATH)
47 .ok()
48 .map(|v| v.trim().to_string())
49 .filter(|v| !v.is_empty())
50 .map(PathBuf::from)
51 .unwrap_or_default();
52
53 let tokenizer_path = std::env::var(Self::ENV_TOKENIZER_PATH)
54 .ok()
55 .map(|v| v.trim().to_string())
56 .filter(|v| !v.is_empty())
57 .map(PathBuf::from)
58 .unwrap_or_else(|| {
59 if !model_path.as_os_str().is_empty() {
60 let parent = model_path.parent().unwrap_or(model_path.as_path());
61 parent.join("tokenizer.json")
62 } else {
63 PathBuf::new()
64 }
65 });
66
67 Ok(Self {
68 model_path,
69 tokenizer_path,
70 ..Default::default()
71 })
72 }
73
74 pub fn new<P: Into<PathBuf>>(model_path: P) -> Self {
76 let model_path = model_path.into();
77 let tokenizer_path = model_path
78 .parent()
79 .map(|p| p.join("tokenizer.json"))
80 .unwrap_or_default();
81
82 Self {
83 model_path,
84 tokenizer_path,
85 ..Default::default()
86 }
87 }
88
89 pub fn stub() -> Self {
91 Self {
92 testing_stub: true,
93 ..Default::default()
94 }
95 }
96
97 pub fn validate(&self) -> Result<(), EmbeddingError> {
99 if self.testing_stub {
100 return Ok(());
101 }
102
103 if self.model_path.as_os_str().is_empty() {
104 return Err(EmbeddingError::InvalidConfig {
105 reason: "model_path is required (stubbing is disabled)".to_string(),
106 });
107 }
108
109 if !self.model_path.exists() {
110 return Err(EmbeddingError::ModelNotFound {
111 path: self.model_path.clone(),
112 });
113 }
114
115 Ok(())
116 }
117
118 pub fn model_available(&self) -> bool {
120 !self.model_path.as_os_str().is_empty() && self.model_path.exists()
121 }
122
123 pub fn tokenizer_available(&self) -> bool {
125 !self.tokenizer_path.as_os_str().is_empty() && self.tokenizer_path.exists()
126 }
127}