Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

HuggingFace Embeddings

This crate gives you access to thousands of open-source sentence-transformer models for generating text embeddings via the HuggingFace Inference API.

Setup

Add the huggingface feature to your Cargo.toml:

[dependencies]
synaptic = { version = "0.4", features = ["huggingface"] }

Optionally set your HuggingFace API token:

export HF_API_KEY="hf_..."

Configuration

use synaptic::huggingface::{HuggingFaceEmbeddings, HuggingFaceEmbeddingsConfig};

let config = HuggingFaceEmbeddingsConfig::new("BAAI/bge-small-en-v1.5")
    .with_api_key("hf_...");
let embeddings = HuggingFaceEmbeddings::new(config);
ModelDimensionsUse Case
BAAI/bge-small-en-v1.5384Fast English retrieval
BAAI/bge-large-en-v1.51024High-quality English retrieval
sentence-transformers/all-MiniLM-L6-v2384General purpose, popular
intfloat/multilingual-e5-large1024Multilingual retrieval
BAAI/bge-m31024Multilingual, long context

Usage

Embed a query

use synaptic::core::Embeddings;

let vector = embeddings.embed_query("What is Rust?").await?;
println!("Dimension: {}", vector.len());

Embed documents

use synaptic::core::Embeddings;

let docs = ["Rust ensures memory safety", "Python is interpreted"];
let vecs = embeddings.embed_documents(&docs).await?;

RAG Pipeline

Combine HuggingFace embeddings with InMemoryVectorStore for retrieval:

use synaptic::huggingface::{HuggingFaceEmbeddings, HuggingFaceEmbeddingsConfig};
use synaptic::vectorstores::InMemoryVectorStore;

let embeddings = std::sync::Arc::new(HuggingFaceEmbeddings::new(
    HuggingFaceEmbeddingsConfig::new("BAAI/bge-small-en-v1.5").with_api_key("hf_..."),
));
let store = std::sync::Arc::new(InMemoryVectorStore::new());
store.add_documents(&docs, embeddings.as_ref()).await?;
let results = retriever.retrieve("memory safe language").await?;

API Key

Get a HuggingFace API token from https://huggingface.co/settings/tokens. The free tier provides access to public models. Paid tokens unlock higher rate limits and private model access.

Configuration Reference

FieldTypeDefaultDescription
modelStringrequiredHuggingFace model ID
api_keyOptionNoneAPI token
base_urlStringhttps://api-inference.huggingface.co/modelsAPI base URL
wait_for_modelbooltrueWait for model to load