openai-interface 0.10.0

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

use url::Url;

use crate::{
    errors::OapiError,
    pagination::PaginationQuery,
    rest::get::{Get, GetNoStream},
};

/// Lists the output items of an eval run.
#[derive(Debug, Clone, Default)]
pub struct ListEvalRunOutputItemsRequest<'a> {
    /// The ID of the eval, e.g. `eval_...`.
    pub eval_id: &'a str,
    /// The ID of the run, e.g. `evalrun_...`.
    pub run_id: &'a str,
    /// The standard pagination parameters (`before`, `after`,
    /// `limit`, `order`).
    pub pagination: PaginationQuery<'a>,
    /// The status to filter output items by (`fail` or `pass`).
    pub status: Option<&'a str>,
    /// Additional query parameters appended verbatim to the URL.
    pub extra_query: Option<std::collections::HashMap<String, String>>,
}

impl Get for ListEvalRunOutputItemsRequest<'_> {
    /// 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")
            .push(self.run_id)
            .push("output_items");

        let mut touched = false;
        {
            let mut pairs = url.query_pairs_mut();
            if self.pagination.any_set() {
                self.pagination.append_to(&mut pairs);
                touched = true;
            }
            if let Some(status) = self.status {
                pairs.append_pair("status", status);
                touched = true;
            }
            if let Some(extra_query) = &self.extra_query {
                for (key, value) in extra_query {
                    pairs.append_pair(key, value);
                }
                touched = true;
            }
        }
        if !touched {
            url.set_query(None);
        }

        Ok(url.to_string())
    }
}

impl GetNoStream for ListEvalRunOutputItemsRequest<'_> {
    type Response = ListEvalRunOutputItemsResponse;
}

/// The testing-criterion result of an eval run output item.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct EvalRunOutputItemTestingCriteriaResult {
    /// The ID of the testing criterion.
    #[serde(default)]
    pub testing_criteria: Option<String>,
    /// Whether the item passed this criterion.
    #[serde(default)]
    pub passed: Option<bool>,
    /// The reason for the result, as raw JSON.
    #[serde(default)]
    pub reason: Option<serde_json::Value>,
}

/// An output item of an eval run.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct EvalRunOutputItem {
    /// The item ID, e.g. `evalrunoi_...`.
    pub id: String,
    /// The object type, always `eval.run.output_item`.
    #[serde(default)]
    pub object: Option<String>,
    /// The ID of the run this item belongs to.
    #[serde(default)]
    pub eval_id: Option<String>,
    /// The ID of the run this item belongs to.
    #[serde(default)]
    pub run_id: Option<String>,
    /// The sample being tested (test case input and model output),
    /// as raw JSON.
    #[serde(default)]
    pub datasource_item: Option<serde_json::Value>,
    /// The index of the sample in the data source.
    #[serde(default)]
    pub datasource_item_id: Option<u64>,
    /// Whether the item passed all criteria.
    #[serde(default)]
    pub passed: Option<bool>,
    /// The per-criterion results of the item.
    #[serde(default)]
    pub testing_criteria_results: Option<Vec<EvalRunOutputItemTestingCriteriaResult>>,
}

/// The response of listing a run's output items.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ListEvalRunOutputItemsResponse {
    /// The output items on this page.
    #[serde(default)]
    pub data: Vec<EvalRunOutputItem>,
    /// Whether more items exist after this page.
    #[serde(default)]
    pub has_more: Option<bool>,
    /// The ID of the first item on the page, for cursor pagination.
    #[serde(default)]
    pub first_id: Option<String>,
    /// The ID of the last item on the page, for cursor pagination.
    #[serde(default)]
    pub last_id: Option<String>,
    /// The object type (`list`), if the provider sends it.
    #[serde(default)]
    pub object: Option<String>,
}

crate::impl_from_str!(ListEvalRunOutputItemsResponse);