openai-tools 0.1.3

Tools for OpenAI API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! # Usage
//! ## Chat Completion
//
//! ### Simple Chat
//
//! ```rust
//! # use openai_tools::{Message, OpenAI, Response};
//! # fn main() {
//!     let mut openai = OpenAI::new();
//!     let messages = vec![
//!         Message::new(String::from("user"), String::from("Hi there!"))
//!     ];
//
//!     openai
//!         .model_id(String::from("gpt-4o-mini"))
//!         .messages(messages)
//!         .temperature(1.0);
//
//!     let response: Response = openai.chat().unwrap();
//!     println!("{}", &response.choices[0].message.content);
//!     // Hello! How can I assist you today?
//! # }
//! ```
//
//! ### Chat with Json Schema
//
//! ```rust
//! # use openai_tools::{json_schema::JsonSchema, Message, OpenAI, Response, ResponseFormat};
//! # use serde::{Deserialize, Serialize};
//! # use serde_json;
//! # use std::env;
//! # fn main() {
//!     #[derive(Debug, Serialize, Deserialize)]
//!     struct Weather {
//!         location: String,
//!         date: String,
//!         weather: String,
//!         error: String,
//!     }
//
//!     let mut openai = OpenAI::new();
//!     let messages = vec![Message::new(
//!         String::from("user"),
//!         String::from("Hi there! How's the weather tomorrow in Tokyo? If you can't answer, report error."),
//!     )];
//
//!     // build json schema
//!     let mut json_schema = JsonSchema::new("weather".to_string());
//!     json_schema.add_property(
//!         String::from("location"),
//!         String::from("string"),
//!         Option::from(String::from("The location to check the weather for.")),
//!     );
//!     json_schema.add_property(
//!         String::from("date"),
//!         String::from("string"),
//!         Option::from(String::from("The date to check the weather for.")),
//!     );
//!     json_schema.add_property(
//!         String::from("weather"),
//!         String::from("string"),
//!         Option::from(String::from("The weather for the location and date.")),
//!     );
//!     json_schema.add_property(
//!         String::from("error"),
//!         String::from("string"),
//!         Option::from(String::from("Error message. If there is no error, leave this field empty.")),
//!     );
//
//!     // configure chat completion model
//!     openai
//!         .model_id(String::from("gpt-4o-mini"))
//!         .messages(messages)
//!         .temperature(1.0)
//!         .response_format(ResponseFormat::new(String::from("json_schema"), json_schema));
//!
//!     // execute chat
//!     let response = openai.chat().unwrap();
//
//!     let answer: Weather = serde_json::from_str::<Weather>(&response.choices[0].message.content).unwrap();
//!     println!("{:?}", answer)
//!     // Weather {
//!     //     location: "Tokyo",
//!     //     date: "2023-10-01",
//!     //     weather: "Temperatures around 25°C with partly cloudy skies and a slight chance of rain.",
//!     //     error: "",
//!     // }
//! # }
//! ```
pub mod json_schema;

use anyhow::Result;
use dotenvy::dotenv;
use fxhash::FxHashMap;
use json_schema::JsonSchema;
use serde::{Deserialize, Serialize};
use std::env;
use std::process::Command;

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Message {
    pub role: String,
    pub content: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub refusal: Option<String>,
}

