openai-interface 0.10.0

A low-level Rust interface for the OpenAI API
Documentation
//! The Batches API: run async batch processing jobs over collections of
//! requests via `/batches`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.
//!
//! A batch job reads a JSONL file of requests (uploaded through the
//! [`files`](crate::files) API), processes all of them within the
//! completion window, and writes the results to an output file. See
//! [the OpenAI Batch API guide](https://platform.openai.com/docs/guides/batch).
//!
//! Submodules: [`create`], [`retrieve`], [`list`], [`cancel`].

pub mod cancel;
pub mod create;
pub mod list;
pub mod retrieve;

/// The lifecycle status of a batch job.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BatchStatus {
    /// The input file is being validated.
    Validating,
    /// Validation failed; see the `errors` field.
    Failed,
    /// The batch is currently being processed.
    InProgress,
    /// The batch finished processing and the output file is being
    /// prepared.
    Finalizing,
    /// The batch completed and the output file is ready.
    Completed,
    /// The batch could not complete within the completion window.
    Expired,
    /// The batch is being cancelled.
    Cancelling,
    /// The batch was cancelled.
    Cancelled,
}

/// The per-request result counts of a batch job.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct BatchRequestCounts {
    /// The total number of requests in the input file.
    pub total: u64,
    /// The number of requests that completed successfully.
    pub completed: u64,
    /// The number of requests that failed.
    pub failed: u64,
}

/// An error entry attached to a batch job.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct BatchError {
    /// An error code identifying the failure mode.
    #[serde(default)]
    pub code: Option<String>,
    /// A human-readable error message.
    #[serde(default)]
    pub message: Option<String>,
    /// The request this error refers to, if applicable. Parameters
    /// follow the JSONL line format of the input file.
    #[serde(default)]
    pub param: Option<serde_json::Value>,
    /// The JSONL line of the input file this error refers to, if
    /// applicable.
    #[serde(default)]
    pub line: Option<u64>,
}

/// The error list of a batch job.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct BatchErrors {
    /// Always `list`, when present.
    #[serde(default)]
    pub object: Option<String>,
    /// The individual errors.
    #[serde(default)]
    pub data: Vec<BatchError>,
}

/// A batch processing job.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct Batch {
    /// The batch ID, e.g. `batch_...`.
    pub id: String,
    /// The object type, always `batch`.
    #[serde(default)]
    pub object: Option<String>,
    /// The API endpoint the batch runs against, e.g.
    /// `/v1/chat/completions`.
    pub endpoint: String,
    /// Errors of the batch, if any.
    #[serde(default)]
    pub errors: Option<BatchErrors>,
    /// The ID of the input JSONL file.
    pub input_file_id: String,
    /// The time window within which the batch should be processed,
    /// e.g. `24h`.
    pub completion_window: String,
    /// The current lifecycle status.
    pub status: BatchStatus,
    /// The ID of the output JSONL file, once the batch completes.
    #[serde(default)]
    pub output_file_id: Option<String>,
    /// The ID of the JSONL file carrying the per-request errors, when
    /// some requests failed.
    #[serde(default)]
    pub error_file_id: Option<String>,
    /// Unix timestamp (seconds) of when the batch was created.
    pub created_at: u64,
    /// Unix timestamp (seconds) of when the batch started processing.
    #[serde(default)]
    pub in_progress_at: Option<u64>,
    /// Unix timestamp (seconds) of when the batch expires if not
    /// completed.
    #[serde(default)]
    pub expires_at: Option<u64>,
    /// Unix timestamp (seconds) of when the batch started finalizing.
    #[serde(default)]
    pub finalizing_at: Option<u64>,
    /// Unix timestamp (seconds) of when the batch completed.
    #[serde(default)]
    pub completed_at: Option<u64>,
    /// Unix timestamp (seconds) of when the batch failed.
    #[serde(default)]
    pub failed_at: Option<u64>,
    /// Unix timestamp (seconds) of when the batch expired.
    #[serde(default)]
    pub expired_at: Option<u64>,
    /// Unix timestamp (seconds) of when the batch started cancelling.
    #[serde(default)]
    pub cancelling_at: Option<u64>,
    /// Unix timestamp (seconds) of when the batch was cancelled.
    #[serde(default)]
    pub cancelled_at: Option<u64>,
    /// Per-request result counts, once available.
    #[serde(default)]
    pub request_counts: Option<BatchRequestCounts>,
    /// Arbitrary key-value metadata attached to the batch.
    #[serde(default)]
    pub metadata: Option<std::collections::HashMap<String, String>>,
}

crate::impl_from_str!(Batch);