async_openai/types/
impls.rs1#[cfg(feature = "audio-types")]
2use crate::types::audio::AudioInput;
3#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
4use crate::types::chat::{Prompt, StopConfiguration};
5#[cfg(feature = "embedding-types")]
6use crate::types::embeddings::EmbeddingInput;
7#[cfg(feature = "file-types")]
8use crate::types::files::FileInput;
9#[cfg(feature = "moderation-types")]
10use crate::types::moderations::ModerationInput;
11#[cfg(any(feature = "image-types", feature = "video-types"))]
12use crate::types::shared::ImageInput;
13#[cfg(any(
14 feature = "audio-types",
15 feature = "file-types",
16 feature = "image-types",
17 feature = "video-types"
18))]
19use crate::types::InputSource;
20
21#[cfg(any(
30 feature = "chat-completion-types",
31 feature = "completion-types",
32 feature = "embedding-types",
33 feature = "moderation-types"
34))]
35macro_rules! impl_from {
36 ($from_typ:ty, $to_typ:ty) => {
37 impl From<$from_typ> for $to_typ {
39 fn from(value: $from_typ) -> Self {
40 <$to_typ>::String(value.into())
41 }
42 }
43
44 impl From<Vec<$from_typ>> for $to_typ {
46 fn from(value: Vec<$from_typ>) -> Self {
47 <$to_typ>::StringArray(value.iter().map(|v| v.to_string()).collect())
48 }
49 }
50
51 impl From<&Vec<$from_typ>> for $to_typ {
53 fn from(value: &Vec<$from_typ>) -> Self {
54 <$to_typ>::StringArray(value.iter().map(|v| v.to_string()).collect())
55 }
56 }
57
58 impl<const N: usize> From<[$from_typ; N]> for $to_typ {
60 fn from(value: [$from_typ; N]) -> Self {
61 <$to_typ>::StringArray(value.into_iter().map(|v| v.to_string()).collect())
62 }
63 }
64
65 impl<const N: usize> From<&[$from_typ; N]> for $to_typ {
67 fn from(value: &[$from_typ; N]) -> Self {
68 <$to_typ>::StringArray(value.into_iter().map(|v| v.to_string()).collect())
69 }
70 }
71 };
72}
73
74#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
76impl_from!(&str, Prompt);
77#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
78impl_from!(String, Prompt);
79#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
80impl_from!(&String, Prompt);
81
82#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
84impl_from!(&str, StopConfiguration);
85#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
86impl_from!(String, StopConfiguration);
87#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
88impl_from!(&String, StopConfiguration);
89
90#[cfg(feature = "moderation-types")]
92impl_from!(&str, ModerationInput);
93#[cfg(feature = "moderation-types")]
94impl_from!(String, ModerationInput);
95#[cfg(feature = "moderation-types")]
96impl_from!(&String, ModerationInput);
97
98#[cfg(feature = "embedding-types")]
100impl_from!(&str, EmbeddingInput);
101#[cfg(feature = "embedding-types")]
102impl_from!(String, EmbeddingInput);
103#[cfg(feature = "embedding-types")]
104impl_from!(&String, EmbeddingInput);
105
106#[cfg(any(
108 feature = "chat-completion-types",
109 feature = "completion-types",
110 feature = "embedding-types",
111 feature = "moderation-types"
112))]
113macro_rules! impl_default {
114 ($for_typ:ty) => {
115 impl Default for $for_typ {
116 fn default() -> Self {
117 Self::String("".into())
118 }
119 }
120 };
121}
122
123#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
124impl_default!(Prompt);
125#[cfg(feature = "moderation-types")]
126impl_default!(ModerationInput);
127#[cfg(feature = "embedding-types")]
128impl_default!(EmbeddingInput);
129
130#[cfg(any(
139 feature = "audio-types",
140 feature = "file-types",
141 feature = "image-types",
142 feature = "video-types"
143))]
144macro_rules! impl_input {
145 ($for_typ:ty) => {
146 impl $for_typ {
147 pub fn from_bytes(filename: String, bytes: bytes::Bytes) -> Self {
148 Self {
149 source: InputSource::Bytes { filename, bytes },
150 }
151 }
152
153 pub fn from_vec_u8(filename: String, vec: Vec<u8>) -> Self {
154 Self {
155 source: InputSource::VecU8 { filename, vec },
156 }
157 }
158 }
159
160 #[cfg(not(target_family = "wasm"))]
161 impl<P: AsRef<std::path::Path>> From<P> for $for_typ {
162 fn from(path: P) -> Self {
163 let path_buf = path.as_ref().to_path_buf();
164 Self {
165 source: InputSource::Path { path: path_buf },
166 }
167 }
168 }
169 };
170}
171
172#[cfg(feature = "audio-types")]
173impl_input!(AudioInput);
174#[cfg(feature = "file-types")]
175impl_input!(FileInput);
176#[cfg(any(feature = "image-types", feature = "video-types"))]
177impl_input!(ImageInput);
178
179#[cfg(any(
180 feature = "chat-completion-types",
181 feature = "completion-types",
182 feature = "embedding-types"
183))]
184macro_rules! impl_from_for_integer_array {
185 ($from_typ:ty, $to_typ:ty) => {
186 impl<const N: usize> From<[$from_typ; N]> for $to_typ {
187 fn from(value: [$from_typ; N]) -> Self {
188 Self::IntegerArray(value.to_vec())
189 }
190 }
191
192 impl<const N: usize> From<&[$from_typ; N]> for $to_typ {
193 fn from(value: &[$from_typ; N]) -> Self {
194 Self::IntegerArray(value.to_vec())
195 }
196 }
197
198 impl From<Vec<$from_typ>> for $to_typ {
199 fn from(value: Vec<$from_typ>) -> Self {
200 Self::IntegerArray(value)
201 }
202 }
203
204 impl From<&Vec<$from_typ>> for $to_typ {
205 fn from(value: &Vec<$from_typ>) -> Self {
206 Self::IntegerArray(value.clone())
207 }
208 }
209 };
210}
211
212#[cfg(feature = "embedding-types")]
213impl_from_for_integer_array!(u32, EmbeddingInput);
214#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
215impl_from_for_integer_array!(u32, Prompt);
216
217#[cfg(any(
218 feature = "chat-completion-types",
219 feature = "completion-types",
220 feature = "embedding-types"
221))]
222macro_rules! impl_from_for_array_of_integer_array {
223 ($from_typ:ty, $to_typ:ty) => {
224 impl From<Vec<Vec<$from_typ>>> for $to_typ {
225 fn from(value: Vec<Vec<$from_typ>>) -> Self {
226 Self::ArrayOfIntegerArray(value)
227 }
228 }
229
230 impl From<&Vec<Vec<$from_typ>>> for $to_typ {
231 fn from(value: &Vec<Vec<$from_typ>>) -> Self {
232 Self::ArrayOfIntegerArray(value.clone())
233 }
234 }
235
236 impl<const M: usize, const N: usize> From<[[$from_typ; N]; M]> for $to_typ {
237 fn from(value: [[$from_typ; N]; M]) -> Self {
238 Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
239 }
240 }
241
242 impl<const M: usize, const N: usize> From<[&[$from_typ; N]; M]> for $to_typ {
243 fn from(value: [&[$from_typ; N]; M]) -> Self {
244 Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
245 }
246 }
247
248 impl<const M: usize, const N: usize> From<&[[$from_typ; N]; M]> for $to_typ {
249 fn from(value: &[[$from_typ; N]; M]) -> Self {
250 Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
251 }
252 }
253
254 impl<const M: usize, const N: usize> From<&[&[$from_typ; N]; M]> for $to_typ {
255 fn from(value: &[&[$from_typ; N]; M]) -> Self {
256 Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
257 }
258 }
259
260 impl<const N: usize> From<[Vec<$from_typ>; N]> for $to_typ {
261 fn from(value: [Vec<$from_typ>; N]) -> Self {
262 Self::ArrayOfIntegerArray(value.to_vec())
263 }
264 }
265
266 impl<const N: usize> From<&[Vec<$from_typ>; N]> for $to_typ {
267 fn from(value: &[Vec<$from_typ>; N]) -> Self {
268 Self::ArrayOfIntegerArray(value.to_vec())
269 }
270 }
271
272 impl<const N: usize> From<[&Vec<$from_typ>; N]> for $to_typ {
273 fn from(value: [&Vec<$from_typ>; N]) -> Self {
274 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.clone()).collect())
275 }
276 }
277
278 impl<const N: usize> From<&[&Vec<$from_typ>; N]> for $to_typ {
279 fn from(value: &[&Vec<$from_typ>; N]) -> Self {
280 Self::ArrayOfIntegerArray(
281 value
282 .to_vec()
283 .into_iter()
284 .map(|inner| inner.clone())
285 .collect(),
286 )
287 }
288 }
289
290 impl<const N: usize> From<Vec<[$from_typ; N]>> for $to_typ {
291 fn from(value: Vec<[$from_typ; N]>) -> Self {
292 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
293 }
294 }
295
296 impl<const N: usize> From<&Vec<[$from_typ; N]>> for $to_typ {
297 fn from(value: &Vec<[$from_typ; N]>) -> Self {
298 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
299 }
300 }
301
302 impl<const N: usize> From<Vec<&[$from_typ; N]>> for $to_typ {
303 fn from(value: Vec<&[$from_typ; N]>) -> Self {
304 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
305 }
306 }
307
308 impl<const N: usize> From<&Vec<&[$from_typ; N]>> for $to_typ {
309 fn from(value: &Vec<&[$from_typ; N]>) -> Self {
310 Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
311 }
312 }
313 };
314}
315
316#[cfg(feature = "embedding-types")]
317impl_from_for_array_of_integer_array!(u32, EmbeddingInput);
318#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
319impl_from_for_array_of_integer_array!(u32, Prompt);