openai_interface/completions/
response.rs

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