openai-types 0.1.3

Typed OpenAI API models — zero dependencies beyond serde
Documentation
// AUTO-GENERATED by py2rust — do not edit.
// Re-generate: python3 scripts/py2rust.py sync <python_dir> <rust_dir>
// Domain: completion

use serde::{Deserialize, Serialize};

/// Represents a completion response from the API.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
pub struct Completion {
    /// A unique identifier for the completion.
    pub id: String,
    /// The list of completion choices the model generated for the input prompt.
    pub choices: Vec<CompletionChoice>,
    /// The Unix timestamp (in seconds) of when the completion was created.
    pub created: i64,
    /// The model used for completion.
    pub model: String,
    /// The object type, which is always "text_completion"
    pub object: String,
    /// This fingerprint represents the backend configuration that the model runs with.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub system_fingerprint: Option<String>,
    /// Usage statistics for the completion request.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub usage: Option<CompletionUsage>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
pub struct Logprobs {
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub text_offset: Option<Vec<i64>>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub token_logprobs: Option<Vec<f64>>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub tokens: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub top_logprobs: Option<Vec<std::collections::HashMap<String, f64>>>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum CompletionChoiceFinishReason {
    #[serde(rename = "stop")]
    Stop,
    #[serde(rename = "length")]
    Length,
    #[serde(rename = "content_filter")]
    ContentFilter,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
pub struct CompletionChoice {
    /// The reason the model stopped generating tokens.
    pub finish_reason: CompletionChoiceFinishReason,
    pub index: i64,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub logprobs: Option<Logprobs>,
    pub text: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
pub struct CompletionCreateParamsBase {
    /// ID of the model to use.
    pub model: Option<String>,
    /// The prompt(s) to generate completions for, encoded as a string, array of
    pub prompt: serde_json::Value,
    /// Generates `best_of` completions server-side and returns the "best" (the one with
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub best_of: Option<i64>,
    /// Echo back the prompt in addition to the completion
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub echo: Option<bool>,
    /// Number between -2.0 and 2.0.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub frequency_penalty: Option<f64>,
    /// Modify the likelihood of specified tokens appearing in the completion.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub logit_bias: Option<std::collections::HashMap<String, i64>>,
    /// Include the log probabilities on the `logprobs` most likely output tokens, as
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub logprobs: Option<i64>,
    /// The maximum number of [tokens](/tokenizer) that can be generated in the
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub max_tokens: Option<i64>,
    /// How many completions to generate for each prompt.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub n: Option<i64>,
    /// Number between -2.0 and 2.0.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub presence_penalty: Option<f64>,
    /// If specified, our system will make a best effort to sample deterministically,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub seed: Option<i64>,
    /// Not supported with latest reasoning models `o3` and `o4-mini`.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub stop: Option<serde_json::Value>,
    /// Options for streaming response. Only set this when you set `stream: true`.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub stream_options: Option<serde_json::Value>,
    /// The suffix that comes after a completion of inserted text.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub suffix: Option<String>,
    /// What sampling temperature to use, between 0 and 2.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub temperature: Option<f64>,
    /// An alternative to sampling with temperature, called nucleus sampling, where the
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub top_p: Option<f64>,
    /// A unique identifier representing your end-user, which can help OpenAI to monitor
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub user: Option<String>,
}

pub type CompletionCreateParams = serde_json::Value;

/// Breakdown of tokens used in a completion.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
pub struct CompletionTokensDetails {
    /// When using Predicted Outputs, the number of tokens in the prediction that
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub accepted_prediction_tokens: Option<i64>,
    /// Audio input tokens generated by the model.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub audio_tokens: Option<i64>,
    /// Tokens generated by the model for reasoning.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub reasoning_tokens: Option<i64>,
    /// When using Predicted Outputs, the number of tokens in the prediction that did
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub rejected_prediction_tokens: Option<i64>,
}

/// Breakdown of tokens used in the prompt.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
pub struct PromptTokensDetails {
    /// Audio input tokens present in the prompt.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub audio_tokens: Option<i64>,
    /// Cached tokens present in the prompt.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub cached_tokens: Option<i64>,
}

/// Usage statistics for the completion request.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
pub struct CompletionUsage {
    /// Number of tokens in the generated completion.
    pub completion_tokens: i64,
    /// Number of tokens in the prompt.
    pub prompt_tokens: i64,
    /// Total number of tokens used in the request (prompt + completion).
    pub total_tokens: i64,
    /// Breakdown of tokens used in a completion.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub completion_tokens_details: Option<CompletionTokensDetails>,
    /// Breakdown of tokens used in the prompt.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub prompt_tokens_details: Option<PromptTokensDetails>,
}