unai 0.0.1

Common AI abstractions
Documentation
use std::error::Error;

use async_trait::async_trait;

/// A text embedding represented as a vector of f32 values
#[derive(Debug, Clone)]
pub struct TextEmbedding(Vec<f32>);

impl TextEmbedding {
    pub fn new(embedding: Vec<f32>) -> Self {
        Self(embedding)
    }

    pub fn as_slice(&self) -> &[f32] {
        &self.0
    }
}

impl From<Vec<f32>> for TextEmbedding {
    fn from(embedding: Vec<f32>) -> TextEmbedding {
        TextEmbedding::new(embedding)
    }
}

impl From<TextEmbedding> for Vec<f32> {
    fn from(embedding: TextEmbedding) -> Vec<f32> {
        embedding.0
    }
}

pub type EmbedTextResult = Result<TextEmbedding, Box<dyn Error>>;

#[async_trait]
pub trait TextEmbeddingModel: Send + Sync {
    /// Embeds the given text
    async fn embed_text(&self, text: &str) -> EmbedTextResult;
}