Skip to main content

openai_interface/evals/runs/
output_items.rs

1//! List the output items of an eval run via
2//! `GET /evals/{eval_id}/runs/{run_id}/output_items`.
3//!
4//! > ![warn] This module is untested!
5//! > If you encounter any issues, please report them on the repository.
6
7use url::Url;
8
9use crate::{
10    errors::OapiError,
11    pagination::PaginationQuery,
12    rest::get::{Get, GetNoStream},
13};
14
15/// Lists the output items of an eval run.
16#[derive(Debug, Clone, Default)]
17pub struct ListEvalRunOutputItemsRequest<'a> {
18    /// The ID of the eval, e.g. `eval_...`.
19    pub eval_id: &'a str,
20    /// The ID of the run, e.g. `evalrun_...`.
21    pub run_id: &'a str,
22    /// The standard pagination parameters (`before`, `after`,
23    /// `limit`, `order`).
24    pub pagination: PaginationQuery<'a>,
25    /// The status to filter output items by (`fail` or `pass`).
26    pub status: Option<&'a str>,
27    /// Additional query parameters appended verbatim to the URL.
28    pub extra_query: Option<std::collections::HashMap<String, String>>,
29}
30
31impl Get for ListEvalRunOutputItemsRequest<'_> {
32    /// Builds the URL for the request.
33    ///
34    /// `base_url` should be like <https://api.openai.com/v1>
35    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
36        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
37        url.path_segments_mut()
38            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
39            .push("evals")
40            .push(self.eval_id)
41            .push("runs")
42            .push(self.run_id)
43            .push("output_items");
44
45        let mut touched = false;
46        {
47            let mut pairs = url.query_pairs_mut();
48            if self.pagination.any_set() {
49                self.pagination.append_to(&mut pairs);
50                touched = true;
51            }
52            if let Some(status) = self.status {
53                pairs.append_pair("status", status);
54                touched = true;
55            }
56            if let Some(extra_query) = &self.extra_query {
57                for (key, value) in extra_query {
58                    pairs.append_pair(key, value);
59                }
60                touched = true;
61            }
62        }
63        if !touched {
64            url.set_query(None);
65        }
66
67        Ok(url.to_string())
68    }
69}
70
71impl GetNoStream for ListEvalRunOutputItemsRequest<'_> {
72    type Response = ListEvalRunOutputItemsResponse;
73}
74
75/// The testing-criterion result of an eval run output item.
76#[derive(Debug, Clone, serde::Deserialize)]
77pub struct EvalRunOutputItemTestingCriteriaResult {
78    /// The ID of the testing criterion.
79    #[serde(default)]
80    pub testing_criteria: Option<String>,
81    /// Whether the item passed this criterion.
82    #[serde(default)]
83    pub passed: Option<bool>,
84    /// The reason for the result, as raw JSON.
85    #[serde(default)]
86    pub reason: Option<serde_json::Value>,
87}
88
89/// An output item of an eval run.
90#[derive(Debug, Clone, serde::Deserialize)]
91pub struct EvalRunOutputItem {
92    /// The item ID, e.g. `evalrunoi_...`.
93    pub id: String,
94    /// The object type, always `eval.run.output_item`.
95    #[serde(default)]
96    pub object: Option<String>,
97    /// The ID of the run this item belongs to.
98    #[serde(default)]
99    pub eval_id: Option<String>,
100    /// The ID of the run this item belongs to.
101    #[serde(default)]
102    pub run_id: Option<String>,
103    /// The sample being tested (test case input and model output),
104    /// as raw JSON.
105    #[serde(default)]
106    pub datasource_item: Option<serde_json::Value>,
107    /// The index of the sample in the data source.
108    #[serde(default)]
109    pub datasource_item_id: Option<u64>,
110    /// Whether the item passed all criteria.
111    #[serde(default)]
112    pub passed: Option<bool>,
113    /// The per-criterion results of the item.
114    #[serde(default)]
115    pub testing_criteria_results: Option<Vec<EvalRunOutputItemTestingCriteriaResult>>,
116}
117
118/// The response of listing a run's output items.
119#[derive(Debug, Clone, serde::Deserialize)]
120pub struct ListEvalRunOutputItemsResponse {
121    /// The output items on this page.
122    #[serde(default)]
123    pub data: Vec<EvalRunOutputItem>,
124    /// Whether more items exist after this page.
125    #[serde(default)]
126    pub has_more: Option<bool>,
127    /// The ID of the first item on the page, for cursor pagination.
128    #[serde(default)]
129    pub first_id: Option<String>,
130    /// The ID of the last item on the page, for cursor pagination.
131    #[serde(default)]
132    pub last_id: Option<String>,
133    /// The object type (`list`), if the provider sends it.
134    #[serde(default)]
135    pub object: Option<String>,
136}
137
138crate::impl_from_str!(ListEvalRunOutputItemsResponse);