feat(server): refactor handlers, router, state management, and memory tools
This commit is contained in:
1 parent
87ddb01063
commit
a083719cf1
36 files changed
+1899
-597
No files matched your search
+34
-5
@@ -1,7 +1,7 @@
|
||||
#[allow(deprecated)]
|
||||
use fastembed::{TextEmbedding, EmbeddingModel};
|
||||
use std::sync::OnceLock;
|
||||
use fastembed::{EmbeddingModel, TextEmbedding};
|
||||
use std::sync::Mutex;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
static EMBEDDING_MODEL: OnceLock<Mutex<TextEmbedding>> = OnceLock::new();
|
||||
|
||||
@@ -12,7 +12,7 @@ pub fn get_embedding_model() -> Result<&'static Mutex<TextEmbedding>, String> {
|
||||
}
|
||||
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())
|
||||
@@ -24,7 +24,9 @@ pub async fn generate_embedding_async(text: String) -> Result<Vec<f32>, String>
|
||||
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())?
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
|
||||
@@ -49,5 +51,32 @@ pub async fn generate_embeddings_async(texts: Vec<String>) -> Result<Vec<Vec<f32
|
||||
let mut model = model_mutex.lock().map_err(|e| e.to_string())?;
|
||||
let embeddings = model.embed(texts, None).map_err(|e| e.to_string())?;
|
||||
Ok(embeddings)
|
||||
}).await.map_err(|e| e.to_string())?
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_cosine_similarity_edge_cases() {
|
||||
assert_eq!(cosine_similarity(&[], &[]), 0.0);
|
||||
assert_eq!(cosine_similarity(&[1.0, 2.0], &[1.0]), 0.0);
|
||||
assert_eq!(cosine_similarity(&[0.0, 0.0], &[0.0, 0.0]), 0.0);
|
||||
|
||||
let v1 = vec![1.0, 0.0, 0.0];
|
||||
let v2 = vec![1.0, 0.0, 0.0];
|
||||
assert!((cosine_similarity(&v1, &v2) - 1.0).abs() < 1e-5);
|
||||
|
||||
let v3 = vec![0.0, 1.0, 0.0];
|
||||
assert!((cosine_similarity(&v1, &v3) - 0.0).abs() < 1e-5);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_generate_embeddings_async_empty() {
|
||||
let res = generate_embeddings_async(vec![]).await.unwrap();
|
||||
assert!(res.is_empty());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user