Skip to main content

mongodb_voyageai/
lib.rs

1//! # voyageai
2//!
3//! An async Rust client for the [VoyageAI](https://www.voyageai.com) API —
4//! generate embeddings and rerank documents.
5//!
6//! ## Quick Start
7//!
8//! ```rust,no_run
9//! use mongodb_voyageai::{Client, Config};
10//!
11//! # #[tokio::main]
12//! # async fn main() -> Result<(), mongodb_voyageai::Error> {
13//! // Reads VOYAGEAI_API_KEY from the environment
14//! let client = Client::new(&Config::new())?;
15//!
16//! let embed = client
17//!     .embed(vec!["Hello, world!"])
18//!     .send()
19//!     .await?;
20//!
21//! println!("model: {}", embed.model);
22//! println!("dimensions: {}", embed.embedding(0).unwrap().len());
23//! # Ok(())
24//! # }
25//! ```
26//!
27//! ## Embeddings
28//!
29//! ```rust,no_run
30//! # use mongodb_voyageai::{Client, Config, model};
31//! # #[tokio::main]
32//! # async fn main() -> Result<(), mongodb_voyageai::Error> {
33//! let config = Config::new();
34//! let client = Client::new(&config)?;
35//!
36//! // Single embedding
37//! let embed = client
38//!     .embed(vec!["A quick brown fox."])
39//!     .send()
40//!     .await?;
41//! let vector = embed.embedding(0).unwrap();
42//!
43//! // Multiple embeddings with a specific model
44//! let embed = client
45//!     .embed(vec!["doc one", "doc two"])
46//!     .model(model::VOYAGE_3)
47//!     .input_type("document")
48//!     .send()
49//!     .await?;
50//!
51//! assert_eq!(embed.embeddings.len(), 2);
52//! # Ok(())
53//! # }
54//! ```
55//!
56//! ## Reranking
57//!
58//! ```rust,no_run
59//! # use mongodb_voyageai::{Client, Config};
60//! # #[tokio::main]
61//! # async fn main() -> Result<(), mongodb_voyageai::Error> {
62//! let client = Client::with_api_key("pa-...")?;
63//!
64//! let rerank = client
65//!     .rerank(
66//!         "Who fixes pipes?",
67//!         vec!["Paul is a plumber.", "John is a musician."]
68//!     )
69//!     .send()
70//!     .await?;
71//!
72//! println!("best match: index={}", rerank.results[0].index);
73//! # Ok(())
74//! # }
75//! ```
76//!
77//! ## Configuration
78//!
79//! ```rust
80//! use std::time::Duration;
81//! use mongodb_voyageai::Config;
82//!
83//! let config = Config {
84//!     api_key: Some("pa-...".into()),
85//!     host: "https://api.voyageai.com".into(),
86//!     version: "v1".into(),
87//!     timeout: Some(Duration::from_secs(30)),
88//! };
89//! ```
90//!
91//! ## Error Handling
92//!
93//! ```rust,no_run
94//! # use mongodb_voyageai::{Client, Config, Error};
95//! # #[tokio::main]
96//! # async fn main() {
97//! # let client = Client::with_api_key("pa-...").unwrap();
98//! match client.embed(vec!["hello"]).send().await {
99//!     Ok(embed) => println!("{} embeddings", embed.embeddings.len()),
100//!     Err(Error::MissingApiKey) => eprintln!("set VOYAGEAI_API_KEY"),
101//!     Err(Error::RequestError { status, body }) => eprintln!("HTTP {status}: {body}"),
102//!     Err(Error::Http(e)) => eprintln!("network: {e}"),
103//!     Err(Error::Json(e)) => eprintln!("parse: {e}"),
104//! }
105//! # }
106//! ```
107
108pub mod client;
109pub mod config;
110pub mod embed;
111pub mod model;
112pub mod rerank;
113pub mod reranking;
114pub mod usage;
115
116pub use client::{Client, Error};
117pub use config::Config;
118pub use embed::Embed;
119pub use rerank::Rerank;
120pub use reranking::Reranking;
121pub use usage::Usage;