Back to Mistral Rs

Compute and compare text embeddings with cosine similarity

docs/src/content/docs/examples/rust/advanced/embeddings.md

0.9.01.0 KB
Original Source
<!-- generated by docs/scripts/render_examples.py; edit the source example instead -->

Compute and compare text embeddings with cosine similarity.

Run with: cargo run --release --example embeddings -p mistralrs

rust
//! Compute and compare text embeddings with cosine similarity.
//!
//! Run with: `cargo run --release --example embeddings -p mistralrs`

use anyhow::Result;
use mistralrs::{Device, EmbeddingModelBuilder, EmbeddingRequest, Tensor};

#[tokio::main]
async fn main() -> Result<()> {
    let model = EmbeddingModelBuilder::new("Qwen/Qwen3-Embedding-0.6B")
        .with_logging()
        .build()
        .await?;

    let embeddings = model
        .generate_embeddings(EmbeddingRequest::builder().add_prompt("What is graphene"))
        .await?;

    let y = Tensor::new(embeddings[0].clone(), &Device::Cpu)?;
    y.write_npy("test.npy")?;

    Ok(())
}

Source: mistralrs/examples/advanced/embeddings/main.rs