rust_gpt/
completion.rs

1//! # Completion API.
2//!
3//! Includes the structs that represent a response from the Completion API.
4
5use serde::{ser::SerializeStruct, Deserialize, Serialize};
6
7#[derive(Debug, Serialize, Deserialize)]
8/// Represents one of the choices returned by the completion API.
9pub struct CompletionChoice {
10    pub text: String,
11    pub index: u32,
12    pub logprobs: Option<u64>,
13    pub finish_reason: String,
14}
15
16#[derive(Debug, Deserialize)]
17/// Represents a response from the completion API.
18pub struct CompletionResponse {
19    pub id: String,
20    pub object: String,
21    pub created: u64,
22    pub model: String,
23    pub choices: Vec<CompletionChoice>,
24}
25
26impl Serialize for CompletionResponse {
27    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
28    where
29        S: serde::Serializer,
30    {
31        let mut state = serializer.serialize_struct("CompletionResponse", 5)?;
32        state.serialize_field("id", &self.id)?;
33        state.serialize_field("object", &self.object)?;
34        state.serialize_field("created", &self.created)?;
35        state.serialize_field("model", &self.model)?;
36        state.serialize_field("choices", &self.choices)?;
37        state.end()
38    }
39}