openai_rs_api/core/mod.rs
1pub mod models;
2
3use std::{error::Error, sync::Arc};
4
5use awc::Client;
6
7use rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore};
8
9use self::models::{
10 audio::{TextResponse, TranscriptionParameters, TranslationParameters},
11 chat::{ChatParameters, ChatResponse},
12 completions::{CompletionParameters, CompletionResponse},
13 edits::{EditParameters, EditResponse},
14 embeddings::{EmbeddingParameters, EmbeddingResponse},
15 files::{DeleteResponse, FileData, FileList, FileUpload},
16 fine_tunes::{
17 CreateFineTuneParameters, FineTuneDelete, FineTuneEventList, FineTuneList,
18 FineTuneRetriveData,
19 },
20 images::{ImageCreateParameters, ImageEditParameters, ImageResponse, ImageVariationParameters},
21 list_models::{Model, ModelList},
22 moderations::{TextModerationParameters, TextModerationResult},
23};
24
25#[derive(Clone)]
26pub struct OpenAI {
27 pub token: String,
28 pub oia_org: String,
29 https_client: Client,
30}
31
32impl OpenAI {
33 /// The function creates a new instance of the OpenAI struct with the provided token and
34 /// organization, along with an HTTPS client.
35 ///
36 /// Arguments:
37 ///
38 /// * `token`: The `token` parameter is a string that represents the authentication token used to
39 /// access the OpenAI API. This token is typically provided by OpenAI when you sign up for their
40 /// services.
41 /// * `oia_org`: The `oia_org` parameter represents the OpenAI organization ID. It is used to
42 /// identify the organization associated with the API token being used.
43 ///
44 /// Returns:
45 ///
46 /// The `new` function returns an instance of the `OpenAI` struct.
47 pub fn new(token: String, oia_org: String) -> Self {
48 let https_client = Client::builder()
49 .connector(awc::Connector::new().rustls(Arc::new(Self::rustls_config())))
50 .finish();
51 OpenAI {
52 token,
53 oia_org,
54 https_client,
55 }
56 }
57
58 fn rustls_config() -> ClientConfig {
59 let mut root_store = RootCertStore::empty();
60 root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
61 OwnedTrustAnchor::from_subject_spki_name_constraints(
62 ta.subject,
63 ta.spki,
64 ta.name_constraints,
65 )
66 }));
67
68 rustls::ClientConfig::builder()
69 .with_safe_defaults()
70 .with_root_certificates(root_store)
71 .with_no_client_auth()
72 }
73
74 /// The function `list_models` sends a GET request to the OpenAI API to retrieve a list of models
75 /// and returns the parsed response as a `ModelList` object.
76 ///
77 /// Returns:
78 ///
79 /// a Result object with the type ModelList.
80 #[cfg(feature = "list_models")]
81 pub async fn list_models(self) -> Result<ModelList, Box<dyn Error>> {
82 let client = self.https_client;
83 let url = String::from("https://api.openai.com/v1/models");
84
85 let result = client
86 .get(url)
87 .bearer_auth(self.token)
88 .send()
89 .await
90 .unwrap()
91 .body()
92 .await
93 .unwrap();
94
95 Ok(serde_json::from_slice::<ModelList>(&result).expect("Failed to parse model list"))
96 }
97
98 /// The function retrieves a model from the OpenAI API using an HTTPS client and returns the parsed
99 /// model response.
100 ///
101 /// Arguments:
102 ///
103 /// * `model`: The `model` parameter is a `String` that represents the name of the model you want to
104 /// retrieve. It is used to construct the URL for the API request.
105 ///
106 /// Returns:
107 ///
108 /// a Result object with the type Model as the Ok variant and Box<dyn Error> as the Err variant.
109 #[cfg(feature = "list_models")]
110 pub async fn retrive_model(self, model: String) -> Result<Model, Box<dyn Error>> {
111 let client = self.https_client;
112 let url = format!("https://api.openai.com/v1/models/{}", model);
113
114 let result = client
115 .get(url)
116 .bearer_auth(self.token)
117 .send()
118 .await
119 .unwrap()
120 .body()
121 .await
122 .unwrap();
123
124 Ok(serde_json::from_slice::<Model>(&result).expect("Failed to parse model response"))
125 }
126
127 /// The function `create_chat_completions` sends a POST request to the OpenAI API to generate chat completions
128 /// based on the given parameters.
129 ///
130 /// Arguments:
131 ///
132 /// * `parameters`: The `parameters` parameter in the `create_chat_completions` function is of type
133 /// `ChatParameters`. It is an input parameter that contains the information required to
134 /// generate chat completions using the OpenAI API.
135 ///
136 /// Returns:
137 ///
138 /// a `Result` with a `ChatResponse` on success or a `Box<dyn Error>` on failure.
139 #[cfg(feature = "chat")]
140 pub async fn create_chat_completions(
141 self,
142 parameters: ChatParameters,
143 ) -> Result<ChatResponse, Box<dyn Error>> {
144 let client = self.https_client;
145 let url = String::from("https://api.openai.com/v1/chat/completions");
146
147 let result = client
148 .post(url)
149 .insert_header(("Content-Type", "application/json"))
150 .bearer_auth(self.token)
151 .send_json(¶meters)
152 .await
153 .unwrap()
154 .body()
155 .await
156 .unwrap();
157
158 Ok(serde_json::from_slice::<ChatResponse>(&result).expect("Failed to parse chat response"))
159 }
160
161 /// The function `create_completions` sends a POST request to the OpenAI API to generate completions
162 /// based on the given parameters.
163 ///
164 /// Arguments:
165 ///
166 /// * `parameters`: The `parameters` parameter in the `create_completions` function is of type
167 /// `CompletionParameters`. It is an input parameter that contains the information required to
168 /// generate completions using the OpenAI API.
169 ///
170 /// Returns:
171 ///
172 /// a `Result` with a `CompletionResponse` on success or a `Box<dyn Error>` on failure.
173 #[cfg(feature = "completions")]
174 pub async fn create_completions(
175 self,
176 parameters: CompletionParameters,
177 ) -> Result<CompletionResponse, Box<dyn Error>> {
178 let client = self.https_client;
179 let url = String::from("https://api.openai.com/v1/completions");
180
181 let result = client
182 .post(url)
183 .insert_header(("Content-Type", "application/json"))
184 .bearer_auth(self.token)
185 .send_json(¶meters)
186 .await
187 .unwrap()
188 .body()
189 .await
190 .unwrap();
191
192 Ok(serde_json::from_slice::<CompletionResponse>(&result)
193 .expect("Failed to parse completion response"))
194 }
195
196 /// The function `create_edit` sends a POST request to the OpenAI API to create or edit a completion
197 /// and returns the response.
198 ///
199 /// Arguments:
200 ///
201 /// * `parameters`: The `parameters` parameter in the `create_edit` function is of type
202 /// `EditParameters`. It is an input parameter that contains the necessary information for creating
203 /// an edit. The specific fields and their meanings depend on the implementation of the
204 /// `EditParameters` struct. You would need to refer to the definition
205 ///
206 /// Returns:
207 ///
208 /// a `Result` with the type `EditResponse` on success or a `Box<dyn Error>` on failure.
209 #[cfg(feature = "edits")]
210 pub async fn create_edit(
211 self,
212 parameters: EditParameters,
213 ) -> Result<EditResponse, Box<dyn Error>> {
214 let client = self.https_client;
215 let url = String::from("https://api.openai.com/v1/completions");
216
217 let result = client
218 .post(url)
219 .insert_header(("Content-Type", "application/json"))
220 .bearer_auth(self.token)
221 .send_json(¶meters)
222 .await
223 .unwrap()
224 .body()
225 .await
226 .unwrap();
227
228 Ok(serde_json::from_slice::<EditResponse>(&result).expect("Failed to parse edit response"))
229 }
230
231 /// The `create_image` function sends a POST request to the OpenAI API to generate an image based on
232 /// the provided parameters.
233 ///
234 /// Arguments:
235 ///
236 /// * `parameters`: The `parameters` parameter in the `create_image` function is of type
237 /// `ImageCreateParameters`. It is an input parameter that contains the necessary information for
238 /// generating an image.
239 ///
240 /// Returns:
241 ///
242 /// The function `create_image` returns a `Result` enum with the success case containing an
243 /// `ImageResponse` and the error case containing a `Box<dyn Error>`.
244 #[cfg(feature = "images")]
245 pub async fn create_image(
246 self,
247 parameters: ImageCreateParameters,
248 ) -> Result<ImageResponse, Box<dyn Error>> {
249 let client = self.https_client;
250 let url = String::from("https://api.openai.com/v1/images/generations");
251
252 let result = client
253 .post(url)
254 .insert_header(("Content-Type", "application/json"))
255 .bearer_auth(self.token)
256 .send_json(¶meters)
257 .await
258 .unwrap()
259 .body()
260 .await
261 .unwrap();
262
263 Ok(serde_json::from_slice::<ImageResponse>(&result)
264 .expect("Failed to parse image response"))
265 }
266
267 /// The function `create_image_edit` sends a POST request to the OpenAI API to create an image edit,
268 /// using the provided parameters, and returns the resulting image response.
269 ///
270 /// Arguments:
271 ///
272 /// * `parameters`: The `parameters` parameter in the `create_image_edit` function is of type
273 /// `ImageEditParameters`. It is an input parameter that contains the necessary information for
274 /// creating an image edit.
275 ///
276 /// Returns:
277 ///
278 /// a Result type with the success variant containing an ImageResponse or the error variant
279 /// containing a Box<dyn Error>.
280 #[cfg(feature = "images")]
281 pub async fn create_image_edit(
282 self,
283 parameters: ImageEditParameters,
284 ) -> Result<ImageResponse, Box<dyn Error>> {
285 let client = self.https_client;
286 let url = String::from("https://api.openai.com/v1/images/edits");
287
288 let result = client
289 .post(url)
290 .insert_header(("Content-Type", "application/json"))
291 .bearer_auth(self.token)
292 .send_json(¶meters)
293 .await
294 .unwrap()
295 .body()
296 .await
297 .unwrap();
298
299 Ok(serde_json::from_slice::<ImageResponse>(&result)
300 .expect("Failed to parse image response"))
301 }
302
303 /// The function `create_image_variations` sends a POST request to the OpenAI API to create image
304 /// variations based on the provided parameters.
305 ///
306 /// Arguments:
307 ///
308 /// * `parameters`: The `parameters` parameter in the `create_image_variations` function is of type
309 /// `ImageVariationParameters`. It is an input parameter that contains the necessary information for
310 /// creating image variations.
311 ///
312 /// Returns:
313 ///
314 /// a Result object with the type ImageResponse.
315 #[cfg(feature = "images")]
316 pub async fn create_image_variations(
317 self,
318 parameters: ImageVariationParameters,
319 ) -> Result<ImageResponse, Box<dyn Error>> {
320 let client = self.https_client;
321 let url = String::from("https://api.openai.com/v1/images/variations");
322
323 let result = client
324 .post(url)
325 .insert_header(("Content-Type", "application/json"))
326 .bearer_auth(self.token)
327 .send_json(¶meters)
328 .await
329 .unwrap()
330 .body()
331 .await
332 .unwrap();
333
334 Ok(serde_json::from_slice::<ImageResponse>(&result)
335 .expect("Failed to parse image response"))
336 }
337
338 /// The function `create_embedding` sends a POST request to the OpenAI API to create an embedding,
339 /// using the provided parameters, and returns the resulting embedding response.
340 ///
341 /// Arguments:
342 ///
343 /// * `parameters`: The `parameters` parameter in the `create_embedding` function is of type
344 /// `EmbeddingParameters`. It is an input parameter that contains the necessary information for
345 /// creating an embedding.
346 ///
347 /// Returns:
348 ///
349 /// a Result type with the success variant containing an EmbeddingResponse or the error variant
350 /// containing a Box<dyn Error>.
351 #[cfg(feature = "embeddings")]
352 pub async fn create_embedding(
353 self,
354 parameters: EmbeddingParameters,
355 ) -> Result<EmbeddingResponse, Box<dyn Error>> {
356 let client = self.https_client;
357 let url = String::from("https://api.openai.com/v1/embeddings");
358
359 let result = client
360 .post(url)
361 .insert_header(("Content-Type", "application/json"))
362 .bearer_auth(self.token)
363 .send_json(¶meters)
364 .await
365 .unwrap()
366 .body()
367 .await
368 .unwrap();
369
370 Ok(serde_json::from_slice::<EmbeddingResponse>(&result)
371 .expect("Failed to parse embedding response"))
372 }
373
374 /// The function `create_transcription` sends a POST request to the OpenAI API to create a transcription,
375 /// using the provided parameters, and returns the resulting transcription response.
376 ///
377 /// Arguments:
378 ///
379 /// * `parameters`: The `parameters` parameter in the `create_transcription` function is of type
380 /// `TranscriptionParameters`. It is an input parameter that contains the necessary information for
381 /// creating a transcription.
382 ///
383 /// Returns:
384 ///
385 /// a Result type with the success variant containing a TextResponse or the error variant
386 /// containing a Box<dyn Error>.
387 #[cfg(feature = "audio")]
388 pub async fn create_transcription(
389 self,
390 parameters: TranscriptionParameters,
391 ) -> Result<TextResponse, Box<dyn Error>> {
392 let client = self.https_client;
393 let url = String::from("https://api.openai.com/v1/audio/transcriptions");
394
395 let result = client
396 .post(url)
397 .insert_header(("Content-Type", "application/json"))
398 .bearer_auth(self.token)
399 .send_json(¶meters)
400 .await
401 .unwrap()
402 .body()
403 .await
404 .unwrap();
405
406 Ok(serde_json::from_slice::<TextResponse>(&result).expect("Failed to parse text response"))
407 }
408
409 /// The function `create_translation` sends a POST request to the OpenAI API to create a translation,
410 /// using the provided parameters, and returns the resulting translation response.
411 ///
412 /// Arguments:
413 ///
414 /// * `parameters`: The `parameters` parameter in the `create_translation` function is of type
415 /// `TranslationParameters`. It is an input parameter that contains the necessary information for
416 /// creating a translation.
417 ///
418 /// Returns:
419 ///
420 /// a Result type with the success variant containing a TextResponse or the error variant
421 /// containing a Box<dyn Error>.
422 #[cfg(feature = "audio")]
423 pub async fn create_translation(
424 self,
425 parameters: TranslationParameters,
426 ) -> Result<TextResponse, Box<dyn Error>> {
427 let client = self.https_client;
428 let url = String::from("https://api.openai.com/v1/audio/translations");
429
430 let result = client
431 .post(url)
432 .insert_header(("Content-Type", "application/json"))
433 .bearer_auth(self.token)
434 .send_json(¶meters)
435 .await
436 .unwrap()
437 .body()
438 .await
439 .unwrap();
440
441 Ok(serde_json::from_slice::<TextResponse>(&result).expect("Failed to parse text response"))
442 }
443
444 /// The function `list_files` makes an asynchronous HTTP GET request to the OpenAI API to retrieve a
445 /// list of files and returns the parsed result.
446 ///
447 /// Returns:
448 ///
449 /// The function `list_files` returns a `Result` containing either a `FileList` or a boxed dynamic
450 /// error (`Box<dyn Error>`).
451 #[cfg(feature = "files")]
452 pub async fn list_files(self) -> Result<FileList, Box<dyn Error>> {
453 let client = self.https_client;
454 let url = String::from("https://api.openai.com/v1/files");
455
456 let result = client
457 .get(url)
458 .bearer_auth(self.token)
459 .send()
460 .await
461 .unwrap()
462 .body()
463 .await
464 .unwrap();
465
466 Ok(serde_json::from_slice::<FileList>(&result).expect("Failed to parse file list"))
467 }
468
469 /// The `upload_files` function in Rust uploads files to the OpenAI API and returns the file data.
470 ///
471 /// Arguments:
472 ///
473 /// * `parameters`: The `parameters` parameter in the `upload_files` function is of type
474 /// `FileUpload`. It represents the data that needs to be uploaded to the server. The `FileUpload`
475 /// struct should contain the necessary information for the file upload, such as the file content,
476 /// file name, and file type
477 ///
478 /// Returns:
479 ///
480 /// The function `upload_files` returns a `Result` containing either a `FileData` object or an error
481 /// (`Box<dyn Error>`).
482 #[cfg(feature = "files")]
483 pub async fn upload_files(self, parameters: FileUpload) -> Result<FileData, Box<dyn Error>> {
484 let client = self.https_client;
485 let url = String::from("https://api.openai.com/v1/files");
486
487 let result = client
488 .post(url)
489 .bearer_auth(self.token)
490 .send_json(¶meters)
491 .await
492 .unwrap()
493 .body()
494 .await
495 .unwrap();
496
497 Ok(serde_json::from_slice::<FileData>(&result).expect("Failed to parse file data"))
498 }
499
500 /// The function `delete_file` is an asynchronous function in Rust that sends a DELETE request to the
501 /// OpenAI API to delete a file.
502 ///
503 /// Arguments:
504 ///
505 /// * `file_id`: The `file_id` parameter is a string that represents the unique identifier of the
506 /// file you want to delete. It is used to construct the URL for the DELETE request to the OpenAI
507 /// API.
508 ///
509 /// Returns:
510 ///
511 /// The function `delete_file` returns a `Result` containing either a `DeleteResponse` or a boxed
512 /// dynamic error (`Box<dyn Error>`).
513 #[cfg(feature = "files")]
514 pub async fn delete_file(self, file_id: String) -> Result<DeleteResponse, Box<dyn Error>> {
515 let client = self.https_client;
516 let url = format!("https://api.openai.com/v1/files/{}", file_id);
517
518 let result = client
519 .delete(url)
520 .bearer_auth(self.token)
521 .send()
522 .await
523 .unwrap()
524 .body()
525 .await
526 .unwrap();
527
528 Ok(serde_json::from_slice::<DeleteResponse>(&result)
529 .expect("Failed to parse delete response"))
530 }
531
532 /// The `retrieve_file` function retrieves file data from the OpenAI API using the provided file ID.
533 ///
534 /// Arguments:
535 ///
536 /// * `file_id`: The `file_id` parameter is a unique identifier for the file you want to retrieve.
537 /// It is used to construct the URL for the API request to retrieve the file data.
538 ///
539 /// Returns:
540 ///
541 /// The function `retrieve_file` returns a `Result` containing either a `FileData` object or an
542 /// error (`Box<dyn Error>`).
543 #[cfg(feature = "files")]
544 pub async fn retrieve_file(self, file_id: String) -> Result<FileData, Box<dyn Error>> {
545 let client = self.https_client;
546 let url = format!("https://api.openai.com/v1/files/{}", file_id);
547
548 let result = client
549 .get(url)
550 .bearer_auth(self.token)
551 .send()
552 .await
553 .unwrap()
554 .body()
555 .await
556 .unwrap();
557
558 Ok(serde_json::from_slice::<FileData>(&result).expect("Failed to parse file data"))
559 }
560
561 /// The function `retrieve_file_content` retrieves the content of a file from the OpenAI API using a
562 /// provided file ID.
563 ///
564 /// Arguments:
565 ///
566 /// * `file_id`: The `file_id` parameter is a unique identifier for the file you want to retrieve
567 /// the content of. It is used to construct the URL for the API request to fetch the file content.
568 ///
569 /// Returns:
570 ///
571 /// The function `retrieve_file_content` returns a `Result` containing a `String` representing the
572 /// content of the file with the given `file_id`. The `Ok` variant of the `Result` contains the file
573 /// content as a `String`, while the `Err` variant contains a boxed dynamic error (`Box<dyn
574 /// Error>`).
575 #[cfg(feature = "files")]
576 pub async fn retrieve_file_content(self, file_id: String) -> Result<String, Box<dyn Error>> {
577 let client = self.https_client;
578 let url = format!("https://api.openai.com/v1/files/{}/content", file_id);
579
580 let result = client
581 .get(url)
582 .bearer_auth(self.token)
583 .send()
584 .await
585 .unwrap()
586 .body()
587 .await
588 .unwrap();
589
590 Ok(String::from_utf8(result.to_vec()).expect("Failed to parse file content"))
591 }
592
593 /// The function `create_fine_tune` sends a POST request to the OpenAI API to create a fine-tuned
594 /// model and returns the retrieved data.
595 ///
596 /// Arguments:
597 ///
598 /// * `parameters`: The `parameters` parameter in the `create_fine_tune` function is of type
599 /// `CreateFineTuneParameters`. It is an input parameter that contains the data required to create a
600 /// fine-tune task. The specific structure and fields of the `CreateFineTuneParameters` type are not
601 ///
602 /// Returns:
603 ///
604 /// a Result object with the type FineTuneRetriveData.
605 #[cfg(feature = "fine_tunes")]
606 pub async fn create_fine_tune(
607 self,
608 parameters: CreateFineTuneParameters,
609 ) -> Result<FineTuneRetriveData, Box<dyn Error>> {
610 let client = self.https_client;
611 let url = String::from("https://api.openai.com/v1/fine-tunes");
612
613 let result = client
614 .post(url)
615 .bearer_auth(self.token)
616 .send_json(¶meters)
617 .await
618 .unwrap()
619 .body()
620 .await
621 .unwrap();
622
623 Ok(serde_json::from_slice::<FineTuneRetriveData>(&result)
624 .expect("Failed to parse fine tune data"))
625 }
626
627 /// The function `list_fine_tunes` makes an HTTP GET request to the OpenAI API to retrieve a list of
628 /// fine-tuned models.
629 ///
630 /// Returns:
631 ///
632 /// a Result object with the type FineTuneList.
633 #[cfg(feature = "fine_tunes")]
634 pub async fn list_fine_tunes(self) -> Result<FineTuneList, Box<dyn Error>> {
635 let client = self.https_client;
636 let url = String::from("https://api.openai.com/v1/fine-tunes");
637
638 let result = client
639 .get(url)
640 .bearer_auth(self.token)
641 .send()
642 .await
643 .unwrap()
644 .body()
645 .await
646 .unwrap();
647
648 Ok(
649 serde_json::from_slice::<FineTuneList>(&result)
650 .expect("Failed to parse fine tune list"),
651 )
652 }
653
654 /// The function retrieves fine-tune data from the OpenAI API using the provided fine-tune ID.
655 ///
656 /// Arguments:
657 ///
658 /// * `fine_tune_id`: The `fine_tune_id` parameter is a unique identifier for a specific fine-tuning
659 /// job. It is used to retrieve the data associated with that fine-tuning job from the OpenAI API.
660 ///
661 /// Returns:
662 ///
663 /// a `Result` type with the success variant containing a `FineTuneRetriveData` object and the error
664 /// variant containing a `Box<dyn Error>` object.
665 #[cfg(feature = "fine_tunes")]
666 pub async fn retrive_fine_tune(
667 self,
668 fine_tune_id: String,
669 ) -> Result<FineTuneRetriveData, Box<dyn Error>> {
670 let client = self.https_client;
671 let url = format!("https://api.openai.com/v1/fine-tunes/{}", fine_tune_id);
672
673 let result = client
674 .get(url)
675 .bearer_auth(self.token)
676 .send()
677 .await
678 .unwrap()
679 .body()
680 .await
681 .unwrap();
682
683 Ok(serde_json::from_slice::<FineTuneRetriveData>(&result)
684 .expect("Failed to parse fine tune data"))
685 }
686
687 /// The `cancel_fine_tune` function cancels a fine-tuning process by sending a POST request to the
688 /// OpenAI API.
689 ///
690 /// Arguments:
691 ///
692 /// * `fine_tune_id`: The `fine_tune_id` parameter is a unique identifier for a fine-tuning process.
693 /// It is used to specify which fine-tuning process you want to cancel.
694 ///
695 /// Returns:
696 ///
697 /// a Result object with the type FineTuneRetriveData.
698 #[cfg(feature = "fine_tunes")]
699 pub async fn cancel_fine_tune(
700 self,
701 fine_tune_id: String,
702 ) -> Result<FineTuneRetriveData, Box<dyn Error>> {
703 let client = self.https_client;
704 let url = format!(
705 "https://api.openai.com/v1/fine-tunes/{}/cancel",
706 fine_tune_id
707 );
708
709 let result = client
710 .post(url)
711 .bearer_auth(self.token)
712 .send()
713 .await
714 .unwrap()
715 .body()
716 .await
717 .unwrap();
718
719 Ok(serde_json::from_slice::<FineTuneRetriveData>(&result)
720 .expect("Failed to parse fine tune data"))
721 }
722
723 /// The function `list_fine_tune_events` is an asynchronous function in Rust that retrieves a list
724 /// of fine-tune events from the OpenAI API.
725 ///
726 /// Arguments:
727 ///
728 /// * `fine_tune_id`: The `fine_tune_id` parameter is a unique identifier for a fine-tuning job. It
729 /// is used to specify which fine-tuning job's events you want to retrieve.
730 ///
731 /// Returns:
732 ///
733 /// a `Result` type with the `Ok` variant containing a `FineTuneEventList` object and the `Err`
734 /// variant containing a `Box<dyn Error>` object.
735 #[cfg(feature = "fine_tunes")]
736 pub async fn list_fine_tune_events(
737 self,
738 fine_tune_id: String,
739 ) -> Result<FineTuneEventList, Box<dyn Error>> {
740 let client = self.https_client;
741 let url = format!(
742 "https://api.openai.com/v1/fine-tunes/{}/events",
743 fine_tune_id
744 );
745
746 let result = client
747 .get(url)
748 .bearer_auth(self.token)
749 .send()
750 .await
751 .unwrap()
752 .body()
753 .await
754 .unwrap();
755
756 Ok(serde_json::from_slice::<FineTuneEventList>(&result)
757 .expect("Failed to parse fine tune event list"))
758 }
759
760 /// The function `delete_fine_tune` sends a DELETE request to the OpenAI API to delete a fine-tuned
761 /// model and returns the result as a `FineTuneDelete` object.
762 ///
763 /// Arguments:
764 ///
765 /// * `model`: The `model` parameter is a string that represents the name or ID of the fine-tuned
766 /// model that you want to delete.
767 ///
768 /// Returns:
769 ///
770 /// a `Result` enum with the success variant containing a `FineTuneDelete` object or the error
771 /// variant containing a `Box<dyn Error>` object.
772 #[cfg(feature = "fine_tunes")]
773 pub async fn delete_fine_tune(self, model: String) -> Result<FineTuneDelete, Box<dyn Error>> {
774 let client = self.https_client;
775 let url = format!("https://api.openai.com/v1/models/{}", model);
776
777 let result = client
778 .delete(url)
779 .bearer_auth(self.token)
780 .send()
781 .await
782 .unwrap()
783 .body()
784 .await
785 .unwrap();
786
787 Ok(serde_json::from_slice::<FineTuneDelete>(&result)
788 .expect("Failed to parse fine tune delete"))
789 }
790
791 /// The function `create_moderation` sends a POST request to the OpenAI API to create a text
792 /// moderation task and returns the result.
793 ///
794 /// Arguments:
795 ///
796 /// * `parameters`: The `parameters` parameter in the `create_moderation` function is of type
797 /// `TextModerationParameters`. It represents the input parameters for the text moderation request.
798 /// The specific structure and fields of the `TextModerationParameters` type are not provided in the
799 /// code snippet, so it would be
800 ///
801 /// Returns:
802 ///
803 /// a `Result` type with the success variant containing a `TextModerationResult` and the error
804 /// variant containing a `Box<dyn Error>`.
805 #[cfg(feature = "moderations")]
806 pub async fn create_moderation(
807 self,
808 parameters: TextModerationParameters,
809 ) -> Result<TextModerationResult, Box<dyn Error>> {
810 let client = self.https_client;
811 let url = String::from("https://api.openai.com/v1/moderations");
812
813 let result = client
814 .post(url)
815 .bearer_auth(self.token)
816 .send_json(¶meters)
817 .await
818 .unwrap()
819 .body()
820 .await
821 .unwrap();
822
823 Ok(serde_json::from_slice::<TextModerationResult>(&result)
824 .expect("Failed to parse text moderation result"))
825 }
826}