async_openai/
evals.rs

1use serde::Serialize;
2
3use crate::{
4    config::Config,
5    error::OpenAIError,
6    eval_runs::EvalRuns,
7    types::evals::{CreateEvalRequest, DeleteEvalResponse, Eval, EvalList, UpdateEvalRequest},
8    Client,
9};
10
11/// Create, manage, and run evals in the OpenAI platform. Related guide:
12/// [Evals](https://platform.openai.com/docs/guides/evals)
13pub struct Evals<'c, C: Config> {
14    client: &'c Client<C>,
15}
16
17impl<'c, C: Config> Evals<'c, C> {
18    pub fn new(client: &'c Client<C>) -> Self {
19        Self { client }
20    }
21
22    /// [EvalRuns] API group
23    pub fn runs(&self, eval_id: &str) -> EvalRuns<'_, C> {
24        EvalRuns::new(self.client, eval_id)
25    }
26
27    /// List evaluations for a project.
28    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
29    pub async fn list<Q>(&self, query: &Q) -> Result<EvalList, OpenAIError>
30    where
31        Q: Serialize + ?Sized,
32    {
33        self.client.get_with_query("/evals", &query).await
34    }
35
36    /// Create the structure of an evaluation that can be used to test a model's performance.
37    /// An evaluation is a set of testing criteria and the config for a data source, which dictates
38    /// the schema of the data used in the evaluation. After creating an evaluation, you can run it
39    /// on different models and model parameters. We support several types of graders and
40    /// datasources. For more information, see the [Evals guide](https://platform.openai.com/docs/guides/evals).
41    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
42    pub async fn create(&self, request: CreateEvalRequest) -> Result<Eval, OpenAIError> {
43        self.client.post("/evals", request).await
44    }
45
46    /// Get an evaluation by ID.
47    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
48    pub async fn retrieve(&self, eval_id: &str) -> Result<Eval, OpenAIError> {
49        self.client.get(&format!("/evals/{eval_id}")).await
50    }
51
52    /// Update certain properties of an evaluation.
53    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
54    pub async fn update(
55        &self,
56        eval_id: &str,
57        request: UpdateEvalRequest,
58    ) -> Result<Eval, OpenAIError> {
59        self.client
60            .post(&format!("/evals/{eval_id}"), request)
61            .await
62    }
63
64    /// Delete an evaluation.
65    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
66    pub async fn delete(&self, eval_id: &str) -> Result<DeleteEvalResponse, OpenAIError> {
67        self.client.delete(&format!("/evals/{eval_id}")).await
68    }
69}