openai-interface 0.6.0

A low-level Rust interface for the OpenAI API
Documentation
//! Module for deleting a fine-tuned model.
//!
//! You must have the Owner role in your organization to delete a model.
//!
//! > ![warn] This module is untested!
//! > OpenAI-compatible providers are not known to implement this endpoint.
//! > If you encounter any issues, please report them on the repository.

pub mod request {
    use url::Url;

    use crate::{
        errors::OapiError,
        rest::delete::{Delete, DeleteNoStream},
    };

    /// Delete a fine-tuned model by its identifier.
    #[derive(Debug, Clone)]
    pub struct DeleteModelRequest<'a> {
        /// The ID of the model to delete.
        pub model: &'a str,
    }

    impl Delete for DeleteModelRequest<'_> {
        /// base_url should look like <https://api.openai.com/v1>
        fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
            let mut url =
                Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
            url.path_segments_mut()
                .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
                .push("models")
                .push(self.model);

            Ok(url.to_string())
        }
    }

    impl DeleteNoStream for DeleteModelRequest<'_> {
        type Response = super::response::ModelDeleted;
    }
}

pub mod response {
    use serde::Deserialize;

    /// The response of a model deletion.
    #[derive(Debug, Deserialize, Clone)]
    pub struct ModelDeleted {
        /// The ID of the model that was deleted.
        pub id: String,
        /// Whether the model was deleted.
        pub deleted: bool,
        /// The object type, which is always `model`.
        pub object: String,
    }

    crate::impl_from_str!(ModelDeleted);
}

#[cfg(test)]
mod tests {
    use super::request::DeleteModelRequest;
    use crate::rest::delete::Delete;

    #[test]
    fn test_build_url() {
        let request = DeleteModelRequest {
            model: "ft:gpt-4o:my-org:custom-suffix:7p8f9k",
        };
        let url = request.build_url("https://api.openai.com/v1").unwrap();
        assert_eq!(
            url,
            "https://api.openai.com/v1/models/ft:gpt-4o:my-org:custom-suffix:7p8f9k"
        );
    }

    /// Deserializes a model deletion response.
    ///
    /// No accessible provider implements this endpoint, so this fixture is
    /// NOT captured from a live response. The structure (`id` / `deleted` /
    /// `object`) follows the schema of openai-python `types/model_deleted.py`;
    /// the values are constructed for the test.
    #[test]
    fn test_parse_delete_response() {
        let content =
            r#"{"id":"ft:gpt-4o:my-org:custom-suffix:7p8f9k","deleted":true,"object":"model"}"#;

        let response: super::response::ModelDeleted = content.parse().unwrap();
        assert_eq!(response.id, "ft:gpt-4o:my-org:custom-suffix:7p8f9k");
        assert!(response.deleted);
        assert_eq!(response.object, "model");
    }
}