mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 23:36:20 +00:00
feat(search): full-text, vector, hybrid search and auto-link suggestions
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fbcfcf678f
commit
e1e64ca2de
11 files changed
+3372
-5
No files matched your search
@@ -0,0 +1,50 @@
|
||||
//! Deterministic fake embedder for tests, examples and benchmarks.
|
||||
|
||||
use synapse_core::embed::{EmbedError, Embedder};
|
||||
|
||||
/// Bag-of-words hashing embedder: texts sharing words get a high cosine similarity.
|
||||
pub struct HashEmbedder {
|
||||
dims: usize,
|
||||
model: String,
|
||||
}
|
||||
|
||||
impl HashEmbedder {
|
||||
pub fn new(dims: usize) -> Self {
|
||||
Self::with_model(dims, "test/hash")
|
||||
}
|
||||
|
||||
pub fn with_model(dims: usize, model: &str) -> Self {
|
||||
Self {
|
||||
dims,
|
||||
model: model.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn embed_one(&self, text: &str) -> Vec<f32> {
|
||||
let mut v = vec![0f32; self.dims];
|
||||
for word in text
|
||||
.split(|c: char| !c.is_alphanumeric())
|
||||
.filter(|w| !w.is_empty())
|
||||
{
|
||||
// FNV-1a over the lowercased word.
|
||||
let mut h: u64 = 0xcbf29ce484222325;
|
||||
for b in word.to_lowercase().bytes() {
|
||||
h = (h ^ b as u64).wrapping_mul(0x100000001b3);
|
||||
}
|
||||
v[(h % self.dims as u64) as usize] += 1.0;
|
||||
}
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
impl Embedder for HashEmbedder {
|
||||
fn model_id(&self) -> String {
|
||||
self.model.clone()
|
||||
}
|
||||
fn dims(&self) -> usize {
|
||||
self.dims
|
||||
}
|
||||
fn embed(&self, texts: &[String]) -> Result<Vec<Vec<f32>>, EmbedError> {
|
||||
Ok(texts.iter().map(|t| self.embed_one(t)).collect())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user