Skip to main content

aiway_model_protocol/
image.rs

1use crate::shared::FileUpload;
2use derive_builder::Builder;
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::fmt::Display;
6
7#[derive(Serialize, Deserialize, Debug, Default, Builder, Clone, PartialEq)]
8#[builder(name = "CreateImageParametersBuilder")]
9#[builder(setter(into, strip_option), default)]
10pub struct CreateImageParameters {
11    /// 所需图像的文字描述。对于gpt-image-1最大长度为32000个字符,对于dall-e-2为1000个字符,对于dall-e-3为4000个字符。
12    pub prompt: String,
13    /// 允许为生成的图像设置背景透明度。此参数仅支持gpt-image-1。
14    /// 必须是transparent、opaque或auto(默认值)之一。当使用auto时,模型将自动确定图像的最佳背景。
15    /// 如果设置为transparent,输出格式需要支持透明度,因此应设置为png(默认值)或webp。
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub background: Option<BackgroundStyle>,
18    /// 用于图像生成的模型。
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub model: Option<String>,
21    /// 控制gpt-image-1生成图像的内容审核级别。必须是low(较少限制性过滤)或auto(默认值)。
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub moderation: Option<ModerationLevel>,
24    /// 要生成的图像数量。必须介于1到10之间。对于dall-e-3,仅支持n=1。
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub n: Option<u32>,
27    /// 生成图像的压缩级别(0-100%)。此参数仅支持gpt-image-1与webp或jpeg输出格式,并默认为100。
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub output_compression: Option<u32>,
30    /// 返回生成图像的格式。此参数仅支持gpt-image-1。必须是png、jpeg或webp之一。
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub output_format: Option<OutputFormat>,
33    /// 要生成的部分图像数量。此参数用于返回部分图像的流式响应。
34    /// 值必须在0到3之间。设置为0时,响应将是单个图像在一个流事件中发送。
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub partial_images: Option<u32>,
37    /// 将生成的图像质量。hd创建具有更精细细节和更大一致性的图像。
38    /// gpt-image-1支持high、medium和low。dall-e-3支持hd和standard。dall-e-2只支持standard选项。
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub quality: Option<ImageQuality>,
41    /// 返回生成图像的格式。必须是url或b64_json之一。
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub response_format: Option<ResponseFormat>,
44    /// 生成图像的尺寸。对于gpt-image-1必须是1024x1024、1536x1024(横向)、1024x1536(纵向)或auto(默认值),
45    /// 对于dall-e-2是256x256、512x512或1024x1024之一,对于dall-e-3是1024x1024、1792x1024或1024x1792之一。
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub size: Option<ImageSize>,
48    /// 在流模式下生成图像。
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub stream: Option<bool>,
51    /// 生成图像的风格。此参数仅支持dall-e-3。必须是vivid或natural之一。
52    /// Vivid使模型倾向于生成超现实和戏剧性图像。
53    /// Natural使模型产生更自然、不太超现实的图像。
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub style: Option<ImageStyle>,
56    /// 一个稳定标识符,用于帮助检测可能违反OpenAI使用政策的应用程序用户。
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub safety_identifier: Option<String>,
59    /// OpenAI用于缓存类似请求响应以优化缓存命中率。
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub prompt_cache_key: Option<String>,
62    /// 表示您的最终用户的唯一标识符,可以帮助OpenAI监控和检测滥用行为。
63    #[serde(skip_serializing_if = "Option::is_none")]
64    pub user: Option<String>,
65    /// 附加参数
66    pub ext: Option<HashMap<String, serde_json::Value>>,
67}
68
69#[derive(Serialize, Deserialize, Debug, Default, Builder, Clone, PartialEq)]
70#[builder(name = "EditImageParametersBuilder")]
71#[builder(setter(into, strip_option), default)]
72pub struct EditImageParameters {
73    /// 要编辑的图像。可以是base64编码的图像数据或者URL。
74    pub image: String,
75    /// 提示词。
76    pub prompt: Option<String>,
77    /// 允许为生成的图像设置背景透明度。此参数仅支持gpt-image-1。
78    /// 必须是transparent、opaque或auto(默认值)之一。当使用auto时,模型将自动确定图像的最佳背景。
79    /// 如果设置为transparent,输出格式需要支持透明度,因此应设置为png(默认值)或webp。
80    #[serde(skip_serializing_if = "Option::is_none")]
81    pub background: Option<BackgroundStyle>,
82    /// 控制模型在匹配输入图像的风格和特征(尤其是面部特征)上付出的努力程度。
83    /// 此参数仅支持gpt-image-1。不支持gpt-image-1-mini。支持high和low。默认为low。
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub input_fidelity: Option<InputFidelity>,
86    /// 另一个图像,其完全透明区域(例如alpha为零的地方)指示应该编辑图像的位置。
87    /// 如果提供了多个图像,则遮罩将应用于第一个图像。
88    /// 必须是有效的PNG文件,小于4MB,并且具有与图像相同的尺寸。
89    #[serde(skip_serializing_if = "Option::is_none")]
90    pub mask: Option<FileUpload>,
91    /// 图像的mime类型。如果未提供,mime类型将被设置为application/octet-stream。
92    /// gpt-image-1期望`image/png`、`image/jpeg`或`image/webp`。
93    #[serde(skip_serializing_if = "Option::is_none")]
94    pub mime_type: Option<MimeType>,
95    /// 用于图像生成的模型。
96    #[serde(skip_serializing_if = "Option::is_none")]
97    pub model: Option<String>,
98    /// 要生成的图像数量。必须介于1到10之间。
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub n: Option<u32>,
101    /// 生成图像的压缩级别(0-100%)。此参数仅支持gpt-image-1与webp或jpeg输出格式,并默认为100。
102    #[serde(skip_serializing_if = "Option::is_none")]
103    pub output_compression: Option<u32>,
104    /// 返回生成图像的格式。此参数仅支持gpt-image-1。必须是png、jpeg或webp之一。默认值是png。
105    #[serde(skip_serializing_if = "Option::is_none")]
106    pub output_format: Option<OutputFormat>,
107    /// 要生成的部分图像数量。此参数用于返回部分图像的流式响应。
108    /// 值必须在0到3之间。设置为0时,响应将是单个图像在一个流事件中发送。
109    /// 注意,如果完整图像生成得更快,则最终图像可能会在生成完全部分图像之前发送。
110    #[serde(skip_serializing_if = "Option::is_none")]
111    pub partial_images: Option<u32>,
112    /// 将生成的图像质量。hd创建具有更精细细节和更大一致性的图像。
113    /// gpt-image-1支持high、medium和low。dall-e-2仅支持standard质量。
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub quality: Option<ImageQuality>,
116    /// 返回生成图像的格式。必须是url或b64_json之一。URL在图像生成后仅在60分钟内有效。此参数仅支持dall-e-2,因为gpt-image-1将始终返回base64编码的图像。
117    #[serde(skip_serializing_if = "Option::is_none")]
118    pub response_format: Option<ResponseFormat>,
119    /// 生成图像的尺寸。对于gpt-image-1必须是1024x1024、1536x1024(横向)、1024x1536(纵向)或auto(默认值),
120    /// 对于dall-e-2是256x256、512x512或1024x1024之一,对于dall-e-3是1024x1024、1792x1024或1024x1792之一。
121    #[serde(skip_serializing_if = "Option::is_none")]
122    pub size: Option<ImageSize>,
123    /// 在流模式下编辑图像。默认为false。请参阅图像生成指南以获取更多信息。
124    #[serde(skip_serializing_if = "Option::is_none")]
125    pub stream: Option<bool>,
126    /// 表示您的最终用户的唯一标识符,可以帮助OpenAI监控和检测滥用行为。
127    #[serde(skip_serializing_if = "Option::is_none")]
128    pub user: Option<String>,
129}
130
131#[derive(Serialize, Deserialize, Debug, Default, Builder, Clone, PartialEq)]
132#[builder(name = "CreateImageVariationParametersBuilder")]
133#[builder(setter(into, strip_option), default)]
134pub struct CreateImageVariationParameters {
135    /// 用作变体基础的图像。必须是有效的PNG文件,小于4MB,且为正方形。
136    pub image: FileUpload,
137    /// 用于图像生成的模型。目前仅支持dall-e-2。
138    #[serde(skip_serializing_if = "Option::is_none")]
139    pub model: Option<String>,
140    /// 要生成的图像数量。必须介于1到10之间。对于dall-e-3,仅支持n=1。
141    #[serde(skip_serializing_if = "Option::is_none")]
142    pub n: Option<u32>,
143    /// 返回生成图像的格式。必须是url或b64_json之一。
144    #[serde(skip_serializing_if = "Option::is_none")]
145    pub response_format: Option<ResponseFormat>,
146    /// 生成图像的尺寸。对于gpt-image-1必须是1024x1024、1536x1024(横向)、1024x1536(纵向)或auto(默认值),
147    /// 对于dall-e-2是256x256、512x512或1024x1024之一,对于dall-e-3是1024x1024、1792x1024或1024x1792之一。
148    #[serde(skip_serializing_if = "Option::is_none")]
149    pub size: Option<ImageSize>,
150    /// 表示您的最终用户的唯一标识符,可以帮助OpenAI监控和检测滥用行为。
151    #[serde(skip_serializing_if = "Option::is_none")]
152    pub user: Option<String>,
153}
154
155#[derive(Default, Serialize, Deserialize, Debug, Clone, PartialEq)]
156pub struct ImageResponse {
157    /// 用于图像生成的背景参数。可以是transparent或opaque。
158    #[serde(skip_serializing_if = "Option::is_none")]
159    pub background: Option<BackgroundStyle>,
160    /// 图像创建时的Unix时间戳(以秒为单位)。
161    #[serde(skip_serializing_if = "Option::is_none")]
162    pub created: Option<u32>,
163    /// 生成图像的列表。
164    pub data: Vec<ImageData>,
165    /// 图像生成的输出格式。可以是png、webp或jpeg。
166    #[serde(skip_serializing_if = "Option::is_none")]
167    pub output_format: Option<OutputFormat>,
168    /// 生成图像的质量。可以是low、medium或high。
169    #[serde(skip_serializing_if = "Option::is_none")]
170    pub quality: Option<ImageQuality>,
171    /// 生成图像的尺寸。可以是1024x1024、1024x1536或1536x1024。
172    #[serde(skip_serializing_if = "Option::is_none")]
173    pub size: Option<ImageSizeResponse>,
174    /// 仅针对gpt-image-1,图像生成的令牌使用信息。
175    #[serde(skip_serializing_if = "Option::is_none")]
176    pub usage: Option<ImageUsage>,
177}
178
179#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
180pub enum ImageSize {
181    #[serde(rename = "256x256")]
182    Size256X256,
183    #[serde(rename = "512x512")]
184    Size512X512,
185    #[serde(rename = "1024x1024")]
186    Size1024X1024,
187    #[serde(rename = "1024x1536")]
188    Size1024X1536,
189    #[serde(rename = "1536x1024")]
190    Size1536X1024,
191    #[serde(rename = "1792x1024")]
192    Size1792X1024,
193    #[serde(rename = "1024x1792")]
194    Size1024X1792,
195    #[serde(rename = "auto")]
196    Auto,
197}
198
199#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
200pub enum ImageSizeResponse {
201    #[serde(rename = "256x256")]
202    Size256X256,
203    #[serde(rename = "512x512")]
204    Size512X512,
205    #[serde(rename = "1024x1024")]
206    Size1024X1024,
207    #[serde(rename = "1024x1536")]
208    Size1024X1536,
209    #[serde(rename = "1536x1024")]
210    Size1536X1024,
211    #[serde(rename = "1792x1024")]
212    Size1792X1024,
213    #[serde(rename = "1024x1792")]
214    Size1024X1792,
215}
216
217#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
218#[serde(rename_all = "snake_case")]
219pub enum BackgroundStyle {
220    Transparent,
221    Opaque,
222    Auto,
223}
224
225#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
226#[serde(rename_all = "snake_case")]
227pub enum ImageQuality {
228    Standard,
229    Hd,
230    High,
231    Medium,
232    Low,
233    Auto,
234}
235
236#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
237#[serde(rename_all = "snake_case")]
238pub enum MimeType {
239    #[serde(rename = "image/png")]
240    Png,
241    #[serde(rename = "image/jpeg")]
242    Jpeg,
243    #[serde(rename = "image/webp")]
244    Webp,
245    #[serde(rename = "application/octet-stream")]
246    OctetStream,
247}
248
249#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
250#[serde(rename_all = "snake_case")]
251pub enum ImageStyle {
252    Vivid,
253    Natural,
254}
255
256#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
257#[serde(rename_all = "snake_case")]
258pub enum ModerationLevel {
259    Auto,
260    Low,
261}
262
263#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
264#[serde(rename_all = "snake_case")]
265pub enum OutputFormat {
266    Png,
267    Jpeg,
268    Webp,
269}
270
271#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
272#[serde(rename_all = "snake_case")]
273pub enum InputFidelity {
274    High,
275    Low,
276}
277
278#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
279#[serde(rename_all = "snake_case")]
280pub enum ResponseFormat {
281    Url,
282    B64Json,
283}
284
285#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
286pub struct ImageUsage {
287    /// 输入提示中的令牌数(图像和文本)。
288    #[serde(default = "zero")]
289    pub input_tokens: u32,
290    /// 图像生成的输入令牌详细信息。
291    pub input_tokens_details: Option<InputTokensDetails>,
292    /// 模型生成的输出令牌数。
293    #[serde(default = "zero")]
294    pub output_tokens: u32,
295    /// 用于图像生成的总令牌数(图像和文本)。
296    #[serde(default = "zero")]
297    pub total_tokens: u32,
298}
299fn zero() -> u32 {
300    0
301}
302
303#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
304pub struct InputTokensDetails {
305    /// 输入中的图像令牌数。
306    pub image_tokens: u32,
307    /// 输入中的文本令牌数。
308    pub text_tokens: u32,
309}
310
311#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
312#[serde(untagged)]
313pub enum ImageData {
314    Url {
315        url: String,
316        #[serde(skip_serializing_if = "Option::is_none")]
317        revised_prompt: Option<String>,
318    },
319    B64Json {
320        b64_json: String,
321        #[serde(skip_serializing_if = "Option::is_none")]
322        revised_prompt: Option<String>,
323    },
324}
325
326impl Display for InputFidelity {
327    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
328        write!(
329            f,
330            "{}",
331            match self {
332                InputFidelity::High => "high",
333                InputFidelity::Low => "low",
334            }
335        )
336    }
337}
338
339impl Display for OutputFormat {
340    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
341        write!(
342            f,
343            "{}",
344            match self {
345                OutputFormat::Png => "png",
346                OutputFormat::Jpeg => "jpeg",
347                OutputFormat::Webp => "webp",
348            }
349        )
350    }
351}
352
353impl Display for BackgroundStyle {
354    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
355        write!(
356            f,
357            "{}",
358            match self {
359                BackgroundStyle::Transparent => "transparent",
360                BackgroundStyle::Opaque => "opaque",
361                BackgroundStyle::Auto => "auto",
362            }
363        )
364    }
365}
366
367impl Display for ImageQuality {
368    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
369        write!(
370            f,
371            "{}",
372            match self {
373                ImageQuality::Standard => "standard",
374                ImageQuality::Hd => "hd",
375                ImageQuality::High => "high",
376                ImageQuality::Medium => "medium",
377                ImageQuality::Low => "low",
378                ImageQuality::Auto => "auto",
379            }
380        )
381    }
382}
383
384impl Display for ImageSize {
385    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
386        write!(
387            f,
388            "{}",
389            match self {
390                ImageSize::Size256X256 => "256x256",
391                ImageSize::Size512X512 => "512x512",
392                ImageSize::Size1024X1024 => "1024x1024",
393                ImageSize::Size1536X1024 => "1536x1024",
394                ImageSize::Size1024X1536 => "1024x1536",
395                ImageSize::Size1792X1024 => "1792x1024",
396                ImageSize::Size1024X1792 => "1024x1792",
397                ImageSize::Auto => "auto",
398            }
399        )
400    }
401}
402
403impl Display for MimeType {
404    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
405        write!(
406            f,
407            "{}",
408            match self {
409                MimeType::Png => "image/png",
410                MimeType::Jpeg => "image/jpeg",
411                MimeType::Webp => "image/webp",
412                MimeType::OctetStream => "application/octet-stream",
413            }
414        )
415    }
416}
417
418impl Display for ResponseFormat {
419    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
420        write!(
421            f,
422            "{}",
423            match self {
424                ResponseFormat::Url => "url",
425                ResponseFormat::B64Json => "b64_json",
426            }
427        )
428    }
429}