async_openai/types/
image.rs

1use derive_builder::Builder;
2use serde::{Deserialize, Serialize};
3
4use crate::error::OpenAIError;
5
6use super::InputSource;
7
8#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
9pub enum ImageSize {
10    #[serde(rename = "256x256")]
11    S256x256,
12    #[serde(rename = "512x512")]
13    S512x512,
14    #[default]
15    #[serde(rename = "1024x1024")]
16    S1024x1024,
17    #[serde(rename = "1792x1024")]
18    S1792x1024,
19    #[serde(rename = "1024x1792")]
20    S1024x1792,
21}
22
23#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
24pub enum DallE2ImageSize {
25    #[serde(rename = "256x256")]
26    S256x256,
27    #[serde(rename = "512x512")]
28    S512x512,
29    #[default]
30    #[serde(rename = "1024x1024")]
31    S1024x1024,
32}
33
34#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)]
35#[serde(rename_all = "lowercase")]
36pub enum ImageResponseFormat {
37    #[default]
38    Url,
39    #[serde(rename = "b64_json")]
40    B64Json,
41}
42
43#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
44pub enum ImageModel {
45    #[default]
46    #[serde(rename = "dall-e-2")]
47    DallE2,
48    #[serde(rename = "dall-e-3")]
49    DallE3,
50    #[serde(untagged)]
51    Other(String),
52}
53
54#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
55#[serde(rename_all = "lowercase")]
56pub enum ImageQuality {
57    #[default]
58    Standard,
59    HD,
60    High,
61    Medium,
62    Low,
63    Auto,
64}
65
66#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
67#[serde(rename_all = "lowercase")]
68pub enum ImageStyle {
69    #[default]
70    Vivid,
71    Natural,
72}
73
74#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
75#[serde(rename_all = "lowercase")]
76pub enum ImageModeration {
77    #[default]
78    Auto,
79    Low,
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize, Default, Builder, PartialEq)]
83#[builder(name = "CreateImageRequestArgs")]
84#[builder(pattern = "mutable")]
85#[builder(setter(into, strip_option), default)]
86#[builder(derive(Debug))]
87#[builder(build_fn(error = "OpenAIError"))]
88pub struct CreateImageRequest {
89    /// A text description of the desired image(s). The maximum length is 1000 characters for `dall-e-2`
90    /// and 4000 characters for `dall-e-3`.
91    pub prompt: String,
92
93    /// The model to use for image generation.
94    #[serde(skip_serializing_if = "Option::is_none")]
95    pub model: Option<ImageModel>,
96
97    /// The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported.
98    #[serde(skip_serializing_if = "Option::is_none")]
99    pub n: Option<u8>, // min:1 max:10 default:1
100
101    /// The quality of the image that will be generated. `hd` creates images with finer details and greater
102    /// consistency across the image. This param is only supported for `dall-e-3`.
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub quality: Option<ImageQuality>,
105
106    /// The format in which the generated images are returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes after the image has been generated.
107    #[serde(skip_serializing_if = "Option::is_none")]
108    pub response_format: Option<ImageResponseFormat>,
109
110    /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`.
111    /// Must be one of `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3` models.
112    #[serde(skip_serializing_if = "Option::is_none")]
113    pub size: Option<ImageSize>,
114
115    /// The style of the generated images. Must be one of `vivid` or `natural`.
116    /// Vivid causes the model to lean towards generating hyper-real and dramatic images.
117    /// Natural causes the model to produce more natural, less hyper-real looking images.
118    /// This param is only supported for `dall-e-3`.
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub style: Option<ImageStyle>,
121
122    /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids).
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub user: Option<String>,
125
126    /// Control the content-moderation level for images generated by gpt-image-1.
127    /// Must be either `low` for less restrictive filtering or `auto` (default value).
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub moderation: Option<ImageModeration>,
130}
131
132#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
133#[serde(untagged)]
134pub enum Image {
135    /// The URL of the generated image, if `response_format` is `url` (default).
136    Url {
137        url: String,
138        revised_prompt: Option<String>,
139    },
140    /// The base64-encoded JSON of the generated image, if `response_format` is `b64_json`.
141    B64Json {
142        b64_json: std::sync::Arc<String>,
143        revised_prompt: Option<String>,
144    },
145}
146
147#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
148pub struct ImagesResponse {
149    pub created: u32,
150    pub data: Vec<std::sync::Arc<Image>>,
151}
152
153#[derive(Debug, Default, Clone, PartialEq)]
154pub struct ImageInput {
155    pub source: InputSource,
156}
157
158#[derive(Debug, Clone, Default, Builder, PartialEq)]
159#[builder(name = "CreateImageEditRequestArgs")]
160#[builder(pattern = "mutable")]
161#[builder(setter(into, strip_option), default)]
162#[builder(derive(Debug))]
163#[builder(build_fn(error = "OpenAIError"))]
164pub struct CreateImageEditRequest {
165    /// The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask.
166    pub image: ImageInput,
167
168    /// A text description of the desired image(s). The maximum length is 1000 characters.
169    pub prompt: String,
170
171    /// An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where `image` should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as `image`.
172    pub mask: Option<ImageInput>,
173
174    /// The model to use for image generation. Only `dall-e-2` is supported at this time.
175    pub model: Option<ImageModel>,
176
177    /// The number of images to generate. Must be between 1 and 10.
178    pub n: Option<u8>, // min:1 max:10 default:1
179
180    /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`.
181    pub size: Option<DallE2ImageSize>,
182
183    /// The format in which the generated images are returned. Must be one of `url` or `b64_json`.
184    pub response_format: Option<ImageResponseFormat>,
185
186    /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids).
187    pub user: Option<String>,
188}
189
190#[derive(Debug, Default, Clone, Builder, PartialEq)]
191#[builder(name = "CreateImageVariationRequestArgs")]
192#[builder(pattern = "mutable")]
193#[builder(setter(into, strip_option), default)]
194#[builder(derive(Debug))]
195#[builder(build_fn(error = "OpenAIError"))]
196pub struct CreateImageVariationRequest {
197    /// The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
198    pub image: ImageInput,
199
200    /// The model to use for image generation. Only `dall-e-2` is supported at this time.
201    pub model: Option<ImageModel>,
202
203    /// The number of images to generate. Must be between 1 and 10.
204    pub n: Option<u8>, // min:1 max:10 default:1
205
206    /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`.
207    pub size: Option<DallE2ImageSize>,
208
209    /// The format in which the generated images are returned. Must be one of `url` or `b64_json`.
210    pub response_format: Option<ImageResponseFormat>,
211
212    /// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/usage-policies/end-user-ids).
213    pub user: Option<String>,
214}