6 releases

0.3.3 Aug 18, 2024
0.3.2 Aug 14, 2024
0.2.1 Feb 28, 2024
0.1.0 Dec 16, 2023

#1058 in Machine learning

Download history 18/week @ 2024-08-01 192/week @ 2024-08-08 210/week @ 2024-08-15 29/week @ 2024-08-22 25/week @ 2024-08-29 5/week @ 2024-09-05 125/week @ 2024-09-12 189/week @ 2024-09-19 139/week @ 2024-09-26 86/week @ 2024-10-03 47/week @ 2024-10-10 78/week @ 2024-10-17 52/week @ 2024-10-24 56/week @ 2024-10-31 28/week @ 2024-11-07 29/week @ 2024-11-14

176 downloads per month
Used in 5 crates (2 directly)

MIT/Apache

180KB
3.5K SLoC

rbert

A Rust wrapper for bert sentence transformers implemented in Candle

Usage

use kalosm_language_model::Embedder;
use rbert::*;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut bert = Bert::new().await?;
    let sentences = [
        "Cats are cool",
        "The geopolitical situation is dire",
        "Pets are great",
        "Napoleon was a tyrant",
        "Napoleon was a great general",
    ];
    let embeddings = bert.embed_batch(sentences).await?;
    println!("embeddings {:?}", embeddings);

    // Find the cosine similarity between the first two sentences
    let mut similarities = vec![];
    let n_sentences = sentences.len();
    for (i, e_i) in embeddings.iter().enumerate() {
        for j in (i + 1)..n_sentences {
            let e_j = embeddings.get(j).unwrap();
            let cosine_similarity = e_j.cosine_similarity(e_i);
            similarities.push((cosine_similarity, i, j))
        }
    }
    similarities.sort_by(|u, v| v.0.total_cmp(&u.0));
    for &(score, i, j) in similarities.iter() {
        println!("score: {score:.2} '{}' '{}'", sentences[i], sentences[j])
    }

    Ok(())
}

Dependencies

~35–59MB
~1M SLoC