openai_rust/
images.rs

1//! See <https://platform.openai.com/docs/api-reference/images>.
2//! Use with [Client::create_image](crate::Client::create_image).
3
4use serde::{Deserialize, Serialize};
5
6/// The format in which the generated images are returned.
7#[derive(Serialize, Debug, Clone)]
8pub enum ResponseFormat {
9    Url,
10    Base64JSON,
11}
12
13#[derive(Serialize, Debug, Clone)]
14pub struct ImageArguments {
15    /// A text description of the desired image(s). The maximum length is 1000 characters.
16    pub prompt: String,
17    /// The number of images to generate. Must be between 1 and 10. Defaults to 1.
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub n: Option<u32>,
20    /// The format in which the generated images are returned Defaults to `url`.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub response_format: Option<ResponseFormat>,
23    /// The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024`. Defaults to `1024x1024`.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub size: Option<u32>,
26    /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub user: Option<String>,
29}
30
31impl ImageArguments {
32    pub fn new(prompt: impl AsRef<str>) -> Self {
33        Self {
34            prompt: prompt.as_ref().to_owned(),
35            n: None,
36            response_format: None,
37            size: None,
38            user: None,
39        }
40    }
41}
42
43#[derive(Deserialize, Debug)]
44pub(crate) enum ImageObject {
45    #[serde(alias = "url")]
46    Url(String),
47    #[serde(alias = "b64_json")]
48    Base64JSON(String), 
49}
50
51#[derive(Deserialize, Debug)]
52pub(crate) struct ImageResponse {
53    #[allow(dead_code)]
54    created: u32,
55    pub data: Vec<ImageObject>,
56}