chore: fix clippy warnings, add embedding/omni_patch, update telemetry, and ignore temp files
This commit is contained in:
1 parent
495304f3fa
commit
98a1716ac8
23 files changed
+2253
-75
No files matched your search
@@ -0,0 +1,42 @@
|
||||
#[allow(deprecated)]
|
||||
use fastembed::{TextEmbedding, EmbeddingModel};
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::Mutex;
|
||||
|
||||
static EMBEDDING_MODEL: OnceLock<Mutex<TextEmbedding>> = OnceLock::new();
|
||||
|
||||
#[allow(deprecated)]
|
||||
pub fn get_embedding_model() -> Result<&'static Mutex<TextEmbedding>, String> {
|
||||
if let Some(model) = EMBEDDING_MODEL.get() {
|
||||
return Ok(model);
|
||||
}
|
||||
let options = fastembed::InitOptions::new(EmbeddingModel::AllMiniLML6V2)
|
||||
.with_show_download_progress(true);
|
||||
|
||||
let model = TextEmbedding::try_new(options).map_err(|e| e.to_string())?;
|
||||
let _ = EMBEDDING_MODEL.set(Mutex::new(model));
|
||||
Ok(EMBEDDING_MODEL.get().unwrap())
|
||||
}
|
||||
|
||||
pub async fn generate_embedding_async(text: String) -> Result<Vec<f32>, String> {
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let model_mutex = get_embedding_model()?;
|
||||
let mut model = model_mutex.lock().map_err(|e| e.to_string())?;
|
||||
let embeddings = model.embed(vec![text], None).map_err(|e| e.to_string())?;
|
||||
Ok(embeddings.into_iter().next().unwrap_or_default())
|
||||
}).await.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
|
||||
if a.is_empty() || b.is_empty() || a.len() != b.len() {
|
||||
return 0.0;
|
||||
}
|
||||
let dot_product: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
|
||||
let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
|
||||
let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();
|
||||
if norm_a == 0.0 || norm_b == 0.0 {
|
||||
0.0
|
||||
} else {
|
||||
dot_product / (norm_a * norm_b)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user