impl Message {
    pub fn new(role: String, message: String) -> Self {
        Self {
            role: String::from(role),
            content: String::from(message),
            refusal: None,
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ResponseFormat {
    #[serde(rename = "type")]
    pub type_name: String,
    pub json_schema: JsonSchema,
}

impl ResponseFormat {
    pub fn new(type_name: String, json_schema: JsonSchema) -> Self {
        Self {
            type_name: String::from(type_name),
            json_schema,
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ChatCompletionRequestBody {
    // ID of the model to use. (https://platform.openai.com/docs/models#model-endpoint-compatibility)
    pub model: String,
    // A list of messages comprising teh conversation so far.
    pub messages: Vec<Message>,
    // Whether or not to store the output of this chat completion request for user. false by default.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub store: Option<bool>,
    // -2.0 ~ 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub frequency_penalty: Option<f32>,
    // Modify the likelihood of specified tokens appearing in the completion. Accepts a JSON object that maps tokens to an associated bias value from 100 to 100.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logit_bias: Option<FxHashMap<String, i32>>,
    // Whether to return log probabilities of the output tokens or not.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logprobs: Option<bool>,
    // 0 ~ 20. Specify the number of most likely tokens to return at each token position, each with an associated log probability.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_logprobs: Option<u8>,
    // An upper bound for the number of tokens that can be generated for a completion.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_completion_tokens: Option<u64>,
    // How many chat completion choices to generate for each input message. 1 by default.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub n: Option<u32>,
    // Output types that you would like the model to generate for this request. ["text"] for most models.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub modalities: Option<Vec<String>>,
    // -2.0 ~ 2.0. Positive values penalize new tokens based on whether they apper in the text so far, increasing the model's likelihood to talk about new topics.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub presence_penalty: Option<f32>,
    // 0 ~ 2. What sampling temperature to use. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,
    // An object specifying the format that the model must output. (https://platform.openai.com/docs/guides/structured-outputs)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_format: Option<ResponseFormat>,
}

impl ChatCompletionRequestBody {
    pub fn new(
        model_id: String,
        messages: Vec<Message>,
        store: Option<bool>,
        frequency_penalty: Option<f32>,
        logit_bias: Option<FxHashMap<String, i32>>,
        logprobs: Option<bool>,
        top_logprobs: Option<u8>,
        max_completion_tokens: Option<u64>,
        n: Option<u32>,
        modalities: Option<Vec<String>>,
        presence_penalty: Option<f32>,
        temperature: Option<f32>,
        response_format: Option<ResponseFormat>,
    ) -> Self {
        Self {
            model: model_id,
            messages,
            store: if let Some(value) = store {
                Option::from(value)
            } else {
                None
            },
            frequency_penalty: if let Some(value) = frequency_penalty {
                Option::from(value)
            } else {
                None
            },
            logit_bias: if let Some(value) = logit_bias {
                Option::from(value)
            } else {
                None
            },
            logprobs: if let Some(value) = logprobs {
                Option::from(value)
            } else {
                None
            },
            top_logprobs: if let Some(value) = top_logprobs {
                Option::from(value)
            } else {
                None
            },
            max_completion_tokens: if let Some(value) = max_completion_tokens {
                Option::from(value)
            } else {
                None
            },
            n: if let Some(value) = n {
                Option::from(value)
            } else {
                None
            },
            modalities: if let Some(value) = modalities {
                Option::from(value)
            } else {
                None
            },
            presence_penalty: if let Some(value) = presence_penalty {
                Option::from(value)
            } else {
                None
            },
            temperature: if let Some(value) = temperature {
                Option::from(value)
            } else {
                None
            },
            response_format: if let Some(value) = response_format {
                Option::from(value)
            } else {
                None
            },
        }
    }

    pub fn default() -> Self {
        Self {
            model: String::default(),
            messages: Vec::new(),
            store: None,
            frequency_penalty: None,
            logit_bias: None,
            logprobs: None,
            top_logprobs: None,
            max_completion_tokens: None,
            n: None,
            modalities: None,
            presence_penalty: None,
            temperature: None,
            response_format: None,
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Choice {
    pub index: u32,
    pub message: Message,
    pub finish_reason: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Usage {
    pub prompt_tokens: u64,
    pub completion_tokens: u64,
    pub total_tokens: u64,
    pub completion_tokens_details: FxHashMap<String, u64>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Response {
    pub id: String,
    pub object: String,
    pub created: u64,
    pub model: String,
    pub system_fingerprint: String,
    pub choices: Vec<Choice>,
    pub usage: Usage,
}

pub struct OpenAI {
    api_key: String,
    pub completion_body: ChatCompletionRequestBody,
}

impl OpenAI {
    pub fn new() -> Self {
        dotenv().ok();
        let api_key = env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY is not set.");
        return Self {
            api_key,
            completion_body: ChatCompletionRequestBody::default(),
        };
    }

    pub fn model_id(&mut self, model_id: String) -> &mut Self {
        self.completion_body.model = String::from(model_id);
        return self;
    }

    pub fn messages(&mut self, messages: Vec<Message>) -> &mut Self {
        self.completion_body.messages = messages;
        return self;
    }

    pub fn store(&mut self, store: bool) -> &mut Self {
        self.completion_body.store = Option::from(store);
        return self;
    }

    pub fn frequency_penalty(&mut self, frequency_penalty: f32) -> &mut Self {
        self.completion_body.frequency_penalty = Option::from(frequency_penalty);
        return self;
    }

    pub fn logit_bias(&mut self, logit_bias: FxHashMap<String, i32>) -> &mut Self {
        self.completion_body.logit_bias = Option::from(logit_bias);
        return self;
    }

    pub fn logprobs(&mut self, logprobs: bool) -> &mut Self {
        self.completion_body.logprobs = Option::from(logprobs);
        return self;
    }

    pub fn top_logprobs(&mut self, top_logprobs: u8) -> &mut Self {
        self.completion_body.top_logprobs = Option::from(top_logprobs);
        return self;
    }

    pub fn max_completion_tokens(&mut self, max_completion_tokens: u64) -> &mut Self {
        self.completion_body.max_completion_tokens = Option::from(max_completion_tokens);
        return self;
    }

    pub fn n(&mut self, n: u32) -> &mut Self {
        self.completion_body.n = Option::from(n);
        return self;
    }

    pub fn modalities(&mut self, modalities: Vec<String>) -> &mut Self {
        self.completion_body.modalities = Option::from(modalities);
        return self;
    }

    pub fn presence_penalty(&mut self, presence_penalty: f32) -> &mut Self {
        self.completion_body.presence_penalty = Option::from(presence_penalty);
        return self;
    }

    pub fn temperature(&mut self, temperature: f32) -> &mut Self {
        self.completion_body.temperature = Option::from(temperature);
        return self;
    }

    pub fn response_format(&mut self, response_format: ResponseFormat) -> &mut Self {
        self.completion_body.response_format = Option::from(response_format);
        return self;
    }

    pub fn chat(&mut self) -> Result<Response> {
        // Check if the API key is set & body is built.
        if self.api_key.is_empty() {
            return Err(anyhow::Error::msg("API key is not set."));
        }
        if self.completion_body.model.is_empty() {
            return Err(anyhow::Error::msg("Model ID is not set."));
        }
        if self.completion_body.messages.is_empty() {
            return Err(anyhow::Error::msg("Messages are not set."));
        }

        let body = serde_json::to_string(&self.completion_body)?;
        let url = "https://api.openai.com/v1/chat/completions";
        let cmd = Command::new("curl")
            .arg(url)
            .arg("-H")
            .arg("Content-Type: application/json")
            .arg("-H")
            .arg(format!("Authorization: Bearer {}", self.api_key))
            .arg("-d")
            .arg(body)
            .output()
            .expect("Failed to execute command");

        let content = String::from_utf8_lossy(&cmd.stdout).to_string();

        match serde_json::from_str::<Response>(&content) {
            Ok(response) => return Ok(response),
            Err(e) => {
                let e_msg = format!("Failed to parse JSON: {} CONTENT: {}", e, content);
                return Err(anyhow::Error::msg(e_msg));
            }
        }
    }
}

#[cfg(test)]
mod tests;