chatgpt/
types.rs

1use serde::{Deserialize, Serialize};
2
3/// A role of a message sender, can be:
4/// - `System`, for starting system message, that sets the tone of model
5/// - `Assistant`, for messages sent by ChatGPT
6/// - `User`, for messages sent by user
7#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
8#[serde(rename_all = "lowercase")]
9pub enum Role {
10    /// A system message, automatically sent at the start to set the tone of the model
11    System,
12    /// A message sent by ChatGPT
13    Assistant,
14    /// A message sent by the user
15    User,
16}
17
18/// Container for the sent/received ChatGPT messages
19#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
20pub struct ChatMessage {
21    /// Role of message sender
22    pub role: Role,
23    /// Actual content of the message
24    pub content: String,
25}
26
27/// A request struct sent to the API to request a message completion
28#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize)]
29pub struct CompletionRequest<'a> {
30    /// The model to be used, currently `gpt-3.5-turbo`, but may change in future
31    pub model: &'a str,
32    /// The message history, including the message that requires completion, which should be the last one
33    pub messages: &'a Vec<ChatMessage>,
34}
35
36/// Represents a response from the API
37#[derive(Debug, Clone, PartialEq, PartialOrd, Deserialize)]
38#[serde(untagged)]
39pub enum ServerResponse {
40    /// An error occurred, most likely the model was just overloaded
41    Error {
42        /// The error that happened
43        error: CompletionError,
44    },
45    /// Completion successfuly completed
46    Completion(CompletionResponse),
47}
48
49/// An error happened while requesting completion
50#[derive(Debug, Clone, PartialEq, PartialOrd, Deserialize)]
51pub struct CompletionError {
52    /// Message, describing the error
53    pub message: String,
54    /// The type of error. Example: `server_error`
55    #[serde(rename = "type")]
56    pub error_type: String,
57}
58
59/// A response struct received from the API after requesting a message completion
60#[derive(Debug, Clone, PartialEq, PartialOrd, Deserialize)]
61pub struct CompletionResponse {
62    /// Unique ID of the message, but not in a UUID format.
63    /// Example: `chatcmpl-6p5FEv1JHictSSnDZsGU4KvbuBsbu`
64    #[serde(rename = "id")]
65    pub message_id: String,
66    /// Unix seconds timestamp of when the response was created
67    #[serde(rename = "created")]
68    pub created_timestamp: u64,
69    /// The model that was used for this completion
70    pub model: String,
71    /// Token usage of this completion
72    pub usage: TokenUsage,
73    /// Message choices for this response, guaranteed to contain at least one message response
74    #[serde(rename = "choices")]
75    pub message_choices: Vec<MessageChoice>,
76}
77
78impl CompletionResponse {
79    /// A shortcut to access the message response
80    pub fn message(&self) -> &ChatMessage {
81        // Unwrap is safe here, as we know that at least one message choice is provided
82        &self.message_choices.first().unwrap().message
83    }
84}
85
86/// A message completion choice struct
87#[derive(Debug, Clone, PartialEq, PartialOrd, Deserialize)]
88pub struct MessageChoice {
89    /// The actual message
90    pub message: ChatMessage,
91    /// The reason completion was stopped
92    pub finish_reason: String,
93    /// The index of this message in the outer `message_choices` array
94    pub index: u32,
95}
96
97/// The token usage of a specific response
98#[derive(Debug, Clone, PartialEq, PartialOrd, Deserialize)]
99pub struct TokenUsage {
100    /// Tokens spent on the prompt message (including previous messages)
101    pub prompt_tokens: u32,
102    /// Tokens spent on the completion message
103    pub completion_tokens: u32,
104    /// Total amount of tokens used (`prompt_tokens + completion_tokens`)
105    pub total_tokens: u32,
106}