openai_rst/common.rs
1//! This module defines enums, structs, and macros for handling message roles and usage metrics.
2//! It includes:
3//! - `MessageRole`: Enum representing different roles in a messaging system.
4//! - `Usage`: Struct for tracking token usage in prompts and completions.
5//! - `impl_builder_methods!`: Macro for generating builder methods for structs.
6
7use serde::{Deserialize, Serialize};
8use strum::{AsRefStr, Display, EnumString};
9
10/// Represents different roles in a messaging system.
11#[derive(Debug, Deserialize, EnumString, Serialize, Clone, PartialEq, Eq, AsRefStr, Display)]
12pub enum MessageRole {
13 /// Represents a user role.
14 #[serde(rename = "user")]
15 #[strum(serialize = "user")]
16 User,
17 /// Represents a system role.
18 #[serde(rename = "system")]
19 #[strum(serialize = "system")]
20 System,
21 /// Represents an assistant role.
22 #[serde(rename = "assistant")]
23 #[strum(serialize = "assistant")]
24 Assistant,
25 /// Represents a function role.
26 #[serde(rename = "function")]
27 #[strum(serialize = "function")]
28 Function,
29}
30
31/// Struct for tracking token usage.
32#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
33pub struct Usage {
34 /// Number of tokens used in the prompt.
35 pub prompt_tokens: i32,
36 /// Number of tokens used in the completion.
37 pub completion_tokens: i32,
38 /// Total number of tokens used.
39 pub total_tokens: i32,
40}
41
42/// Macro for generating builder methods for a struct.
43#[macro_export]
44macro_rules! impl_builder_methods {
45 ($builder:ident, $($field:ident: $field_type:ty),*) => {
46 impl $builder {
47 $(
48 /// Sets the value of the specified field.
49 pub fn $field(mut self, $field: $field_type) -> Self {
50 self.$field = Some($field);
51 self
52 }
53 )*
54 }
55 };
56}