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

#1089 in Machine learning

Download history 33/week @ 2024-08-26 7/week @ 2024-09-02 5/week @ 2024-09-09 231/week @ 2024-09-16 150/week @ 2024-09-23 115/week @ 2024-09-30 58/week @ 2024-10-07 86/week @ 2024-10-14 38/week @ 2024-10-21 69/week @ 2024-10-28 37/week @ 2024-11-04 34/week @ 2024-11-11 62/week @ 2024-11-18 46/week @ 2024-11-25 75/week @ 2024-12-02 136/week @ 2024-12-09

325 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