Skip to main content

openai_interface/completions/
response.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5/// The streaming and non-streaming completion response shares the same json structure.
6#[derive(Debug, Deserialize, Serialize, Clone)]
7pub struct Completion {
8    /// A unique identifier for the completion.
9    pub id: String,
10    /// The list of completion choices the model generated for the input prompt.
11    pub choices: Vec<CompletionChoice>,
12    /// The Unix timestamp (in seconds) of when the completion was created.
13    pub created: usize,
14    /// The model used for completion.
15    pub model: String,
16    /// The object type, which is always "text_completion"
17    pub object: String,
18    /// This fingerprint represents the backend configuration that the model runs with.
19    ///
20    /// Can be used in conjunction with the `seed` request parameter to understand when
21    /// backend changes have been made that might impact determinism.
22    pub system_fingerprint: Option<String>,
23    /// Usage statistics for the completion request.
24    pub usage: Option<CompletionUsage>,
25}
26
27#[derive(Debug, Deserialize, Serialize, Clone)]
28pub struct Logprobs {
29    /// The offset into the generated text for each token.
30    pub text_offset: Option<Vec<usize>>,
31    /// The log probability of each token in the generated text.
32    pub token_logprobs: Option<Vec<f32>>,
33    /// The tokens generated by the model.
34    pub tokens: Option<Vec<String>>,
35    /// The top log probabilities for each token position.
36    pub top_logprobs: Option<Vec<HashMap<String, f32>>>,
37}
38
39#[derive(Debug, Deserialize, Serialize, Clone)]
40pub struct CompletionChoice {
41    /// The reason the model stopped generating tokens.
42    pub finish_reason: Option<String>,
43    /// The index of this choice in the array of choices.
44    pub index: usize,
45    /// The log probabilities for each token in the generated text.
46    pub logprobs: Option<Logprobs>,
47    /// The generated text.
48    pub text: String,
49}
50
51#[derive(Debug, Deserialize, Serialize, Clone)]
52pub struct CompletionTokensDetails {
53    /// When using Predicted Outputs, the number of tokens in the prediction that
54    /// appeared in the completion.
55    pub accepted_prediction_tokens: Option<usize>,
56
57    /// Audio input tokens generated by the model.
58    pub audio_tokens: Option<usize>,
59
60    /// Tokens generated by the model for reasoning.
61    pub reasoning_tokens: Option<usize>,
62
63    /// When using Predicted Outputs, the number of tokens in the prediction that did
64    /// not appear in the completion. However, like reasoning tokens, these tokens are
65    /// still counted in the total completion tokens for purposes of billing, output,
66    /// and context window limits.
67    pub rejected_prediction_tokens: Option<usize>,
68}
69
70#[derive(Debug, Deserialize, Serialize, Clone)]
71pub struct PromptTokensDetails {
72    /// Audio input tokens present in the prompt.
73    pub audio_tokens: Option<usize>,
74
75    /// Cached tokens present in the prompt.
76    pub cached_tokens: Option<usize>,
77}
78
79#[derive(Debug, Deserialize, Serialize, Clone)]
80pub struct CompletionUsage {
81    /// Number of tokens in the generated completion.
82    pub completion_tokens: usize,
83
84    /// Number of tokens in the prompt.
85    pub prompt_tokens: usize,
86
87    /// Total number of tokens used in the request (prompt + completion).
88    pub total_tokens: usize,
89
90    /// Breakdown of tokens used in a completion.
91    pub completion_tokens_details: Option<CompletionTokensDetails>,
92
93    /// Breakdown of tokens used in the prompt.
94    pub prompt_tokens_details: Option<PromptTokensDetails>,
95}
96
97crate::impl_from_str!(Completion);