openai-interface 0.14.0

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

use serde::Serialize;
use url::Url;

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

/// Creates an eval run.
#[derive(Debug, Clone, Serialize)]
pub struct CreateEvalRunRequest<'a> {
    /// The ID of the eval to run, e.g. `eval_...`.
    #[serde(skip)]
    pub eval_id: &'a str,
    /// The name of the run.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// The data source of the run (e.g. a completions file or a
    /// logged-completions query), as raw JSON.
    pub data_source: serde_json::Value,
    /// Arbitrary key-value metadata (up to 16 pairs) attached to the
    /// run.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<std::collections::HashMap<String, String>>,
    /// Whether to run the criteria on a sample of the data source.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub testing_criteria_sample: Option<serde_json::Value>,
    /// Additional JSON properties flattened into the request body, for
    /// fields not covered by the typed struct.
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_body_map: Option<serde_json::Map<String, serde_json::Value>>,
}

impl Post for CreateEvalRunRequest<'_> {
    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")
            .push(self.eval_id)
            .push("runs");
        Ok(url.to_string())
    }
}

impl PostNoStream for CreateEvalRunRequest<'_> {
    type Response = EvalRun;
}