async_openai/
image.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::images::{
5        CreateImageEditRequest, CreateImageRequest, CreateImageVariationRequest, ImageEditStream,
6        ImageGenStream, ImagesResponse,
7    },
8    Client, RequestOptions,
9};
10
11/// Given a prompt and/or an input image, the model will generate a new image.
12///
13/// Related guide: [Image generation](https://platform.openai.com/docs/guides/images)
14pub struct Images<'c, C: Config> {
15    client: &'c Client<C>,
16    pub(crate) request_options: RequestOptions,
17}
18
19impl<'c, C: Config> Images<'c, C> {
20    pub fn new(client: &'c Client<C>) -> Self {
21        Self {
22            client,
23            request_options: RequestOptions::new(),
24        }
25    }
26
27    /// Creates an image given a prompt.
28    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
29    pub async fn generate(
30        &self,
31        request: CreateImageRequest,
32    ) -> Result<ImagesResponse, OpenAIError> {
33        self.client
34            .post("/images/generations", request, &self.request_options)
35            .await
36    }
37
38    /// Creates an image given a prompt.
39    #[crate::byot(
40        T0 = serde::Serialize,
41        R = serde::de::DeserializeOwned,
42        stream = "true",
43        where_clause = "R: std::marker::Send + 'static"
44    )]
45    #[allow(unused_mut)]
46    pub async fn generate_stream(
47        &self,
48        mut request: CreateImageRequest,
49    ) -> Result<ImageGenStream, OpenAIError> {
50        #[cfg(not(feature = "byot"))]
51        {
52            if request.stream.is_some() && !request.stream.unwrap() {
53                return Err(OpenAIError::InvalidArgument(
54                    "When stream is false, use Image::generate".into(),
55                ));
56            }
57
58            request.stream = Some(true);
59        }
60
61        Ok(self
62            .client
63            .post_stream("/images/generations", request, &self.request_options)
64            .await)
65    }
66
67    /// Creates an edited or extended image given one or more source images and a prompt.
68    /// This endpoint only supports gpt-image-1 and dall-e-2.
69    #[crate::byot(
70        T0 = Clone,
71        R = serde::de::DeserializeOwned,
72        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>",
73    )]
74    pub async fn edit(
75        &self,
76        request: CreateImageEditRequest,
77    ) -> Result<ImagesResponse, OpenAIError> {
78        self.client
79            .post_form("/images/edits", request, &self.request_options)
80            .await
81    }
82
83    /// Creates an edited or extended image given one or more source images and a prompt.
84    /// This endpoint only supports gpt-image-1 and dall-e-2.
85    #[crate::byot(
86        T0 = Clone,
87        R = serde::de::DeserializeOwned,
88        stream = "true",
89        where_clause = "R: std::marker::Send + 'static, reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>"
90    )]
91    #[allow(unused_mut)]
92    pub async fn edit_stream(
93        &self,
94        mut request: CreateImageEditRequest,
95    ) -> Result<ImageEditStream, OpenAIError> {
96        #[cfg(not(feature = "byot"))]
97        {
98            if let Some(stream) = request.stream {
99                if !stream {
100                    return Err(OpenAIError::InvalidArgument(
101                        "When stream is false, use Image::edit".into(),
102                    ));
103                }
104            }
105            request.stream = Some(true);
106        }
107        self.client
108            .post_form_stream("/images/edits", request, &self.request_options)
109            .await
110    }
111
112    /// Creates a variation of a given image. This endpoint only supports dall-e-2.
113    #[crate::byot(
114        T0 = Clone,
115        R = serde::de::DeserializeOwned,
116        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>",
117    )]
118    pub async fn create_variation(
119        &self,
120        request: CreateImageVariationRequest,
121    ) -> Result<ImagesResponse, OpenAIError> {
122        self.client
123            .post_form("/images/variations", request, &self.request_options)
124            .await
125    }
126}