use std::sync::Arc;
use crate::embed::Encoder;
use crate::error::{ModelLoadError, QueryError};
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Model {
encoder: Arc<Encoder>,
}
impl Model {
#[must_use = "loading the model is the expensive step; keep the handle to reuse it"]
pub fn load() -> Result<Self, ModelLoadError> {
Ok(Self {
encoder: Arc::new(Encoder::load()?),
})
}
pub(crate) fn embed(&self, text: &str) -> Result<Vec<f32>, QueryError> {
self.encoder.embed(text)
}
#[cfg(test)]
pub(crate) fn shares_encoder(&self, other: &Model) -> bool {
Arc::ptr_eq(&self.encoder, &other.encoder)
}
}
#[cfg(test)]
mod tests {
use super::Model;
const fn assert_send_sync_static<T: Send + Sync + 'static>() {}
#[test]
fn a_model_is_send_sync_static_and_clone_shares_the_encoder() {
assert_send_sync_static::<Model>();
let model = Model::load().expect("the compiled-in model loads");
let clone = model.clone();
assert!(model.shares_encoder(&clone), "cloning must not reload");
}
}