async_openai_types/types/
impls.rs1use std::{
2 fmt::Display,
3 path::{Path, PathBuf},
4};
5
6use crate::types::InputSource;
7
8use bytes::Bytes;
9
10use super::{
11 AudioInput, AudioResponseFormat, ChatCompletionFunctionCall, ChatCompletionNamedToolChoice,
12 ChatCompletionRequestAssistantMessage, ChatCompletionRequestAssistantMessageContent,
13 ChatCompletionRequestFunctionMessage, ChatCompletionRequestMessage,
14 ChatCompletionRequestMessageContentPartImage, ChatCompletionRequestMessageContentPartText,
15 ChatCompletionRequestSystemMessage, ChatCompletionRequestSystemMessageContent,
16 ChatCompletionRequestToolMessage, ChatCompletionRequestToolMessageContent,
17 ChatCompletionRequestUserMessage, ChatCompletionRequestUserMessageContent,
18 ChatCompletionRequestUserMessageContentPart, ChatCompletionToolChoiceOption,
19 CreateMessageRequestContent, DallE2ImageSize, EmbeddingInput, FileInput, FilePurpose,
20 FunctionName, ImageInput, ImageModel, ImageResponseFormat, ImageSize, ImageUrl,
21 ModerationInput, Prompt, Role, Stop, TimestampGranularity,
22};
23
24macro_rules! impl_from {
33 ($from_typ:ty, $to_typ:ty) => {
34 impl From<$from_typ> for $to_typ {
36 fn from(value: $from_typ) -> Self {
37 <$to_typ>::String(value.into())
38 }
39 }
40
41 impl From<Vec<$from_typ>> for $to_typ {
43 fn from(value: Vec<$from_typ>) -> Self {
44 <$to_typ>::StringArray(value.iter().map(|v| v.to_string()).collect())
45 }
46 }
47
48 impl From<&Vec<$from_typ>> for $to_typ {
50 fn from(value: &Vec<$from_typ>) -> Self {
51 <$to_typ>::StringArray(value.iter().map(|v| v.to_string()).collect())
52 }
53 }
54
55 impl<const N: usize> From<[$from_typ; N]> for $to_typ {
57 fn from(value: [$from_typ; N]) -> Self {
58 <$to_typ>::StringArray(value.into_iter().map(|v| v.to_string()).collect())
59 }
60 }
61
62 impl<const N: usize> From<&[$from_typ; N]> for $to_typ {
64 fn from(value: &[$from_typ; N]) -> Self {
65 <$to_typ>::StringArray(value.into_iter().map(|v| v.to_string()).collect())
66 }
67 }
68 };
69}
70
71impl_from!(&str, Prompt);
73impl_from!(String, Prompt);
74impl_from!(&String, Prompt);
75
76impl_from!(&str, Stop);
78impl_from!(String, Stop);
79impl_from!(&String, Stop);
80
81impl_from!(&str, ModerationInput);
83impl_from!(String, ModerationInput);
84impl_from!(&String, ModerationInput);
85
86impl_from!(&str, EmbeddingInput);
88impl_from!(String, EmbeddingInput);
89impl_from!(&String, EmbeddingInput);
90
91macro_rules! impl_default {
93 ($for_typ:ty) => {
94 impl Default for $for_typ {
95 fn default() -> Self {
96 Self::String("".into())
97 }
98 }
99 };
100}
101
102impl_default!(Prompt);
103impl_default!(ModerationInput);
104impl_default!(EmbeddingInput);
105
106impl Default for InputSource {
107 fn default() -> Self {
108 InputSource::Path {
109 path: PathBuf::new(),
110 }
111 }
112}
113
114macro_rules! impl_input {
123 ($for_typ:ty) => {
124 impl $for_typ {
125 pub fn from_bytes(filename: String, bytes: Bytes) -> Self {
126 Self {
127 source: InputSource::Bytes { filename, bytes },
128 }
129 }
130
131 pub fn from_vec_u8(filename: String, vec: Vec<u8>) -> Self {
132 Self {
133 source: InputSource::VecU8 { filename, vec },
134 }
135 }
136 }
137
138 impl<P: AsRef<Path>> From<P> for $for_typ {
139 fn from(path: P) -> Self {
140 let path_buf = path.as_ref().to_path_buf();
141 Self {
142 source: InputSource::Path { path: path_buf },
143 }
144 }
145 }
146 };
147}
148
149impl_input!(AudioInput);
150impl_input!(FileInput);
151impl_input!(ImageInput);
152
153impl Display for ImageSize {
154 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
155 write!(
156 f,
157 "{}",
158 match self {
159 Self::S256x256 => "256x256",
160 Self::S512x512 => "512x512",
161 Self::S1024x1024 => "1024x1024",
162 Self::S1792x1024 => "1792x1024",
163 Self::S1024x1792 => "1024x1792",
164 }
165 )
166 }
167}
168
169impl Display for DallE2ImageSize {
170 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
171 write!(
172 f,
173 "{}",
174 match self {
175 Self::S256x256 => "256x256",
176 Self::S512x512 => "512x512",
177 Self::S1024x1024 => "1024x1024",
178 }
179 )
180 }
181}
182
183impl Display for ImageModel {
184 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
185 write!(
186 f,
187 "{}",
188 match self {
189 Self::DallE2 => "dall-e-2",
190 Self::DallE3 => "dall-e-3",
191 Self::Other(other) => other,
192 }
193 )
194 }
195}
196
197impl Display for ImageResponseFormat {
198 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
199 write!(
200 f,
201 "{}",
202 match self {
203 Self::Url => "url",
204 Self::B64Json => "b64_json",
205 }
206 )
207 }
208}
209
210impl Display for AudioResponseFormat {
211 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
212 write!(
213 f,
214 "{}",
215 match self {
216 AudioResponseFormat::Json => "json",
217 AudioResponseFormat::Srt => "srt",
218 AudioResponseFormat::Text => "text",
219 AudioResponseFormat::VerboseJson => "verbose_json",
220 AudioResponseFormat::Vtt => "vtt",
221 }
222 )
223 }
224}
225
226impl Display for TimestampGranularity {
227 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
228 write!(
229 f,
230 "{}",
231 match self {
232 TimestampGranularity::Word => "word",
233 TimestampGranularity::Segment => "segment",
234 }
235 )
236 }
237}
238
239impl Display for Role {
240 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
241 write!(
242 f,
243 "{}",
244 match self {
245 Role::User => "user",
246 Role::System => "system",
247 Role::Assistant => "assistant",
248 Role::Function => "function",
249 Role::Tool => "tool",
250 }
251 )
252 }
253}
254
255impl Display for FilePurpose {
256 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
257 write!(
258 f,
259 "{}",
260 match self {
261 Self::Assistants => "assistants",
262 Self::Batch => "batch",
263 Self::FineTune => "fine-tune",
264 Self::Vision => "vision",
265 }
266 )
267 }
268}
269
270macro_rules! impl_from_for_integer_array {
271 ($from_typ:ty, $to_typ:ty) => {
272 impl<const N: usize> From<[$from_typ; N]> for $to_typ {
273 fn from(value: [$from_typ; N]) -> Self {
274 Self::IntegerArray(value.to_vec())
275 }
276 }
277
278 impl<const N: usize> From<&[$from_typ; N]> for $to_typ {
279 fn from(value: &[$from_typ; N]) -> Self {
280 Self::IntegerArray(value.to_vec())
281 }
282 }
283
284 impl From<Vec<$from_typ>> for $to_typ {
285 fn from(value: Vec<$from_typ>) -> Self {
286 Self::IntegerArray(value)
287 }
288 }
289
290 impl From<&Vec<$from_typ>> for $to_typ {
291 fn from(value: &Vec<$from_typ>) -> Self {
292 Self::IntegerArray(value.clone())
293 }
294 }
295 };
296}
297
298impl_from_for_integer_array!(u32, EmbeddingInput);
299impl_from_for_integer_array!(u16, Prompt);
300
301macro_rules! impl_from_for_array_of_integer_array {
302 ($from_typ:ty, $to_typ:ty) => {
303 impl From<Vec<Vec<$from_typ>>> for $to_typ {
304 fn from(value: Vec<Vec<$from_typ>>) -> Self {
305 Self::ArrayOfIntegerArray(value)
306 }
307 }
308
309 impl From<&Vec<Vec<$from_typ>>> for $to_typ {
310 fn from(value: &Vec<Vec<$from_typ>>) -> Self {
311 Self::ArrayOfIntegerArray(value.clone())
312 }
313 }
314
315 impl<const M: usize, const N: usize> From<[[$from_typ; N]; M]> for $to_typ {
316 fn from(value: [[$from_typ; N]; M]) -> Self {
317 Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
318 }
319 }
320
321 impl<const M: usize, const N: usize> From<[&[$from_typ; N]; M]> for $to_typ {
322 fn from(value: [&[$from_typ; N]; M]) -> Self {
323 Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
324 }
325 }
326
327 impl<const M: usize, const N: usize> From<&[[$from_typ; N]; M]> for $to_typ {
328 fn from(value: &[[$from_typ; N]; M]) -> Self {
329 Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
330 }
331 }
332
333 impl<const M: usize, const N: usize> From<&[&[$from_typ; N]; M]> for $to_typ {
334 fn from(value: &[&[$from_typ; N]; M]) -> Self {
335 Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
336 }
337 }
338
339 impl<const N: usize> From<[Vec<$from_typ>; N]> for $to_typ {
340 fn from(value: [Vec<$from_typ>; N]) -> Self {
341 Self::ArrayOfIntegerArray(value.to_vec())
342 }
343 }
344
345 impl<const N: usize> From<&[Vec<$from_typ>; N]> for $to_typ {
346 fn from(value: &[Vec<$from_typ>; N]) -> Self {
347 Self::ArrayOfIntegerArray(value.to_vec())
348 }
349 }
350
351 impl<const N: usize> From<[&Vec<$from_typ>; N]> for $to_typ {
352 fn from(value: [&Vec<$from_typ>; N]) -> Self {
353 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.clone()).collect())
354 }
355 }
356
357 impl<const N: usize> From<&[&Vec<$from_typ>; N]> for $to_typ {
358 fn from(value: &[&Vec<$from_typ>; N]) -> Self {
359 Self::ArrayOfIntegerArray(
360 value
361 .to_vec()
362 .into_iter()
363 .map(|inner| inner.clone())
364 .collect(),
365 )
366 }
367 }
368
369 impl<const N: usize> From<Vec<[$from_typ; N]>> for $to_typ {
370 fn from(value: Vec<[$from_typ; N]>) -> Self {
371 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
372 }
373 }
374
375 impl<const N: usize> From<&Vec<[$from_typ; N]>> for $to_typ {
376 fn from(value: &Vec<[$from_typ; N]>) -> Self {
377 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
378 }
379 }
380
381 impl<const N: usize> From<Vec<&[$from_typ; N]>> for $to_typ {
382 fn from(value: Vec<&[$from_typ; N]>) -> Self {
383 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
384 }
385 }
386
387 impl<const N: usize> From<&Vec<&[$from_typ; N]>> for $to_typ {
388 fn from(value: &Vec<&[$from_typ; N]>) -> Self {
389 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
390 }
391 }
392 };
393}
394
395impl_from_for_array_of_integer_array!(u32, EmbeddingInput);
396impl_from_for_array_of_integer_array!(u16, Prompt);
397
398impl From<&str> for ChatCompletionFunctionCall {
399 fn from(value: &str) -> Self {
400 match value {
401 "auto" => Self::Auto,
402 "none" => Self::None,
403 _ => Self::Function { name: value.into() },
404 }
405 }
406}
407
408impl From<&str> for FunctionName {
409 fn from(value: &str) -> Self {
410 Self { name: value.into() }
411 }
412}
413
414impl From<String> for FunctionName {
415 fn from(value: String) -> Self {
416 Self { name: value }
417 }
418}
419
420impl From<&str> for ChatCompletionNamedToolChoice {
421 fn from(value: &str) -> Self {
422 Self {
423 r#type: super::ChatCompletionToolType::Function,
424 function: value.into(),
425 }
426 }
427}
428
429impl From<String> for ChatCompletionNamedToolChoice {
430 fn from(value: String) -> Self {
431 Self {
432 r#type: super::ChatCompletionToolType::Function,
433 function: value.into(),
434 }
435 }
436}
437
438impl From<&str> for ChatCompletionToolChoiceOption {
439 fn from(value: &str) -> Self {
440 match value {
441 "auto" => Self::Auto,
442 "none" => Self::None,
443 _ => Self::Named(value.into()),
444 }
445 }
446}
447
448impl From<String> for ChatCompletionToolChoiceOption {
449 fn from(value: String) -> Self {
450 match value.as_str() {
451 "auto" => Self::Auto,
452 "none" => Self::None,
453 _ => Self::Named(value.into()),
454 }
455 }
456}
457
458impl From<ChatCompletionRequestUserMessage> for ChatCompletionRequestMessage {
461 fn from(value: ChatCompletionRequestUserMessage) -> Self {
462 Self::User(value)
463 }
464}
465
466impl From<ChatCompletionRequestSystemMessage> for ChatCompletionRequestMessage {
467 fn from(value: ChatCompletionRequestSystemMessage) -> Self {
468 Self::System(value)
469 }
470}
471
472impl From<ChatCompletionRequestAssistantMessage> for ChatCompletionRequestMessage {
473 fn from(value: ChatCompletionRequestAssistantMessage) -> Self {
474 Self::Assistant(value)
475 }
476}
477
478impl From<ChatCompletionRequestFunctionMessage> for ChatCompletionRequestMessage {
479 fn from(value: ChatCompletionRequestFunctionMessage) -> Self {
480 Self::Function(value)
481 }
482}
483
484impl From<ChatCompletionRequestToolMessage> for ChatCompletionRequestMessage {
485 fn from(value: ChatCompletionRequestToolMessage) -> Self {
486 Self::Tool(value)
487 }
488}
489
490impl From<ChatCompletionRequestUserMessageContent> for ChatCompletionRequestUserMessage {
491 fn from(value: ChatCompletionRequestUserMessageContent) -> Self {
492 Self {
493 content: value,
494 name: None,
495 }
496 }
497}
498
499impl From<ChatCompletionRequestSystemMessageContent> for ChatCompletionRequestSystemMessage {
500 fn from(value: ChatCompletionRequestSystemMessageContent) -> Self {
501 Self {
502 content: value,
503 name: None,
504 }
505 }
506}
507
508impl From<ChatCompletionRequestAssistantMessageContent> for ChatCompletionRequestAssistantMessage {
509 fn from(value: ChatCompletionRequestAssistantMessageContent) -> Self {
510 Self {
511 content: Some(value),
512 ..Default::default()
513 }
514 }
515}
516
517impl From<&str> for ChatCompletionRequestUserMessageContent {
518 fn from(value: &str) -> Self {
519 ChatCompletionRequestUserMessageContent::Text(value.into())
520 }
521}
522
523impl From<String> for ChatCompletionRequestUserMessageContent {
524 fn from(value: String) -> Self {
525 ChatCompletionRequestUserMessageContent::Text(value)
526 }
527}
528
529impl From<&str> for ChatCompletionRequestSystemMessageContent {
530 fn from(value: &str) -> Self {
531 ChatCompletionRequestSystemMessageContent::Text(value.into())
532 }
533}
534
535impl From<String> for ChatCompletionRequestSystemMessageContent {
536 fn from(value: String) -> Self {
537 ChatCompletionRequestSystemMessageContent::Text(value)
538 }
539}
540
541impl From<&str> for ChatCompletionRequestAssistantMessageContent {
542 fn from(value: &str) -> Self {
543 ChatCompletionRequestAssistantMessageContent::Text(value.into())
544 }
545}
546
547impl From<String> for ChatCompletionRequestAssistantMessageContent {
548 fn from(value: String) -> Self {
549 ChatCompletionRequestAssistantMessageContent::Text(value)
550 }
551}
552
553impl From<&str> for ChatCompletionRequestToolMessageContent {
554 fn from(value: &str) -> Self {
555 ChatCompletionRequestToolMessageContent::Text(value.into())
556 }
557}
558
559impl From<String> for ChatCompletionRequestToolMessageContent {
560 fn from(value: String) -> Self {
561 ChatCompletionRequestToolMessageContent::Text(value)
562 }
563}
564
565impl From<&str> for ChatCompletionRequestUserMessage {
566 fn from(value: &str) -> Self {
567 ChatCompletionRequestUserMessageContent::Text(value.into()).into()
568 }
569}
570
571impl From<String> for ChatCompletionRequestUserMessage {
572 fn from(value: String) -> Self {
573 value.as_str().into()
574 }
575}
576
577impl From<&str> for ChatCompletionRequestSystemMessage {
578 fn from(value: &str) -> Self {
579 ChatCompletionRequestSystemMessageContent::Text(value.into()).into()
580 }
581}
582
583impl From<String> for ChatCompletionRequestSystemMessage {
584 fn from(value: String) -> Self {
585 value.as_str().into()
586 }
587}
588
589impl From<&str> for ChatCompletionRequestAssistantMessage {
590 fn from(value: &str) -> Self {
591 ChatCompletionRequestAssistantMessageContent::Text(value.into()).into()
592 }
593}
594
595impl From<String> for ChatCompletionRequestAssistantMessage {
596 fn from(value: String) -> Self {
597 value.as_str().into()
598 }
599}
600
601impl From<Vec<ChatCompletionRequestUserMessageContentPart>>
602 for ChatCompletionRequestUserMessageContent
603{
604 fn from(value: Vec<ChatCompletionRequestUserMessageContentPart>) -> Self {
605 ChatCompletionRequestUserMessageContent::Array(value)
606 }
607}
608
609impl From<ChatCompletionRequestMessageContentPartText>
610 for ChatCompletionRequestUserMessageContentPart
611{
612 fn from(value: ChatCompletionRequestMessageContentPartText) -> Self {
613 ChatCompletionRequestUserMessageContentPart::Text(value)
614 }
615}
616
617impl From<ChatCompletionRequestMessageContentPartImage>
618 for ChatCompletionRequestUserMessageContentPart
619{
620 fn from(value: ChatCompletionRequestMessageContentPartImage) -> Self {
621 ChatCompletionRequestUserMessageContentPart::ImageUrl(value)
622 }
623}
624
625impl From<&str> for ChatCompletionRequestMessageContentPartText {
626 fn from(value: &str) -> Self {
627 ChatCompletionRequestMessageContentPartText { text: value.into() }
628 }
629}
630
631impl From<String> for ChatCompletionRequestMessageContentPartText {
632 fn from(value: String) -> Self {
633 ChatCompletionRequestMessageContentPartText { text: value }
634 }
635}
636
637impl From<&str> for ImageUrl {
638 fn from(value: &str) -> Self {
639 Self {
640 url: value.into(),
641 detail: Default::default(),
642 }
643 }
644}
645
646impl From<String> for ImageUrl {
647 fn from(value: String) -> Self {
648 Self {
649 url: value,
650 detail: Default::default(),
651 }
652 }
653}
654
655impl From<String> for CreateMessageRequestContent {
656 fn from(value: String) -> Self {
657 Self::Content(value)
658 }
659}
660
661impl From<&str> for CreateMessageRequestContent {
662 fn from(value: &str) -> Self {
663 Self::Content(value.to_string())
664 }
665}
666
667impl Default for ChatCompletionRequestUserMessageContent {
668 fn default() -> Self {
669 ChatCompletionRequestUserMessageContent::Text("".into())
670 }
671}
672
673impl Default for CreateMessageRequestContent {
674 fn default() -> Self {
675 Self::Content("".into())
676 }
677}
678
679impl Default for ChatCompletionRequestSystemMessageContent {
680 fn default() -> Self {
681 ChatCompletionRequestSystemMessageContent::Text("".into())
682 }
683}
684
685impl Default for ChatCompletionRequestToolMessageContent {
686 fn default() -> Self {
687 ChatCompletionRequestToolMessageContent::Text("".into())
688 }
689}