openmodex 0.1.1

Official Rust SDK for the OpenModex API
Documentation
use crate::client::OpenModex;
use crate::error::Error;
use crate::types::{EmbeddingRequest, EmbeddingResponse};

/// Service for embedding operations.
///
/// Obtained via [`OpenModex::embeddings`].
#[derive(Debug)]
pub struct EmbeddingService<'a> {
    client: &'a OpenModex,
}

impl<'a> EmbeddingService<'a> {
    pub(crate) fn new(client: &'a OpenModex) -> Self {
        Self { client }
    }

    /// Create embeddings.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use openmodex::*;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Error> {
    /// let client = OpenModex::new("omx_sk_...")?;
    /// let response = client.embeddings().create(
    ///     EmbeddingRequest::new("text-embedding-3-small", "Hello world")
    /// ).await?;
    ///
    /// println!("Embedding dimension: {}", response.data[0].embedding.len());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(
        &self,
        mut req: EmbeddingRequest,
    ) -> Result<EmbeddingResponse, Error> {
        // Apply default model if none specified.
        if req.model.is_empty() {
            if let Some(ref default) = self.client.default_model {
                req.model = default.clone();
            }
        }

        let model = req.model.clone();

        self.client
            .with_fallback(&model, |m| {
                let mut r = req.clone();
                r.model = m;
                let client = self.client;
                async move { client.post("/embeddings", &r).await }
            })
            .await
    }
}