1use std::fmt::Display;
2
3use bytes::Bytes;
4
5use super::{
6 AddUploadPartRequest, AudioInput, AudioResponseFormat, ChatCompletionFunctionCall,
7 ChatCompletionFunctions, ChatCompletionNamedToolChoice, ChatCompletionRequestAssistantMessage,
8 ChatCompletionRequestAssistantMessageContent, ChatCompletionRequestDeveloperMessage,
9 ChatCompletionRequestDeveloperMessageContent, ChatCompletionRequestFunctionMessage,
10 ChatCompletionRequestMessage, ChatCompletionRequestMessageContentPartAudio,
11 ChatCompletionRequestMessageContentPartImage, ChatCompletionRequestMessageContentPartText,
12 ChatCompletionRequestSystemMessage, ChatCompletionRequestSystemMessageContent,
13 ChatCompletionRequestToolMessage, ChatCompletionRequestToolMessageContent,
14 ChatCompletionRequestUserMessage, ChatCompletionRequestUserMessageContent,
15 ChatCompletionRequestUserMessageContentPart, ChatCompletionToolChoiceOption, CreateFileRequest,
16 CreateImageEditRequest, CreateImageVariationRequest, CreateMessageRequestContent,
17 CreateTranscriptionRequest, CreateTranslationRequest, CreateVideoRequest, DallE2ImageSize,
18 EmbeddingInput, FileExpiresAfterAnchor, FileInput, FilePurpose, FunctionName, ImageInput,
19 ImageModel, ImageResponseFormat, ImageSize, ImageUrl, ModerationInput, Prompt, Role, Stop,
20 TimestampGranularity, VideoSize,
21 responses::{CodeInterpreterContainer, Input, InputContent, Role as ResponsesRole},
22};
23use crate::traits::AsyncTryFrom;
24use crate::{error::OpenAIError, types::InputSource, util::create_file_part};
25
26macro_rules! impl_from {
35 ($from_typ:ty, $to_typ:ty) => {
36 impl From<$from_typ> for $to_typ {
38 fn from(value: $from_typ) -> Self {
39 <$to_typ>::String(value.into())
40 }
41 }
42
43 impl From<Vec<$from_typ>> for $to_typ {
45 fn from(value: Vec<$from_typ>) -> Self {
46 <$to_typ>::StringArray(value.iter().map(|v| v.to_string()).collect())
47 }
48 }
49
50 impl From<&Vec<$from_typ>> for $to_typ {
52 fn from(value: &Vec<$from_typ>) -> Self {
53 <$to_typ>::StringArray(value.iter().map(|v| v.to_string()).collect())
54 }
55 }
56
57 impl<const N: usize> From<[$from_typ; N]> for $to_typ {
59 fn from(value: [$from_typ; N]) -> Self {
60 <$to_typ>::StringArray(value.into_iter().map(|v| v.to_string()).collect())
61 }
62 }
63
64 impl<const N: usize> From<&[$from_typ; N]> for $to_typ {
66 fn from(value: &[$from_typ; N]) -> Self {
67 <$to_typ>::StringArray(value.into_iter().map(|v| v.to_string()).collect())
68 }
69 }
70 };
71}
72
73impl_from!(&str, Prompt);
75impl_from!(String, Prompt);
76impl_from!(&String, Prompt);
77
78impl_from!(&str, Stop);
80impl_from!(String, Stop);
81impl_from!(&String, Stop);
82
83impl_from!(&str, ModerationInput);
85impl_from!(String, ModerationInput);
86impl_from!(&String, ModerationInput);
87
88impl_from!(&str, EmbeddingInput);
90impl_from!(String, EmbeddingInput);
91impl_from!(&String, EmbeddingInput);
92
93macro_rules! impl_default {
95 ($for_typ:ty) => {
96 impl Default for $for_typ {
97 fn default() -> Self {
98 Self::String("".into())
99 }
100 }
101 };
102}
103
104impl_default!(Prompt);
105impl_default!(ModerationInput);
106impl_default!(EmbeddingInput);
107
108impl Default for InputSource {
109 fn default() -> Self {
110 const EMPTY_STR: String = String::new();
111 const EMPTY_VEC: Vec<u8> = Vec::new();
112 InputSource::VecU8 {
113 filename: EMPTY_STR,
114 vec: EMPTY_VEC,
115 }
116 }
117}
118
119macro_rules! impl_input {
128 ($for_typ:ty) => {
129 impl $for_typ {
130 pub fn from_bytes(filename: String, bytes: Bytes) -> Self {
131 Self {
132 source: InputSource::Bytes { filename, bytes },
133 }
134 }
135
136 pub fn from_vec_u8(filename: String, vec: Vec<u8>) -> Self {
137 Self {
138 source: InputSource::VecU8 { filename, vec },
139 }
140 }
141 }
142 };
143}
144
145impl_input!(AudioInput);
146impl_input!(FileInput);
147impl_input!(ImageInput);
148
149impl Display for VideoSize {
150 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
151 write!(
152 f,
153 "{}",
154 match self {
155 Self::S720x1280 => "720x1280",
156 Self::S1280x720 => "1280x720",
157 Self::S1024x1792 => "1024x1792",
158 Self::S1792x1024 => "1792x1024",
159 }
160 )
161 }
162}
163
164impl Display for ImageSize {
165 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
166 write!(
167 f,
168 "{}",
169 match self {
170 Self::S256x256 => "256x256",
171 Self::S512x512 => "512x512",
172 Self::S1024x1024 => "1024x1024",
173 Self::S1792x1024 => "1792x1024",
174 Self::S1024x1792 => "1024x1792",
175 }
176 )
177 }
178}
179
180impl Display for DallE2ImageSize {
181 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
182 write!(
183 f,
184 "{}",
185 match self {
186 Self::S256x256 => "256x256",
187 Self::S512x512 => "512x512",
188 Self::S1024x1024 => "1024x1024",
189 }
190 )
191 }
192}
193
194impl Display for ImageModel {
195 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
196 write!(
197 f,
198 "{}",
199 match self {
200 Self::DallE2 => "dall-e-2",
201 Self::DallE3 => "dall-e-3",
202 Self::Other(other) => other,
203 }
204 )
205 }
206}
207
208impl Display for ImageResponseFormat {
209 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
210 write!(
211 f,
212 "{}",
213 match self {
214 Self::Url => "url",
215 Self::B64Json => "b64_json",
216 }
217 )
218 }
219}
220
221impl Display for AudioResponseFormat {
222 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
223 write!(
224 f,
225 "{}",
226 match self {
227 AudioResponseFormat::Json => "json",
228 AudioResponseFormat::Srt => "srt",
229 AudioResponseFormat::Text => "text",
230 AudioResponseFormat::VerboseJson => "verbose_json",
231 AudioResponseFormat::Vtt => "vtt",
232 }
233 )
234 }
235}
236
237impl Display for TimestampGranularity {
238 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
239 write!(
240 f,
241 "{}",
242 match self {
243 TimestampGranularity::Word => "word",
244 TimestampGranularity::Segment => "segment",
245 }
246 )
247 }
248}
249
250impl Display for Role {
251 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
252 write!(
253 f,
254 "{}",
255 match self {
256 Role::User => "user",
257 Role::System => "system",
258 Role::Assistant => "assistant",
259 Role::Function => "function",
260 Role::Tool => "tool",
261 }
262 )
263 }
264}
265
266impl Display for FilePurpose {
267 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
268 write!(
269 f,
270 "{}",
271 match self {
272 Self::Assistants => "assistants",
273 Self::Batch => "batch",
274 Self::FineTune => "fine-tune",
275 Self::Vision => "vision",
276 }
277 )
278 }
279}
280
281impl Display for FileExpiresAfterAnchor {
282 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
283 write!(
284 f,
285 "{}",
286 match self {
287 Self::CreatedAt => "created_at",
288 }
289 )
290 }
291}
292
293macro_rules! impl_from_for_integer_array {
294 ($from_typ:ty, $to_typ:ty) => {
295 impl<const N: usize> From<[$from_typ; N]> for $to_typ {
296 fn from(value: [$from_typ; N]) -> Self {
297 Self::IntegerArray(value.to_vec())
298 }
299 }
300
301 impl<const N: usize> From<&[$from_typ; N]> for $to_typ {
302 fn from(value: &[$from_typ; N]) -> Self {
303 Self::IntegerArray(value.to_vec())
304 }
305 }
306
307 impl From<Vec<$from_typ>> for $to_typ {
308 fn from(value: Vec<$from_typ>) -> Self {
309 Self::IntegerArray(value)
310 }
311 }
312
313 impl From<&Vec<$from_typ>> for $to_typ {
314 fn from(value: &Vec<$from_typ>) -> Self {
315 Self::IntegerArray(value.clone())
316 }
317 }
318 };
319}
320
321impl_from_for_integer_array!(u32, EmbeddingInput);
322impl_from_for_integer_array!(u32, Prompt);
323
324macro_rules! impl_from_for_array_of_integer_array {
325 ($from_typ:ty, $to_typ:ty) => {
326 impl From<Vec<Vec<$from_typ>>> for $to_typ {
327 fn from(value: Vec<Vec<$from_typ>>) -> Self {
328 Self::ArrayOfIntegerArray(value)
329 }
330 }
331
332 impl From<&Vec<Vec<$from_typ>>> for $to_typ {
333 fn from(value: &Vec<Vec<$from_typ>>) -> Self {
334 Self::ArrayOfIntegerArray(value.clone())
335 }
336 }
337
338 impl<const M: usize, const N: usize> From<[[$from_typ; N]; M]> for $to_typ {
339 fn from(value: [[$from_typ; N]; M]) -> Self {
340 Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
341 }
342 }
343
344 impl<const M: usize, const N: usize> From<[&[$from_typ; N]; M]> for $to_typ {
345 fn from(value: [&[$from_typ; N]; M]) -> Self {
346 Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
347 }
348 }
349
350 impl<const M: usize, const N: usize> From<&[[$from_typ; N]; M]> for $to_typ {
351 fn from(value: &[[$from_typ; N]; M]) -> Self {
352 Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
353 }
354 }
355
356 impl<const M: usize, const N: usize> From<&[&[$from_typ; N]; M]> for $to_typ {
357 fn from(value: &[&[$from_typ; N]; M]) -> Self {
358 Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
359 }
360 }
361
362 impl<const N: usize> From<[Vec<$from_typ>; N]> for $to_typ {
363 fn from(value: [Vec<$from_typ>; N]) -> Self {
364 Self::ArrayOfIntegerArray(value.to_vec())
365 }
366 }
367
368 impl<const N: usize> From<&[Vec<$from_typ>; N]> for $to_typ {
369 fn from(value: &[Vec<$from_typ>; N]) -> Self {
370 Self::ArrayOfIntegerArray(value.to_vec())
371 }
372 }
373
374 impl<const N: usize> From<[&Vec<$from_typ>; N]> for $to_typ {
375 fn from(value: [&Vec<$from_typ>; N]) -> Self {
376 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.clone()).collect())
377 }
378 }
379
380 impl<const N: usize> From<&[&Vec<$from_typ>; N]> for $to_typ {
381 fn from(value: &[&Vec<$from_typ>; N]) -> Self {
382 Self::ArrayOfIntegerArray(
383 value
384 .to_vec()
385 .into_iter()
386 .map(|inner| inner.clone())
387 .collect(),
388 )
389 }
390 }
391
392 impl<const N: usize> From<Vec<[$from_typ; N]>> for $to_typ {
393 fn from(value: Vec<[$from_typ; N]>) -> Self {
394 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
395 }
396 }
397
398 impl<const N: usize> From<&Vec<[$from_typ; N]>> for $to_typ {
399 fn from(value: &Vec<[$from_typ; N]>) -> Self {
400 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
401 }
402 }
403
404 impl<const N: usize> From<Vec<&[$from_typ; N]>> for $to_typ {
405 fn from(value: Vec<&[$from_typ; N]>) -> Self {
406 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
407 }
408 }
409
410 impl<const N: usize> From<&Vec<&[$from_typ; N]>> for $to_typ {
411 fn from(value: &Vec<&[$from_typ; N]>) -> Self {
412 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
413 }
414 }
415 };
416}
417
418impl_from_for_array_of_integer_array!(u32, EmbeddingInput);
419impl_from_for_array_of_integer_array!(u32, Prompt);
420
421impl From<&str> for ChatCompletionFunctionCall {
422 fn from(value: &str) -> Self {
423 match value {
424 "auto" => Self::Auto,
425 "none" => Self::None,
426 _ => Self::Function { name: value.into() },
427 }
428 }
429}
430
431impl From<&str> for FunctionName {
432 fn from(value: &str) -> Self {
433 Self { name: value.into() }
434 }
435}
436
437impl From<String> for FunctionName {
438 fn from(value: String) -> Self {
439 Self { name: value }
440 }
441}
442
443impl From<&str> for ChatCompletionNamedToolChoice {
444 fn from(value: &str) -> Self {
445 Self {
446 r#type: super::ChatCompletionToolType::Function,
447 function: value.into(),
448 }
449 }
450}
451
452impl From<String> for ChatCompletionNamedToolChoice {
453 fn from(value: String) -> Self {
454 Self {
455 r#type: super::ChatCompletionToolType::Function,
456 function: value.into(),
457 }
458 }
459}
460
461impl From<&str> for ChatCompletionToolChoiceOption {
462 fn from(value: &str) -> Self {
463 match value {
464 "auto" => Self::Auto,
465 "none" => Self::None,
466 _ => Self::Named(value.into()),
467 }
468 }
469}
470
471impl From<String> for ChatCompletionToolChoiceOption {
472 fn from(value: String) -> Self {
473 match value.as_str() {
474 "auto" => Self::Auto,
475 "none" => Self::None,
476 _ => Self::Named(value.into()),
477 }
478 }
479}
480
481impl From<(String, serde_json::Value)> for ChatCompletionFunctions {
482 fn from(value: (String, serde_json::Value)) -> Self {
483 Self {
484 name: value.0,
485 description: None,
486 parameters: value.1,
487 }
488 }
489}
490
491impl From<ChatCompletionRequestUserMessage> for ChatCompletionRequestMessage {
494 fn from(value: ChatCompletionRequestUserMessage) -> Self {
495 Self::User(value)
496 }
497}
498
499impl From<ChatCompletionRequestSystemMessage> for ChatCompletionRequestMessage {
500 fn from(value: ChatCompletionRequestSystemMessage) -> Self {
501 Self::System(value)
502 }
503}
504
505impl From<ChatCompletionRequestDeveloperMessage> for ChatCompletionRequestMessage {
506 fn from(value: ChatCompletionRequestDeveloperMessage) -> Self {
507 Self::Developer(value)
508 }
509}
510
511impl From<ChatCompletionRequestAssistantMessage> for ChatCompletionRequestMessage {
512 fn from(value: ChatCompletionRequestAssistantMessage) -> Self {
513 Self::Assistant(value)
514 }
515}
516
517impl From<ChatCompletionRequestFunctionMessage> for ChatCompletionRequestMessage {
518 fn from(value: ChatCompletionRequestFunctionMessage) -> Self {
519 Self::Function(value)
520 }
521}
522
523impl From<ChatCompletionRequestToolMessage> for ChatCompletionRequestMessage {
524 fn from(value: ChatCompletionRequestToolMessage) -> Self {
525 Self::Tool(value)
526 }
527}
528
529impl From<ChatCompletionRequestUserMessageContent> for ChatCompletionRequestUserMessage {
530 fn from(value: ChatCompletionRequestUserMessageContent) -> Self {
531 Self {
532 content: value,
533 name: None,
534 }
535 }
536}
537
538impl From<ChatCompletionRequestSystemMessageContent> for ChatCompletionRequestSystemMessage {
539 fn from(value: ChatCompletionRequestSystemMessageContent) -> Self {
540 Self {
541 content: value,
542 name: None,
543 }
544 }
545}
546
547impl From<ChatCompletionRequestDeveloperMessageContent> for ChatCompletionRequestDeveloperMessage {
548 fn from(value: ChatCompletionRequestDeveloperMessageContent) -> Self {
549 Self {
550 content: value,
551 name: None,
552 }
553 }
554}
555
556impl From<ChatCompletionRequestAssistantMessageContent> for ChatCompletionRequestAssistantMessage {
557 fn from(value: ChatCompletionRequestAssistantMessageContent) -> Self {
558 Self {
559 content: Some(value),
560 ..Default::default()
561 }
562 }
563}
564
565impl From<&str> for ChatCompletionRequestUserMessageContent {
566 fn from(value: &str) -> Self {
567 ChatCompletionRequestUserMessageContent::Text(value.into())
568 }
569}
570
571impl From<String> for ChatCompletionRequestUserMessageContent {
572 fn from(value: String) -> Self {
573 ChatCompletionRequestUserMessageContent::Text(value)
574 }
575}
576
577impl From<&str> for ChatCompletionRequestSystemMessageContent {
578 fn from(value: &str) -> Self {
579 ChatCompletionRequestSystemMessageContent::Text(value.into())
580 }
581}
582
583impl From<String> for ChatCompletionRequestSystemMessageContent {
584 fn from(value: String) -> Self {
585 ChatCompletionRequestSystemMessageContent::Text(value)
586 }
587}
588
589impl From<&str> for ChatCompletionRequestDeveloperMessageContent {
590 fn from(value: &str) -> Self {
591 ChatCompletionRequestDeveloperMessageContent::Text(value.into())
592 }
593}
594
595impl From<String> for ChatCompletionRequestDeveloperMessageContent {
596 fn from(value: String) -> Self {
597 ChatCompletionRequestDeveloperMessageContent::Text(value)
598 }
599}
600
601impl From<&str> for ChatCompletionRequestAssistantMessageContent {
602 fn from(value: &str) -> Self {
603 ChatCompletionRequestAssistantMessageContent::Text(value.into())
604 }
605}
606
607impl From<String> for ChatCompletionRequestAssistantMessageContent {
608 fn from(value: String) -> Self {
609 ChatCompletionRequestAssistantMessageContent::Text(value)
610 }
611}
612
613impl From<&str> for ChatCompletionRequestToolMessageContent {
614 fn from(value: &str) -> Self {
615 ChatCompletionRequestToolMessageContent::Text(value.into())
616 }
617}
618
619impl From<String> for ChatCompletionRequestToolMessageContent {
620 fn from(value: String) -> Self {
621 ChatCompletionRequestToolMessageContent::Text(value)
622 }
623}
624
625impl From<&str> for ChatCompletionRequestUserMessage {
626 fn from(value: &str) -> Self {
627 ChatCompletionRequestUserMessageContent::Text(value.into()).into()
628 }
629}
630
631impl From<String> for ChatCompletionRequestUserMessage {
632 fn from(value: String) -> Self {
633 value.as_str().into()
634 }
635}
636
637impl From<&str> for ChatCompletionRequestSystemMessage {
638 fn from(value: &str) -> Self {
639 ChatCompletionRequestSystemMessageContent::Text(value.into()).into()
640 }
641}
642
643impl From<&str> for ChatCompletionRequestDeveloperMessage {
644 fn from(value: &str) -> Self {
645 ChatCompletionRequestDeveloperMessageContent::Text(value.into()).into()
646 }
647}
648
649impl From<String> for ChatCompletionRequestSystemMessage {
650 fn from(value: String) -> Self {
651 value.as_str().into()
652 }
653}
654
655impl From<String> for ChatCompletionRequestDeveloperMessage {
656 fn from(value: String) -> Self {
657 value.as_str().into()
658 }
659}
660
661impl From<&str> for ChatCompletionRequestAssistantMessage {
662 fn from(value: &str) -> Self {
663 ChatCompletionRequestAssistantMessageContent::Text(value.into()).into()
664 }
665}
666
667impl From<String> for ChatCompletionRequestAssistantMessage {
668 fn from(value: String) -> Self {
669 value.as_str().into()
670 }
671}
672
673impl From<Vec<ChatCompletionRequestUserMessageContentPart>>
674 for ChatCompletionRequestUserMessageContent
675{
676 fn from(value: Vec<ChatCompletionRequestUserMessageContentPart>) -> Self {
677 ChatCompletionRequestUserMessageContent::Array(value)
678 }
679}
680
681impl From<ChatCompletionRequestMessageContentPartText>
682 for ChatCompletionRequestUserMessageContentPart
683{
684 fn from(value: ChatCompletionRequestMessageContentPartText) -> Self {
685 ChatCompletionRequestUserMessageContentPart::Text(value)
686 }
687}
688
689impl From<ChatCompletionRequestMessageContentPartImage>
690 for ChatCompletionRequestUserMessageContentPart
691{
692 fn from(value: ChatCompletionRequestMessageContentPartImage) -> Self {
693 ChatCompletionRequestUserMessageContentPart::ImageUrl(value)
694 }
695}
696
697impl From<ChatCompletionRequestMessageContentPartAudio>
698 for ChatCompletionRequestUserMessageContentPart
699{
700 fn from(value: ChatCompletionRequestMessageContentPartAudio) -> Self {
701 ChatCompletionRequestUserMessageContentPart::InputAudio(value)
702 }
703}
704
705impl From<&str> for ChatCompletionRequestMessageContentPartText {
706 fn from(value: &str) -> Self {
707 ChatCompletionRequestMessageContentPartText { text: value.into() }
708 }
709}
710
711impl From<String> for ChatCompletionRequestMessageContentPartText {
712 fn from(value: String) -> Self {
713 ChatCompletionRequestMessageContentPartText { text: value }
714 }
715}
716
717impl From<&str> for ImageUrl {
718 fn from(value: &str) -> Self {
719 Self {
720 url: value.into(),
721 detail: Default::default(),
722 }
723 }
724}
725
726impl From<String> for ImageUrl {
727 fn from(value: String) -> Self {
728 Self {
729 url: value,
730 detail: Default::default(),
731 }
732 }
733}
734
735impl From<String> for CreateMessageRequestContent {
736 fn from(value: String) -> Self {
737 Self::Content(value)
738 }
739}
740
741impl From<&str> for CreateMessageRequestContent {
742 fn from(value: &str) -> Self {
743 Self::Content(value.to_string())
744 }
745}
746
747impl Default for ChatCompletionRequestUserMessageContent {
748 fn default() -> Self {
749 ChatCompletionRequestUserMessageContent::Text("".into())
750 }
751}
752
753impl Default for CreateMessageRequestContent {
754 fn default() -> Self {
755 Self::Content("".into())
756 }
757}
758
759impl Default for ChatCompletionRequestDeveloperMessageContent {
760 fn default() -> Self {
761 ChatCompletionRequestDeveloperMessageContent::Text("".into())
762 }
763}
764
765impl Default for ChatCompletionRequestSystemMessageContent {
766 fn default() -> Self {
767 ChatCompletionRequestSystemMessageContent::Text("".into())
768 }
769}
770
771impl Default for ChatCompletionRequestToolMessageContent {
772 fn default() -> Self {
773 ChatCompletionRequestToolMessageContent::Text("".into())
774 }
775}
776
777impl AsyncTryFrom<CreateTranscriptionRequest> for reqwest::multipart::Form {
780 type Error = OpenAIError;
781
782 async fn try_from(request: CreateTranscriptionRequest) -> Result<Self, Self::Error> {
783 let audio_part = create_file_part(request.file.source).await?;
784
785 let mut form = reqwest::multipart::Form::new()
786 .part("file", audio_part)
787 .text("model", request.model);
788
789 if let Some(prompt) = request.prompt {
790 form = form.text("prompt", prompt);
791 }
792
793 if let Some(response_format) = request.response_format {
794 form = form.text("response_format", response_format.to_string())
795 }
796
797 if let Some(temperature) = request.temperature {
798 form = form.text("temperature", temperature.to_string())
799 }
800
801 if let Some(language) = request.language {
802 form = form.text("language", language);
803 }
804
805 if let Some(timestamp_granularities) = request.timestamp_granularities {
806 for tg in timestamp_granularities {
807 form = form.text("timestamp_granularities[]", tg.to_string());
808 }
809 }
810
811 Ok(form)
812 }
813}
814
815impl AsyncTryFrom<CreateTranslationRequest> for reqwest::multipart::Form {
816 type Error = OpenAIError;
817
818 async fn try_from(request: CreateTranslationRequest) -> Result<Self, Self::Error> {
819 let audio_part = create_file_part(request.file.source).await?;
820
821 let mut form = reqwest::multipart::Form::new()
822 .part("file", audio_part)
823 .text("model", request.model);
824
825 if let Some(prompt) = request.prompt {
826 form = form.text("prompt", prompt);
827 }
828
829 if let Some(response_format) = request.response_format {
830 form = form.text("response_format", response_format.to_string())
831 }
832
833 if let Some(temperature) = request.temperature {
834 form = form.text("temperature", temperature.to_string())
835 }
836 Ok(form)
837 }
838}
839
840impl AsyncTryFrom<CreateImageEditRequest> for reqwest::multipart::Form {
841 type Error = OpenAIError;
842
843 async fn try_from(request: CreateImageEditRequest) -> Result<Self, Self::Error> {
844 let image_part = create_file_part(request.image.source).await?;
845
846 let mut form = reqwest::multipart::Form::new()
847 .part("image", image_part)
848 .text("prompt", request.prompt);
849
850 if let Some(mask) = request.mask {
851 let mask_part = create_file_part(mask.source).await?;
852 form = form.part("mask", mask_part);
853 }
854
855 if let Some(model) = request.model {
856 form = form.text("model", model.to_string())
857 }
858
859 if request.n.is_some() {
860 form = form.text("n", request.n.unwrap().to_string())
861 }
862
863 if request.size.is_some() {
864 form = form.text("size", request.size.unwrap().to_string())
865 }
866
867 if request.response_format.is_some() {
868 form = form.text(
869 "response_format",
870 request.response_format.unwrap().to_string(),
871 )
872 }
873
874 if request.user.is_some() {
875 form = form.text("user", request.user.unwrap())
876 }
877 Ok(form)
878 }
879}
880
881impl AsyncTryFrom<CreateImageVariationRequest> for reqwest::multipart::Form {
882 type Error = OpenAIError;
883
884 async fn try_from(request: CreateImageVariationRequest) -> Result<Self, Self::Error> {
885 let image_part = create_file_part(request.image.source).await?;
886
887 let mut form = reqwest::multipart::Form::new().part("image", image_part);
888
889 if let Some(model) = request.model {
890 form = form.text("model", model.to_string())
891 }
892
893 if request.n.is_some() {
894 form = form.text("n", request.n.unwrap().to_string())
895 }
896
897 if request.size.is_some() {
898 form = form.text("size", request.size.unwrap().to_string())
899 }
900
901 if request.response_format.is_some() {
902 form = form.text(
903 "response_format",
904 request.response_format.unwrap().to_string(),
905 )
906 }
907
908 if request.user.is_some() {
909 form = form.text("user", request.user.unwrap())
910 }
911 Ok(form)
912 }
913}
914
915impl AsyncTryFrom<CreateFileRequest> for reqwest::multipart::Form {
916 type Error = OpenAIError;
917
918 async fn try_from(request: CreateFileRequest) -> Result<Self, Self::Error> {
919 let file_part = create_file_part(request.file.source).await?;
920 let mut form = reqwest::multipart::Form::new()
921 .part("file", file_part)
922 .text("purpose", request.purpose.to_string());
923
924 if let Some(expires_after) = request.expires_after {
925 form = form
926 .text("expires_after[anchor]", expires_after.anchor.to_string())
927 .text("expires_after[seconds]", expires_after.seconds.to_string());
928 }
929 Ok(form)
930 }
931}
932
933impl AsyncTryFrom<AddUploadPartRequest> for reqwest::multipart::Form {
934 type Error = OpenAIError;
935
936 async fn try_from(request: AddUploadPartRequest) -> Result<Self, Self::Error> {
937 let file_part = create_file_part(request.data).await?;
938 let form = reqwest::multipart::Form::new().part("data", file_part);
939 Ok(form)
940 }
941}
942
943impl AsyncTryFrom<CreateVideoRequest> for reqwest::multipart::Form {
944 type Error = OpenAIError;
945
946 async fn try_from(request: CreateVideoRequest) -> Result<Self, Self::Error> {
947 let mut form = reqwest::multipart::Form::new().text("model", request.model);
948
949 form = form.text("prompt", request.prompt);
950
951 if request.size.is_some() {
952 form = form.text("size", request.size.unwrap().to_string());
953 }
954
955 if request.seconds.is_some() {
956 form = form.text("seconds", request.seconds.unwrap());
957 }
958
959 if request.input_reference.is_some() {
960 let image_part = create_file_part(request.input_reference.unwrap().source).await?;
961 form = form.part("input_reference", image_part);
962 }
963
964 Ok(form)
965 }
966}
967
968impl Default for Input {
971 fn default() -> Self {
972 Self::Text("".to_string())
973 }
974}
975
976impl Default for InputContent {
977 fn default() -> Self {
978 Self::TextInput("".to_string())
979 }
980}
981
982impl From<String> for Input {
983 fn from(value: String) -> Self {
984 Input::Text(value)
985 }
986}
987
988impl From<&str> for Input {
989 fn from(value: &str) -> Self {
990 Input::Text(value.to_owned())
991 }
992}
993
994impl Default for ResponsesRole {
995 fn default() -> Self {
996 Self::User
997 }
998}
999
1000impl From<String> for InputContent {
1001 fn from(value: String) -> Self {
1002 Self::TextInput(value)
1003 }
1004}
1005
1006impl From<&str> for InputContent {
1007 fn from(value: &str) -> Self {
1008 Self::TextInput(value.to_owned())
1009 }
1010}
1011
1012impl Default for CodeInterpreterContainer {
1013 fn default() -> Self {
1014 CodeInterpreterContainer::Id("".to_string())
1015 }
1016}