dynamo_async_openai/types/
image.rs

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