59 stable releases (4 major)

new 4.8.0 Apr 26, 2025
4.6.0 Feb 28, 2025
4.3.0 Nov 27, 2024
3.14.1 Aug 5, 2024
0.6.0 Oct 2, 2023

#1 in #locally

Download history 1144/week @ 2025-01-08 994/week @ 2025-01-15 825/week @ 2025-01-22 980/week @ 2025-01-29 1259/week @ 2025-02-05 1492/week @ 2025-02-12 1744/week @ 2025-02-19 2091/week @ 2025-02-26 1304/week @ 2025-03-05 1220/week @ 2025-03-12 1388/week @ 2025-03-19 1395/week @ 2025-03-26 1469/week @ 2025-04-02 1508/week @ 2025-04-09 1802/week @ 2025-04-16 1520/week @ 2025-04-23

6,468 downloads per month
Used in 20 crates (14 directly)

Apache-2.0

405KB
2.5K SLoC

FastEmbed-rs πŸ¦€

Rust library for generating vector embeddings, reranking locally!

Crates.io MIT Licensed Semantic release

πŸ• Features

πŸ” Not looking for Rust?

πŸ€– Models

Text Embedding

Sparse Text Embedding

Image Embedding

Reranking

πŸš€ Installation

Run the following command in your project directory:

cargo add fastembed

Or add the following line to your Cargo.toml:

[dependencies]
fastembed = "4"

πŸ“– Usage

Text Embeddings

use fastembed::{TextEmbedding, InitOptions, EmbeddingModel};

// With default InitOptions
let model = TextEmbedding::try_new(Default::default())?;

// With custom InitOptions
let model = TextEmbedding::try_new(
    InitOptions::new(EmbeddingModel::AllMiniLML6V2).with_show_download_progress(true),
)?;

let documents = vec![
    "passage: Hello, World!",
    "query: Hello, World!",
    "passage: This is an example passage.",
    // You can leave out the prefix but it's recommended
    "fastembed-rs is licensed under Apache  2.0"
    ];

 // Generate embeddings with the default batch size, 256
 let embeddings = model.embed(documents, None)?;

 println!("Embeddings length: {}", embeddings.len()); // -> Embeddings length: 4
 println!("Embedding dimension: {}", embeddings[0].len()); // -> Embedding dimension: 384

Image Embeddings

use fastembed::{ImageEmbedding, ImageInitOptions, ImageEmbeddingModel};

// With default InitOptions
let model = ImageEmbedding::try_new(Default::default())?;

// With custom InitOptions
let model = ImageEmbedding::try_new(
    ImageInitOptions::new(ImageEmbeddingModel::ClipVitB32).with_show_download_progress(true),
)?;

let images = vec!["assets/image_0.png", "assets/image_1.png"];

// Generate embeddings with the default batch size, 256
let embeddings = model.embed(images, None)?;

println!("Embeddings length: {}", embeddings.len()); // -> Embeddings length: 2
println!("Embedding dimension: {}", embeddings[0].len()); // -> Embedding dimension: 512

Candidates Reranking

use fastembed::{TextRerank, RerankInitOptions, RerankerModel};

let model = TextRerank::try_new(
    RerankInitOptions::new(RerankerModel::BGERerankerBase).with_show_download_progress(true),
)?;

let documents = vec![
    "hi",
    "The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear, is a bear species endemic to China.",
    "panda is animal",
    "i dont know",
    "kind of mammal",
    ];

// Rerank with the default batch size, 256 and return document contents
let results = model.rerank("what is panda?", documents, true, None)?;
println!("Rerank result: {:?}", results);

Alternatively, local model files can be used for inference via the try_new_from_user_defined(...) methods of respective structs.

✊ Support

To support the library, please donate to our primary upstream dependency, ort - The Rust wrapper for the ONNX runtime.

πŸ“„ LICENSE

Apache 2.0 Β© 2024

Dependencies

~17–33MB
~580K SLoC