Trait Embedder

Source
pub trait Embedder:
    Send
    + Sync
    + 'static {
    type Error: Send + Sync + 'static;

    // Required method
    fn embed_for(
        &self,
        input: EmbeddingInput,
    ) -> impl Future<Output = Result<Embedding, Self::Error>> + Send;

    // Provided methods
    fn embed_string(
        &self,
        input: String,
    ) -> impl Future<Output = Result<Embedding, Self::Error>> + Send { ... }
    fn embed_vec(
        &self,
        inputs: Vec<String>,
    ) -> impl Future<Output = Result<Vec<Embedding>, Self::Error>> + Send { ... }
    fn embed_vec_for(
        &self,
        inputs: Vec<EmbeddingInput>,
    ) -> impl Future<Output = Result<Vec<Embedding>, Self::Error>> + Send { ... }
}
Expand description

A model that can be used to embed text. This trait is generic over the vector space that the model uses to help keep track of what embeddings came from which model.

§Example

use kalosm::language::*;
use kalosm_language_model::Embedder;

#[tokio::main]
async fn main() {
    // Bert implements Embedder
    let mut bert = Bert::new().await.unwrap();
    let sentences = [
        "Cats are cool",
        "The geopolitical situation is dire",
        "Pets are great",
        "Napoleon was a tyrant",
        "Napoleon was a great general",
    ];
    // Embed a batch of documents into the bert vector space
    let embeddings = bert.embed_batch(sentences).await.unwrap();
    println!("embeddings {:?}", embeddings);
}

Required Associated Types§

Source

type Error: Send + Sync + 'static

The error type that can occur when embedding a string.

Required Methods§

Source

fn embed_for( &self, input: EmbeddingInput, ) -> impl Future<Output = Result<Embedding, Self::Error>> + Send

Embed a EmbeddingInput into a vector space

Provided Methods§

Source

fn embed_string( &self, input: String, ) -> impl Future<Output = Result<Embedding, Self::Error>> + Send

Embed some text into a vector space.

Source

fn embed_vec( &self, inputs: Vec<String>, ) -> impl Future<Output = Result<Vec<Embedding>, Self::Error>> + Send

Embed a Vec<String> into a vector space. Returns a list of embeddings in the same order as the inputs.

Source

fn embed_vec_for( &self, inputs: Vec<EmbeddingInput>, ) -> impl Future<Output = Result<Vec<Embedding>, Self::Error>> + Send

Embed a Vec<String> into a vector space. Returns a list of embeddings in the same order as the inputs.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<E: Embedder> Embedder for Arc<E>

Source§

type Error = <E as Embedder>::Error

Source§

fn embed_for( &self, input: EmbeddingInput, ) -> impl Future<Output = Result<Embedding, Self::Error>> + Send

Source§

fn embed_string( &self, input: String, ) -> impl Future<Output = Result<Embedding, Self::Error>> + Send

Source§

fn embed_vec( &self, inputs: Vec<String>, ) -> impl Future<Output = Result<Vec<Embedding>, Self::Error>> + Send

Source§

fn embed_vec_for( &self, inputs: Vec<EmbeddingInput>, ) -> impl Future<Output = Result<Vec<Embedding>, Self::Error>> + Send

Implementors§