openmodex 0.1.1

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

/// Service for legacy text completion operations.
///
/// Obtained via [`OpenModex::completions`].
#[derive(Debug)]
pub struct CompletionService<'a> {
    client: &'a OpenModex,
}

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

    /// Create a text completion.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use openmodex::*;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Error> {
    /// let client = OpenModex::new("omx_sk_...")?;
    /// let response = client.completions().create(
    ///     CompletionRequest::new("gpt-3.5-turbo-instruct", "Once upon a time")
    ///         .max_tokens(100)
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(
        &self,
        mut req: CompletionRequest,
    ) -> Result<CompletionResponse, 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();
            }
        }

        req.stream = Some(false);

        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("/completions", &r).await }
            })
            .await
    }
}