Skip to main content

Crate mongodb_voyageai

Crate mongodb_voyageai 

Source
Expand description

§voyageai

An async Rust client for the VoyageAI API — generate embeddings and rerank documents.

§Quick Start

use mongodb_voyageai::{Client, Config};

// Reads VOYAGEAI_API_KEY from the environment
let client = Client::new(&Config::new())?;

let embed = client
    .embed(vec!["Hello, world!"])
    .send()
    .await?;

println!("model: {}", embed.model);
println!("dimensions: {}", embed.embedding(0).unwrap().len());

§Embeddings

let config = Config::new();
let client = Client::new(&config)?;

// Single embedding
let embed = client
    .embed(vec!["A quick brown fox."])
    .send()
    .await?;
let vector = embed.embedding(0).unwrap();

// Multiple embeddings with a specific model
let embed = client
    .embed(vec!["doc one", "doc two"])
    .model(model::VOYAGE_3)
    .input_type("document")
    .send()
    .await?;

assert_eq!(embed.embeddings.len(), 2);

§Reranking

let client = Client::with_api_key("pa-...")?;

let rerank = client
    .rerank(
        "Who fixes pipes?",
        vec!["Paul is a plumber.", "John is a musician."]
    )
    .send()
    .await?;

println!("best match: index={}", rerank.results[0].index);

§Configuration

use std::time::Duration;
use mongodb_voyageai::Config;

let config = Config {
    api_key: Some("pa-...".into()),
    host: "https://api.voyageai.com".into(),
    version: "v1".into(),
    timeout: Some(Duration::from_secs(30)),
};

§Error Handling

match client.embed(vec!["hello"]).send().await {
    Ok(embed) => println!("{} embeddings", embed.embeddings.len()),
    Err(Error::MissingApiKey) => eprintln!("set VOYAGEAI_API_KEY"),
    Err(Error::RequestError { status, body }) => eprintln!("HTTP {status}: {body}"),
    Err(Error::Http(e)) => eprintln!("network: {e}"),
    Err(Error::Json(e)) => eprintln!("parse: {e}"),
}

Re-exports§

pub use client::Client;
pub use client::Error;
pub use config::Config;
pub use embed::Embed;
pub use rerank::Rerank;
pub use reranking::Reranking;
pub use usage::Usage;

Modules§

client
HTTP client for the VoyageAI API.
config
Client configuration for the VoyageAI API.
embed
Embedding response types.
model
Pre-defined model name constants for the VoyageAI API.
rerank
Reranking response types.
reranking
Individual reranking result.
usage
Token usage information returned by the VoyageAI API.