Skip to main content

fastembed/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2//! [FastEmbed](https://github.com/Anush008/fastembed-rs) - Fast, light, accurate library built for retrieval embedding generation.
3//!
4//! Local ONNX inference, synchronous, no Tokio. Models download once and run offline thereafter.
5//!
6//! # What's here
7//!
8//! - [`TextEmbedding`] - dense text embeddings (default: BGE small en v1.5)
9//! - [`SparseTextEmbedding`] - sparse (SPLADE) embeddings for lexical search
10//! - [`Bgem3Embedding`] - dense + sparse + ColBERT in a single pass (BGE-M3)
11//! - [`ImageEmbedding`] - image embeddings (CLIP, ResNet, ...)
12//! - [`TextRerank`] - cross-encoder reranking of candidates
13//!
14//! Qwen3 and Nomic v2 MoE embeddings are available behind the `qwen3` and
15//! `nomic-v2-moe` feature flags (candle backend).
16//!
17//! # Model cache
18//!
19//! Models download to `./.fastembed_cache` on first use, then load from there.
20//! Override the location with the `FASTEMBED_CACHE_DIR` env var or
21//! [`TextInitOptions`]`::with_cache_dir`. `HF_HOME` takes precedence over both;
22//! set `HF_ENDPOINT` to pull from a mirror.
23//!
24#![cfg_attr(
25    feature = "hf-hub",
26    doc = r#"
27 ### Instantiating [TextEmbedding](crate::TextEmbedding)
28 ```
29 use fastembed::{TextEmbedding, TextInitOptions, EmbeddingModel};
30
31# fn model_demo() -> anyhow::Result<()> {
32 // With default TextInitOptions
33 let model = TextEmbedding::try_new(Default::default())?;
34
35 // List all supported models
36 dbg!(TextEmbedding::list_supported_models());
37
38 // With custom TextInitOptions
39 let model = TextEmbedding::try_new(
40        TextInitOptions::new(EmbeddingModel::AllMiniLML6V2).with_show_download_progress(true),
41 )?;
42 # Ok(())
43 # }
44 ```
45"#
46)]
47//! Find more info about the available options in the [TextInitOptions](crate::TextInitOptions) documentation.
48//!
49#![cfg_attr(
50    feature = "hf-hub",
51    doc = r#"
52 ### Embeddings generation
53```
54# use fastembed::{TextEmbedding, TextInitOptions, EmbeddingModel};
55# fn embedding_demo() -> anyhow::Result<()> {
56# let mut model: TextEmbedding = TextEmbedding::try_new(Default::default())?;
57 let documents = vec![
58    "passage: Hello, World!",
59    "query: Hello, World!",
60    "passage: This is an example passage.",
61    // You can leave out the prefix but it's recommended
62    "fastembed-rs is licensed under Apache 2.0"
63    ];
64
65 // Generate embeddings with the default batch size, 256
66 let embeddings = model.embed(documents, None)?;
67
68 println!("Embeddings length: {}", embeddings.len()); // -> Embeddings length: 4
69 # Ok(())
70 # }
71 ```
72"#
73)]
74
75mod common;
76
77mod bgem3_embedding;
78#[cfg(feature = "image-models")]
79mod image_embedding;
80mod init;
81mod models;
82pub mod output;
83mod pooling;
84mod reranking;
85pub mod similarity;
86mod sparse_text_embedding;
87mod text_embedding;
88
89pub use ort::execution_providers::ExecutionProviderDispatch;
90
91pub use crate::common::{get_cache_dir, Embedding, Error, SparseEmbedding, TokenizerFiles};
92pub use crate::models::{
93    model_info::ModelInfo, model_info::RerankerModelInfo, quantization::QuantizationMode,
94};
95pub use crate::output::{EmbeddingOutput, OutputKey, OutputPrecedence, SingleBatchOutput};
96pub use crate::pooling::Pooling;
97
98// For all Embedding
99pub use crate::init::{InitOptions as BaseInitOptions, InitOptionsWithLength};
100pub use crate::models::ModelTrait;
101
102// For Text Embedding
103pub use crate::models::text_embedding::EmbeddingModel;
104#[deprecated(note = "use `TextInitOptions` instead")]
105pub use crate::text_embedding::TextInitOptions as InitOptions;
106pub use crate::text_embedding::{
107    InitOptionsUserDefined, TextEmbedding, TextInitOptions, UserDefinedEmbeddingModel,
108};
109
110// For Sparse Text Embedding
111pub use crate::models::sparse::SparseModel;
112pub use crate::sparse_text_embedding::{
113    SparseInitOptions, SparseTextEmbedding, UserDefinedSparseModel,
114};
115
116// For BGEM3 Joint Embedding
117pub use crate::bgem3_embedding::{
118    Bgem3Embedding, Bgem3EmbeddingOutput, Bgem3InitOptions, UserDefinedBgem3Model,
119};
120pub use crate::models::bgem3::Bgem3Model;
121
122// For Image Embedding
123#[cfg(feature = "image-models")]
124pub use crate::image_embedding::{
125    ImageEmbedding, ImageInitOptions, ImageInitOptionsUserDefined, UserDefinedImageEmbeddingModel,
126};
127pub use crate::models::image_embedding::ImageEmbeddingModel;
128
129// For Reranking
130pub use crate::models::reranking::RerankerModel;
131pub use crate::reranking::{
132    OnnxSource, RerankInitOptions, RerankInitOptionsUserDefined, RerankResult, TextRerank,
133    UserDefinedRerankingModel,
134};
135
136// For Qwen3 (candle backend)
137#[cfg(feature = "qwen3")]
138pub use crate::models::qwen3::{
139    Config as Qwen3Config, Qwen3Model, Qwen3TextEmbedding, Qwen3VLEmbedding,
140};
141
142// For Nomic Embed Text v2 MoE (candle backend)
143#[cfg(feature = "nomic-v2-moe")]
144pub use crate::models::nomic_v2_moe::{NomicConfig, NomicV2MoeTextEmbedding};