llm_rs/
json.rs

1//! The structures for building the Json prompts
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Deserialize)]
5pub struct Choice {
6    // Field that are present in the response but that are not used here
7    // logprobs: Option<Vec<f32>>,
8    // index: i32,
9    pub text: String,
10    pub finish_reason: String,
11}
12
13#[derive(Debug, Deserialize, Serialize, Clone)]
14pub struct Usage {
15    pub prompt_tokens: u32,
16    pub completion_tokens: u32,
17    pub total_tokens: u32,
18}
19
20/// Response for a completions request.  See
21/// https://platform.openai.com/docs/api-reference/completions/create
22#[derive(Debug, Serialize, Deserialize)]
23pub struct CompletionRequestInfo {
24    // The `id` is in response but not used here
25    #[serde(skip_serializing)]
26    pub object: String,
27    #[serde(skip_serializing)]
28    pub choices: Vec<Choice>,
29    #[serde(skip_deserializing)]
30    pub prompt: String,
31    pub model: String,
32    #[serde(skip_deserializing)]
33    temperature: f32,
34    #[serde(skip_deserializing)]
35    max_tokens: u32,
36    #[serde(skip_serializing)]
37    pub usage: Usage,
38}
39
40/// Response for a chats request.  See
41/// https://platform.openai.com/docs/api-reference/chat/create
42
43#[derive(Debug, Serialize, Deserialize)]
44pub struct Message {
45    pub role: String,
46    pub content: String,
47}
48#[derive(Serialize, Debug, Deserialize)]
49#[serde(tag = "t")]
50pub struct ChatChoice {
51    index: u32,
52    pub message: Message,
53    pub finish_reason: String,
54}
55
56#[derive(Debug, Serialize, Deserialize)]
57pub struct ImageURL {
58    pub url: String,
59}
60
61#[derive(Debug, Serialize, Deserialize)]
62pub struct ImageRequestInfo {
63    created: u64,
64    pub data: Vec<ImageURL>,
65}
66
67#[derive(Debug, Serialize, Deserialize)]
68pub struct ChatRequestInfo {
69    id: String,
70    pub object: String,
71    created: u64,
72    pub model: String,
73    pub usage: Usage,
74    pub choices: Vec<ChatChoice>,
75}
76
77/// To receive the transcribed text
78#[derive(Debug, Serialize, Deserialize)]
79pub struct AudioTranscriptionResponse {
80    pub text: String,
81}
82
83// To receive a list of files
84#[derive(Debug, Serialize, Deserialize)]
85pub struct File {
86    pub id: String,
87    object: String,
88    bytes: usize,
89    created_at: usize,
90    pub filename: String,
91    purpose: String,
92}
93
94/// All the files stored at openai
95#[derive(Debug, Serialize, Deserialize)]
96pub struct Files {
97    object: String,
98    pub data: Vec<File>,
99}
100
101/// Response after file uploaded
102#[derive(Debug, Serialize, Deserialize)]
103pub struct FileUploadResponse {
104    pub id: String,
105    object: String,
106    bytes: usize,
107    created_at: usize,
108    filename: String,
109    purpose: String,
110}
111
112/// Information about a file
113#[derive(Debug, Serialize, Deserialize)]
114pub struct FileInfoResponse {
115    //   "id": "file-XjGxS3KTG0uNmNOK362iJua3",
116    pub id: String,
117    //   "object": "file",
118    pub object: String,
119    //   "bytes": 140,
120    pub bytes: usize,
121    //   "created_at": 1613779657,
122    pub created_at: u64,
123    //   "filename": "mydata.jsonl",
124    pub filename: String,
125    //   "purpose": "fine-tune"
126    pub purpose: String,
127}
128
129/// Response after file deleted
130#[derive(Debug, Serialize, Deserialize)]
131pub struct FileDeletedResponse {
132    pub id: String,
133    pub object: String,
134    pub deleted: bool,
135}
136
137impl CompletionRequestInfo {
138    pub fn new(prompt: &str, model: &str, temperature: f32, max_tokens: u32) -> Self {
139        Self {
140            choices: Vec::new(),
141            usage: Usage {
142                prompt_tokens: 0,
143                completion_tokens: 0,
144                total_tokens: 0,
145            },
146            // id: String::new(),
147            object: String::new(),
148            prompt: prompt.to_string(),
149            model: model.to_string(),
150            temperature,
151            max_tokens,
152        }
153    }
154}
155
156#[derive(Debug, Serialize, Deserialize)]
157struct Permission {
158    id: String,
159    object: String,
160    created: u64,
161    allow_create_engine: bool,
162    allow_sampling: bool,
163    allow_logprobs: bool,
164    allow_search_indices: bool,
165    allow_view: bool,
166    allow_fine_tuning: bool,
167    organization: String,
168    group: Option<String>,
169    is_blocking: bool,
170}
171
172#[derive(Debug, Serialize, Deserialize)]
173pub struct Model {
174    id: String,
175    object: String,
176    created: u64,
177    owned_by: String,
178    permission: Vec<Permission>,
179    pub root: String,
180    parent: Option<String>,
181}
182
183#[derive(Debug, Serialize, Deserialize)]
184pub struct ModelReturned {
185    object: String,
186    pub data: Vec<Model>,
187}
188
189/// Response for a "models" query
190#[derive(Debug, Serialize, Deserialize)]
191struct ModelData {
192    id: String,
193}
194
195#[derive(Debug, Serialize, Deserialize)]
196struct ModelRequestInfo {
197    data: Vec<ModelData>,
198}