outfox_openai/spec/images/image.rs
1use derive_builder::Builder;
2use serde::{Deserialize, Serialize};
3
4use crate::error::OpenAIError;
5use crate::spec::images::ImageInput;
6
7#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
8pub enum ImageSize {
9 #[default]
10 #[serde(rename = "auto")]
11 Auto,
12 #[serde(rename = "256x256")]
13 S256x256,
14 #[serde(rename = "512x512")]
15 S512x512,
16 #[serde(rename = "1024x1024")]
17 S1024x1024,
18 #[serde(rename = "1792x1024")]
19 S1792x1024,
20 #[serde(rename = "1024x1792")]
21 S1024x1792,
22 #[serde(rename = "1536x1024")]
23 S1536x1024,
24 #[serde(rename = "1024x1536")]
25 S1024x1536,
26}
27
28#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
29pub enum DallE2ImageSize {
30 #[serde(rename = "256x256")]
31 S256x256,
32 #[serde(rename = "512x512")]
33 S512x512,
34 #[default]
35 #[serde(rename = "1024x1024")]
36 S1024x1024,
37}
38
39#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
40pub enum DallE3ImageSize {
41 #[default]
42 #[serde(rename = "1024x1024")]
43 S1024x1024,
44 #[serde(rename = "1792x1024")]
45 S1792x1024,
46 #[serde(rename = "1024x1792")]
47 S1024x1792,
48}
49
50#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
51pub enum GptImage1ImageSize {
52 #[default]
53 #[serde(rename = "auto")]
54 Auto,
55 #[serde(rename = "1024x1024")]
56 S1024x1024,
57 #[serde(rename = "1536x1024")]
58 S1536x1024,
59 #[serde(rename = "1024x1536")]
60 S1024x1536,
61}
62
63#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)]
64#[serde(rename_all = "lowercase")]
65pub enum ImageResponseFormat {
66 #[default]
67 Url,
68 #[serde(rename = "b64_json")]
69 B64Json,
70}
71
72#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
73pub enum ImageModel {
74 #[serde(rename = "gpt-image-1")]
75 GptImage1,
76 #[serde(rename = "gpt-image-1.5")]
77 GptImage1dot5,
78 #[serde(rename = "gpt-image-1-mini")]
79 GptImage1Mini,
80 #[default]
81 #[serde(rename = "dall-e-2")]
82 DallE2,
83 #[serde(rename = "dall-e-3")]
84 DallE3,
85 #[serde(untagged)]
86 Other(String),
87}
88
89#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
90#[serde(rename_all = "lowercase")]
91pub enum ImageQuality {
92 Standard,
93 HD,
94 High,
95 Medium,
96 Low,
97 #[default]
98 Auto,
99}
100
101#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
102#[serde(rename_all = "lowercase")]
103pub enum ImageStyle {
104 #[default]
105 Vivid,
106 Natural,
107}
108
109#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
110#[serde(rename_all = "lowercase")]
111pub enum ImageModeration {
112 #[default]
113 Auto,
114 Low,
115}
116
117#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
118#[serde(rename_all = "lowercase")]
119pub enum ImageOutputFormat {
120 #[default]
121 Png,
122 Jpeg,
123 Webp,
124}
125
126#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
127#[serde(rename_all = "lowercase")]
128pub enum ImageBackground {
129 #[default]
130 Auto,
131 Transparent,
132 Opaque,
133}
134
135#[derive(Debug, Clone, Serialize, Deserialize, Default, Builder, PartialEq)]
136#[builder(name = "CreateImageRequestArgs")]
137#[builder(pattern = "mutable")]
138#[builder(setter(into, strip_option), default)]
139#[builder(derive(Debug))]
140#[builder(build_fn(error = "OpenAIError"))]
141pub struct CreateImageRequest {
142 /// A text description of the desired image(s). The maximum length is 32000 characters for
143 /// the GPT image models, 1000 characters for `dall-e-2` and 4000 characters for `dall-e-3`.
144 pub prompt: String,
145
146 /// The model to use for image generation. One of `dall-e-2`, `dall-e-3`, or the GPT image
147 /// model (`gpt-image-1`, `gpt-image-1-mini`, `gpt-image-1.5`). Defaults
148 /// to `dall-e-2` unless a parameter specific to the GPT image models is used.
149 #[serde(skip_serializing_if = "Option::is_none")]
150 pub model: Option<ImageModel>,
151
152 /// The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is
153 /// supported.
154 #[serde(skip_serializing_if = "Option::is_none")]
155 pub n: Option<u8>, // min:1 max:10 default:1
156
157 /// The quality of the image that will be generated.
158 ///
159 /// - `auto` (default value) will automatically select the best quality for the given model.
160 /// - `high`, `medium` and `low` are supported for the GPT image models.
161 /// - `hd` and `standard` are supported for `dall-e-3`.
162 /// - `standard` is the only option for `dall-e-2`.
163 #[serde(skip_serializing_if = "Option::is_none")]
164 pub quality: Option<ImageQuality>,
165
166 /// The format in which generated images with `dall-e-2` and `dall-e-3` are returned. Must be
167 /// one of `url` or `b64_json`. URLs are only valid for 60 minutes after the image has been
168 /// generated. This parameter isn't supported for the GPT image models which will always
169 /// return base64-encoded images.
170 #[serde(skip_serializing_if = "Option::is_none")]
171 pub response_format: Option<ImageResponseFormat>,
172
173 /// The format in which the generated images are returned. This parameter is only supported for
174 /// the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
175 #[serde(skip_serializing_if = "Option::is_none")]
176 pub output_format: Option<ImageOutputFormat>,
177
178 /// The compression level (0-100%) for the generated images. This parameter is only supported
179 /// for the GPT image models with the `webp` or `jpeg` output formats, and defaults to 100.
180 #[serde(skip_serializing_if = "Option::is_none")]
181 pub output_compression: Option<u8>,
182
183 /// Generate the image in streaming mode. Defaults to `false`. See the
184 /// [Image generation guide](https://platform.openai.com/docs/guides/image-generation) for more
185 /// information. This parameter is only supported for the GPT image models.
186 #[serde(skip_serializing_if = "Option::is_none")]
187 pub stream: Option<bool>,
188
189 /// The number of partial images to generate. This parameter is used for
190 /// streaming responses that return partial images. Value must be between 0 and 3.
191 /// When set to 0, the response will be a single image sent in one streaming event.
192 /// Note that the final image may be sent before the full number of partial images
193 /// are generated if the full image is generated more quickly.
194 #[serde(skip_serializing_if = "Option::is_none")]
195 pub partial_images: Option<u8>,
196
197 /// The size of the generated images. Must be one of `1024x1024`, `1536x1024` (landscape),
198 /// `1024x1536` (portrait), or `auto` (default value) for the GPT image models, one of
199 /// `256x256`, `512x512`, or `1024x1024` for `dall-e-2`, and one of `1024x1024`,
200 /// `1792x1024`, or `1024x1792` for `dall-e-3`.
201 #[serde(skip_serializing_if = "Option::is_none")]
202 pub size: Option<ImageSize>,
203
204 /// Control the content-moderation level for images generated by the GPT image models. Must be
205 /// either `low` for less restrictive filtering or `auto` (default value).
206 #[serde(skip_serializing_if = "Option::is_none")]
207 pub moderation: Option<ImageModeration>,
208
209 /// Allows to set transparency for the background of the generated image(s).
210 /// This parameter is only supported for the GPT image models. Must be one of
211 /// `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
212 /// model will automatically determine the best background for the image.
213 /// If `transparent`, the output format needs to support transparency, so it
214 /// should be set to either `png` (default value) or `webp`.
215 #[serde(skip_serializing_if = "Option::is_none")]
216 pub background: Option<ImageBackground>,
217
218 /// The style of the generated images. This parameter is only supported for `dall-e-3`. Must be
219 /// one of `vivid` or `natural`. Vivid causes the model to lean towards generating
220 /// hyper-real and dramatic images. Natural causes the model to produce more natural, less
221 /// hyper-real looking images.
222 #[serde(skip_serializing_if = "Option::is_none")]
223 pub style: Option<ImageStyle>,
224
225 /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect
226 /// abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
227 #[serde(skip_serializing_if = "Option::is_none")]
228 pub user: Option<String>,
229}
230
231#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
232#[serde(untagged)]
233pub enum Image {
234 /// The URL of the generated image, if `response_format` is `url` (default).
235 Url {
236 url: String,
237 revised_prompt: Option<String>,
238 },
239 /// The base64-encoded JSON of the generated image, if `response_format` is `b64_json`.
240 B64Json {
241 b64_json: std::sync::Arc<String>,
242 revised_prompt: Option<String>,
243 },
244}
245
246#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
247#[serde(rename_all = "lowercase")]
248pub enum ImageResponseBackground {
249 Transparent,
250 Opaque,
251}
252
253#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
254pub struct ImageGenInputUsageDetails {
255 /// The number of text tokens in the input prompt.
256 pub text_tokens: u32,
257 /// The number of image tokens in the input prompt.
258 pub image_tokens: u32,
259}
260
261#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
262pub struct ImageGenOutputTokensDetails {
263 /// The number of text output tokens generated by the model.
264 pub text_tokens: u32,
265 /// The number of image output tokens generated by the model.
266 pub image_tokens: u32,
267}
268
269#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
270pub struct ImageGenUsage {
271 /// The number of tokens (images and text) in the input prompt.
272 pub input_tokens: u32,
273 /// The total number of tokens (images and text) used for the image generation.
274 pub total_tokens: u32,
275 /// The number of output tokens generated by the model.
276 pub output_tokens: u32,
277 /// The output token details for the image generation.
278 pub output_token_details: Option<ImageGenOutputTokensDetails>,
279 /// The input tokens detailed information for the image generation.
280 pub input_tokens_details: ImageGenInputUsageDetails,
281}
282
283#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
284pub struct ImagesResponse {
285 /// The Unix timestamp (in seconds) of when the image was created.
286 pub created: u32,
287 /// The list of generated images.
288 pub data: Vec<std::sync::Arc<Image>>,
289 /// The background parameter used for the image generation. Either `transparent` or `opaque`.
290 pub background: Option<ImageResponseBackground>,
291 /// The output format of the image generation. Either `png`, `webp`, or `jpeg`.
292 pub output_format: Option<ImageOutputFormat>,
293 /// The size of the generated image. Either `1024x1024`, `1536x1024`, `1024x1536`.
294 pub size: Option<ImageSize>,
295 /// The quality of the image generated. Either `low`, `medium`, or `high`.
296 pub quality: Option<ImageQuality>,
297 /// For the GPT image models only, the token usage information for the image generation.
298 pub usage: Option<ImageGenUsage>,
299}
300
301#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
302#[serde(rename_all = "lowercase")]
303pub enum InputFidelity {
304 High,
305 #[default]
306 Low,
307}
308
309#[derive(Debug, Clone, PartialEq)]
310pub enum ImageEditInput {
311 Image(ImageInput),
312 Images(Vec<ImageInput>),
313}
314
315#[derive(Debug, Clone, Default, Builder, PartialEq)]
316#[builder(name = "CreateImageEditRequestArgs")]
317#[builder(pattern = "mutable")]
318#[builder(setter(into, strip_option), default)]
319#[builder(derive(Debug))]
320#[builder(build_fn(error = "OpenAIError"))]
321pub struct CreateImageEditRequest {
322 /// The image(s) to edit. Must be a supported image file or an array of images.
323 ///
324 /// For the GPT image models (the GPT image models, `gpt-image-1-mini`, and `gpt-image-1.5`),
325 /// each image should be a `png`, `webp`, or `jpg` file less
326 /// than 50MB. You can provide up to 16 images.
327 ///
328 /// For `dall-e-2`, you can only provide one image, and it should be a square
329 /// `png` file less than 4MB.
330 pub image: ImageEditInput,
331
332 /// A text description of the desired image(s). The maximum length is 1000 characters
333 /// for `dall-e-2`, and 32000 characters for the GPT image models.
334 pub prompt: String,
335
336 /// An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where
337 /// `image` should be edited. If there are multiple images provided, the mask will be applied
338 /// on the first image. Must be a valid PNG file, less than 4MB, and have the same
339 /// dimensions as `image`.
340 pub mask: Option<ImageInput>,
341
342 /// Allows to set transparency for the background of the generated image(s).
343 /// This parameter is only supported for the GPT image models. Must be one of
344 /// `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
345 /// model will automatically determine the best background for the image.
346 ///
347 /// If `transparent`, the output format needs to support transparency, so it
348 /// should be set to either `png` (default value) or `webp`.
349 pub background: Option<ImageBackground>,
350
351 /// The model to use for image generation. Only `dall-e-2` and the GPT image models are
352 /// supported. Defaults to `dall-e-2` unless a parameter specific to the GPT image models
353 /// is used.
354 pub model: Option<ImageModel>,
355
356 /// The number of images to generate. Must be between 1 and 10.
357 pub n: Option<u8>, // min:1 max:10 default:1
358
359 /// The size of the generated images. Must be one of `1024x1024`, `1536x1024` (landscape),
360 /// `1024x1536` (portrait), or `auto` (default value) for the GPT image models, and one of
361 /// `256x256`, `512x512`, or `1024x1024` for `dall-e-2`.
362 pub size: Option<ImageSize>,
363
364 /// The format in which the generated images are returned. Must be one of `url` or `b64_json`.
365 /// URLs are only valid for 60 minutes after the image has been generated. This parameter
366 /// is only supported for `dall-e-2`, as the GPT image models will always return
367 /// base64-encoded images.
368 pub response_format: Option<ImageResponseFormat>,
369
370 /// The format in which the generated images are returned. This parameter is
371 /// only supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
372 /// The default value is `png`.
373 pub output_format: Option<ImageOutputFormat>,
374
375 /// The compression level (0-100%) for the generated images. This parameter
376 /// is only supported for the GPT image models with the `webp` or `jpeg` output
377 /// formats, and defaults to 100.
378 pub output_compression: Option<u8>,
379
380 /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect
381 /// abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
382 pub user: Option<String>,
383
384 /// Control how much effort the model will exert to match the style and features, especially
385 /// facial features, of input images. This parameter is only supported for the GPT image
386 /// models. Unsupported for `gpt-image-1-mini`. Supports `high` and `low`. Defaults to
387 /// `low`.
388 pub input_fidelity: Option<InputFidelity>,
389
390 /// Edit the image in streaming mode. Defaults to `false`. See the
391 /// [Image generation guide](https://platform.openai.com/docs/guides/image-generation) for more
392 /// information.
393 pub stream: Option<bool>,
394
395 /// The number of partial images to generate. This parameter is used for
396 /// streaming responses that return partial images. Value must be between 0 and 3.
397 /// When set to 0, the response will be a single image sent in one streaming event.
398
399 /// Note that the final image may be sent before the full number of partial images
400 /// are generated if the full image is generated more quickly.
401 pub partial_images: Option<u8>,
402
403 /// The quality of the image that will be generated. `high`, `medium` and `low` are only
404 /// supported for the GPT image models. `dall-e-2` only supports `standard` quality.
405 /// Defaults to `auto`.
406 pub quality: Option<ImageQuality>,
407}
408
409#[derive(Debug, Default, Clone, Builder, PartialEq)]
410#[builder(name = "CreateImageVariationRequestArgs")]
411#[builder(pattern = "mutable")]
412#[builder(setter(into, strip_option), default)]
413#[builder(derive(Debug))]
414#[builder(build_fn(error = "OpenAIError"))]
415pub struct CreateImageVariationRequest {
416 /// The image to use as the basis for the variation(s). Must be a valid PNG file, less than
417 /// 4MB, and square.
418 pub image: ImageInput,
419
420 /// The model to use for image generation. Only `dall-e-2` is supported at this time.
421 pub model: Option<ImageModel>,
422
423 /// The number of images to generate. Must be between 1 and 10.
424 pub n: Option<u8>, // min:1 max:10 default:1
425
426 /// The format in which the generated images are returned. Must be one of `url` or `b64_json`.
427 /// URLs are only valid for 60 minutes after the image has been generated.
428 pub response_format: Option<ImageResponseFormat>,
429
430 /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`.
431 pub size: Option<DallE2ImageSize>,
432
433 /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect
434 /// abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
435 pub user: Option<String>,
436}