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

#1090 in Machine learning

Download history 5/week @ 2024-09-07 207/week @ 2024-09-14 150/week @ 2024-09-21 117/week @ 2024-09-28 74/week @ 2024-10-05 79/week @ 2024-10-12 45/week @ 2024-10-19 62/week @ 2024-10-26 45/week @ 2024-11-02 31/week @ 2024-11-09 47/week @ 2024-11-16 59/week @ 2024-11-23 48/week @ 2024-11-30 125/week @ 2024-12-07 66/week @ 2024-12-14 27/week @ 2024-12-21

275 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–57MB
~1M SLoC