use anyhow::Result;
use async_trait::async_trait;
use super::EmbeddingService;
const DIM: usize = 256;
#[derive(Default)]
pub struct LocalEmbedding;
impl LocalEmbedding {
pub fn new() -> Self {
Self
}
}
#[async_trait]
impl EmbeddingService for LocalEmbedding {
async fn embed(&self, text: &str) -> Result<Vec<f32>> {
let mut vector = vec![0.0f32; DIM];
for token in text
.to_lowercase()
.split(|c: char| !c.is_alphanumeric())
.filter(|t| !t.is_empty())
{
let index = (fnv1a(token) as usize) % DIM;
vector[index] += 1.0;
}
Ok(vector)
}
}
fn fnv1a(s: &str) -> u64 {
let mut hash: u64 = 0xcbf29ce484222325;
for byte in s.bytes() {
hash ^= byte as u64;
hash = hash.wrapping_mul(0x100000001b3);
}
hash
}