# Model2Vec-RS

> Fast static embedding inference in Rust for Model2Vec models

- Category: Retrieval & Knowledge (RAG) / Embedding Models & Inference
- Language: Rust
- Licence: MIT
- Repository: https://github.com/MinishLab/model2vec-rs
- Canonical page: https://ai-tldr.dev/tools/model2vec-rs/

## In short

Model2Vec-RS is the official Rust implementation for running Model2Vec static embedding models. Model2Vec is a technique that turns sentence transformers into compact static embedding models, cutting model size and speeding up inference. It sits in the Embedding Models & Inference category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Rust and licensed MIT.

## Overview

Model2Vec-RS is the official Rust implementation for running Model2Vec static embedding models. Model2Vec is a technique that turns sentence transformers into compact static embedding models, cutting model size and speeding up inference. This crate handles the inference side, so you can generate embeddings from text with low latency.

It is aimed at Rust developers who need embeddings inside an application or pipeline without the overhead of a full transformer runtime. You can use it as a library in your own code or as a standalone command-line tool for quick terminal-based encoding.

As an embedding inference tool, it loads pre-trained Model2Vec models directly from the Hugging Face Hub or from a local path, supports f32, f16, and i8 weights stored in safetensors files, and offers feature flags for native, local-only, and WebAssembly builds.

## What it does

- Loads Model2Vec models from the Hugging Face Hub by model ID or from a local path via from_pretrained
- Alternative loaders for embedded and WASM use: from_bytes, from_owned, and from_borrowed for in-memory data
- Supports f32, f16, and i8 weight types stored in safetensors files
- Batch encoding with configurable max sequence length and batch size
- Ships both a Rust library and a standalone CLI (encode-single, encode)
- Feature flags for native, fancy-regex, local-only, and wasm runtime configurations

## Getting started

You can use Model2Vec-RS as a Rust library or as a standalone CLI tool. Both load models from the Hugging Face Hub or a local path.

1. Add the crate as a dependency

   Add model2vec-rs to your Rust project with Cargo.

```bash
cargo add model2vec-rs
```

2. Load a model and generate embeddings

   Load a model from the Hugging Face Hub (or a local path) and encode a list of sentences. The result is a Vec<Vec<f32>>.

```rust
use anyhow::Result;
use model2vec_rs::model::StaticModel;

fn main() -> Result<()> {
    // Arguments: (repo_or_path, hf_token, normalize_embeddings, subfolder_in_repo)
    let model = StaticModel::from_pretrained(
        "minishlab/potion-base-8M",
        None,
        None,
        None,
    )?;

    let sentences = vec![
        "Hello world".to_string(),
        "Rust is awesome".to_string(),
    ];

    let embeddings = model.encode(&sentences);
    println!("Generated {} embeddings.", embeddings.len());

    Ok(())
}
```

3. Or install and use the CLI

   Install the executable in release mode, then encode a single sentence. Output is printed as JSON.

```bash
cargo install model2vec-rs
model2vec-rs encode-single "Hello world" "minishlab/potion-base-8M"
```

## When to use it

- Generating text embeddings inside a Rust application or service with low latency
- Quick terminal-based encoding of sentences or files via the CLI
- Running static embeddings in embedded or WebAssembly environments using in-memory model loading
- Batch-encoding documents for semantic search or retrieval indexing
