use std::path::PathBuf;
use crate::constants;
use crate::models::ModelManager;
pub const DEFAULT_MODEL: &str = "sentence-transformers/all-MiniLM-L6-v2";
pub const DEFAULT_EMBEDDING_DIMENSIONS: usize = 384;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct EmbeddingConfig {
pub model_name: String,
pub cache_dir: PathBuf,
pub batch_size: usize,
pub fastembed_batch_size: usize,
pub gguf_batch_size: usize,
pub huggingface_batch_size: usize,
pub cache_size: usize,
pub resource_sampling_interval_ms: u64,
pub embedding_dimensions: usize,
pub batch_size_warning_threshold: usize,
}
impl Default for EmbeddingConfig {
fn default() -> Self {
Self {
model_name: DEFAULT_MODEL.to_string(),
cache_dir: dirs::home_dir()
.map(|p| p.join(".turboprop").join("models"))
.unwrap_or_else(|| PathBuf::from(".turboprop/models")),
batch_size: 32,
fastembed_batch_size: 32,
gguf_batch_size: 8,
huggingface_batch_size: 16,
cache_size: 1000,
resource_sampling_interval_ms: 1000, embedding_dimensions: DEFAULT_EMBEDDING_DIMENSIONS,
batch_size_warning_threshold: constants::text::BATCH_SIZE_WARNING_THRESHOLD,
}
}
}
impl EmbeddingConfig {
pub fn with_model(model_name: impl Into<String>) -> Self {
let model_name = model_name.into();
let model_manager = ModelManager::default();
let embedding_dimensions = model_manager
.get_available_models()
.iter()
.find(|model| model.name.as_str() == model_name)
.map(|model| model.dimensions)
.unwrap_or(DEFAULT_EMBEDDING_DIMENSIONS);
Self {
model_name,
embedding_dimensions,
..Default::default()
}
}
pub fn with_cache_dir(mut self, cache_dir: impl Into<PathBuf>) -> Self {
self.cache_dir = cache_dir.into();
self
}
pub fn with_batch_size(mut self, batch_size: usize) -> Self {
self.batch_size = batch_size;
self
}
pub fn with_embedding_dimensions(mut self, dimensions: usize) -> Self {
self.embedding_dimensions = dimensions;
self
}
pub fn with_batch_size_warning_threshold(mut self, threshold: usize) -> Self {
self.batch_size_warning_threshold = threshold;
self
}
}
#[derive(Debug, Clone)]
pub struct EmbeddingOptions {
pub instruction: Option<String>,
pub normalize: bool,
pub max_length: Option<usize>,
}
impl Default for EmbeddingOptions {
fn default() -> Self {
Self {
instruction: None,
normalize: true,
max_length: None,
}
}
}
impl EmbeddingOptions {
pub fn with_instruction(instruction: impl Into<String>) -> Self {
Self {
instruction: Some(instruction.into()),
..Default::default()
}
}
pub fn without_normalization() -> Self {
Self {
normalize: false,
..Default::default()
}
}
pub fn with_max_length(max_length: usize) -> Self {
Self {
max_length: Some(max_length),
..Default::default()
}
}
}