openai-interface 0.14.0

A low-level Rust interface for the OpenAI API
Documentation
//! Create an eval via `POST /evals`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.

use serde::{Deserialize, Serialize};
use url::Url;

use crate::{
    errors::OapiError,
    evals::Eval,
    rest::post::{Post, PostNoStream},
};

crate::request_body! {
    /// Creates an eval.
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct CreateEvalRequest {
        /// The name of the eval.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub name: Option<String>,
        /// The configuration of the eval (the schema of the test cases),
        /// as raw JSON.
        pub data_source_config: serde_json::Value,
        /// The testing criteria (graders) applied by the eval, as raw
        /// JSON.
        pub testing_criteria: Vec<serde_json::Value>,
        /// Arbitrary key-value metadata (up to 16 pairs) attached to the
        /// eval.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub metadata: Option<std::collections::HashMap<String, String>>,
    }
}

impl Post for CreateEvalRequest {
    fn is_streaming(&self) -> bool {
        false
    }

    /// Builds the URL for the request.
    ///
    /// `base_url` should be 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("evals");
        Ok(url.to_string())
    }
}

impl PostNoStream for CreateEvalRequest {
    type Response = Eval;
}