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}
61
62#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
63#[serde(rename_all = "lowercase")]
64pub enum ImageStyle {
65    #[default]
66    Vivid,
67    Natural,
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize, Default, Builder, PartialEq)]
71#[builder(name = "CreateImageRequestArgs")]
72#[builder(pattern = "mutable")]
73#[builder(setter(into, strip_option), default)]
74#[builder(derive(Debug))]
75#[builder(build_fn(error = "OpenAIError"))]
76pub struct CreateImageRequest {
77    /// A text description of the desired image(s). The maximum length is 1000 characters for `dall-e-2`
78    /// and 4000 characters for `dall-e-3`.
79    pub prompt: String,
80
81    /// The model to use for image generation.
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub model: Option<ImageModel>,
84
85    /// The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported.
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub n: Option<u8>, // min:1 max:10 default:1
88
89    /// The quality of the image that will be generated. `hd` creates images with finer details and greater
90    /// consistency across the image. This param is only supported for `dall-e-3`.
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub quality: Option<ImageQuality>,
93
94    /// 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.
95    #[serde(skip_serializing_if = "Option::is_none")]
96    pub response_format: Option<ImageResponseFormat>,
97
98    /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`.
99    /// Must be one of `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3` models.
100    #[serde(skip_serializing_if = "Option::is_none")]
101    pub size: Option<ImageSize>,
102
103    /// The style of the generated images. Must be one of `vivid` or `natural`.
104    /// Vivid causes the model to lean towards generating hyper-real and dramatic images.
105    /// Natural causes the model to produce more natural, less hyper-real looking images.
106    /// This param is only supported for `dall-e-3`.
107    #[serde(skip_serializing_if = "Option::is_none")]
108    pub style: Option<ImageStyle>,
109
110    /// 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).
111    #[serde(skip_serializing_if = "Option::is_none")]
112    pub user: Option<String>,
113}
114
115#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
116#[serde(untagged)]
117pub enum Image {
118    /// The URL of the generated image, if `response_format` is `url` (default).
119    Url {
120        url: String,
121        revised_prompt: Option<String>,
122    },
123    /// The base64-encoded JSON of the generated image, if `response_format` is `b64_json`.
124    B64Json {
125        b64_json: std::sync::Arc<String>,
126        revised_prompt: Option<String>,
127    },
128}
129
130#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
131pub struct ImagesResponse {
132    pub created: u32,
133    pub data: Vec<std::sync::Arc<Image>>,
134}
135
136#[derive(Debug, Default, Clone, PartialEq)]
137pub struct ImageInput {
138    pub source: InputSource,
139}
140
141#[derive(Debug, Clone, Default, Builder, PartialEq)]
142#[builder(name = "CreateImageEditRequestArgs")]
143#[builder(pattern = "mutable")]
144#[builder(setter(into, strip_option), default)]
145#[builder(derive(Debug))]
146#[builder(build_fn(error = "OpenAIError"))]
147pub struct CreateImageEditRequest {
148    /// 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.
149    pub image: ImageInput,
150
151    /// A text description of the desired image(s). The maximum length is 1000 characters.
152    pub prompt: String,
153
154    /// 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`.
155    pub mask: Option<ImageInput>,
156
157    /// The model to use for image generation. Only `dall-e-2` is supported at this time.
158    pub model: Option<ImageModel>,
159
160    /// The number of images to generate. Must be between 1 and 10.
161    pub n: Option<u8>, // min:1 max:10 default:1
162
163    /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`.
164    pub size: Option<DallE2ImageSize>,
165
166    /// The format in which the generated images are returned. Must be one of `url` or `b64_json`.
167    pub response_format: Option<ImageResponseFormat>,
168
169    /// 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).
170    pub user: Option<String>,
171}
172
173#[derive(Debug, Default, Clone, Builder, PartialEq)]
174#[builder(name = "CreateImageVariationRequestArgs")]
175#[builder(pattern = "mutable")]
176#[builder(setter(into, strip_option), default)]
177#[builder(derive(Debug))]
178#[builder(build_fn(error = "OpenAIError"))]
179pub struct CreateImageVariationRequest {
180    /// The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
181    pub image: ImageInput,
182
183    /// The model to use for image generation. Only `dall-e-2` is supported at this time.
184    pub model: Option<ImageModel>,
185
186    /// The number of images to generate. Must be between 1 and 10.
187    pub n: Option<u8>, // min:1 max:10 default:1
188
189    /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`.
190    pub size: Option<DallE2ImageSize>,
191
192    /// The format in which the generated images are returned. Must be one of `url` or `b64_json`.
193    pub response_format: Option<ImageResponseFormat>,
194
195    /// 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).
196    pub user: Option<String>,
197}