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