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