embedrs
Unified cloud embedding API client for Rust. One interface for OpenAI, Cohere, Gemini, Voyage, and Jina embedding APIs -- with automatic batching, similarity functions, retry with backoff, and timeout support.
Features
- 5 providers -- OpenAI, Cohere, Google Gemini, Voyage AI, Jina AI (plus compatible API variants)
- Automatic batching -- splits large input sets into provider-appropriate chunks, processes concurrently
- Similarity functions -- cosine similarity, dot product, Euclidean distance
- Input type hints -- search document, search query, classification, clustering
- Configurable dimensions -- request reduced-dimension embeddings where supported
- Exponential backoff -- automatic retry on HTTP 429/503 with jitter
- Request timeout -- overall timeout covering retries and backoff
- Builder pattern -- ergonomic
IntoFuture-based API (client.embed(...).await) - Client defaults -- set model, dimensions, input type once, override per-request
- Optional tracing -- structured logging via
tracingcrate behind a feature flag
Installation
[]
= "0.1"
Or via the command line:
Quick Start
use *;
let client = openai;
let result = client.embed.await?;
println!;
println!;
Providers
| Provider | Constructor | Default Model | Max Batch Size |
|---|---|---|---|
| OpenAI | Client::openai(key) |
text-embedding-3-small |
2048 |
| Cohere | Client::cohere(key) |
embed-v4.0 |
96 |
| Google Gemini | Client::gemini(key) |
gemini-embedding-001 |
100 |
| Voyage AI | Client::voyage(key) |
voyage-3-large |
128 |
| Jina AI | Client::jina(key) |
jina-embeddings-v3 |
2048 |
Each provider also has a *_compatible constructor for proxies or API-compatible services:
// OpenAI
let client = openai;
// Cohere
let client = cohere;
// Google Gemini
let client = gemini;
// Voyage AI
let client = voyage;
// Jina AI
let client = jina;
// OpenAI-compatible (e.g., Azure, proxies)
let client = openai_compatible;
// Cohere-compatible
let client = cohere_compatible;
// Gemini-compatible
let client = gemini_compatible;
// Voyage-compatible
let client = voyage_compatible;
// Jina-compatible
let client = jina_compatible;
Batch Embedding
Embed thousands of texts concurrently. Texts are automatically chunked based on the provider's maximum batch size and processed with semaphore-limited concurrency:
let client = openai;
let texts: = .map.collect;
let result = client.embed_batch
.concurrency // max concurrent API requests (default: 5)
.chunk_size // texts per request (default: provider max)
.model
.await?;
println!;
println!;
Input Type
Some providers use input type hints to optimize embeddings for specific use cases:
use InputType;
// for indexing documents
let result = client.embed
.input_type
.await?;
// for search queries
let result = client.embed
.input_type
.await?;
Available variants: SearchDocument, SearchQuery, Classification, Clustering.
Dimensions
Request reduced-dimension embeddings where the provider supports it:
let result = client.embed
.model
.dimensions
.await?;
assert_eq!;
Similarity Functions
Compute similarity and distance between embedding vectors:
use ;
let a = vec!;
let b = vec!;
let cos = cosine_similarity; // 0.0 (orthogonal)
let dot = dot_product; // 0.0
let dist = euclidean_distance; // 1.414...
Backoff and Timeout
Enable exponential backoff on HTTP 429/503 errors and set an overall request timeout:
use Duration;
use BackoffConfig;
let client = openai
.with_retry_backoff // 500ms base, 30s cap, 3 retries
.with_timeout; // overall timeout (default: 60s)
// per-request override
let result = client.embed
.retry_backoff
.timeout
.await?;
Without backoff configured, HTTP 429/503 errors fail immediately.
Client Defaults
Set defaults once, override per-request:
let client = openai
.with_model
.with_dimensions
.with_input_type
.with_retry_backoff
.with_timeout;
// all requests use the defaults above
let a = client.embed.await?;
let b = client.embed.await?;
// override for a specific request
let c = client.embed
.model
.input_type
.await?;
Feature Flags
| Feature | Default | Description |
|---|---|---|
| (none) | yes | Core embedding client, all 5 providers |
tracing |
no | Structured logging via the tracing crate |
Enable tracing:
[]
= { = "0.1", = ["tracing"] }