rig/client/
image_generation.rs

1#[cfg(feature = "image")]
2mod image {
3    use crate::client::Nothing;
4    #[allow(deprecated)]
5    use crate::image_generation::ImageGenerationModelDyn;
6    use crate::image_generation::{
7        ImageGenerationError, ImageGenerationModel, ImageGenerationRequest, ImageGenerationResponse,
8    };
9    use std::future::Future;
10    use std::sync::Arc;
11
12    /// A provider client with image generation capabilities.
13    /// Clone is required for conversions between client types.
14    pub trait ImageGenerationClient {
15        /// The ImageGenerationModel used by the Client
16        type ImageGenerationModel: ImageGenerationModel<Client = Self>;
17
18        /// Create an image generation model with the given name.
19        ///
20        /// # Example with OpenAI
21        /// ```
22        /// use rig::prelude::*;
23        /// use rig::providers::openai::{Client, self};
24        ///
25        /// // Initialize the OpenAI client
26        /// let openai = Client::new("your-open-ai-api-key");
27        ///
28        /// let gpt4 = openai.image_generation_model(openai::DALL_E_3);
29        /// ```
30        fn image_generation_model(&self, model: impl Into<String>) -> Self::ImageGenerationModel;
31
32        /// Create an image generation model with the given name.
33        ///
34        /// # Example with OpenAI
35        /// ```
36        /// use rig::prelude::*;
37        /// use rig::providers::openai::{Client, self};
38        ///
39        /// // Initialize the OpenAI client
40        /// let openai = Client::new("your-open-ai-api-key");
41        ///
42        /// let gpt4 = openai.image_generation_model(openai::DALL_E_3);
43        /// ```
44        fn custom_image_generation_model(
45            &self,
46            model: impl Into<String>,
47        ) -> Self::ImageGenerationModel {
48            Self::ImageGenerationModel::make(self, model)
49        }
50    }
51
52    #[allow(deprecated)]
53    #[deprecated(
54        since = "0.25.0",
55        note = "`DynClientBuilder` and related features have been deprecated and will be removed in a future release. In this case, use `ImageGenerationClient` instead."
56    )]
57    pub trait ImageGenerationClientDyn {
58        /// Create an image generation model with the given name.
59        fn image_generation_model<'a>(&self, model: &str) -> Box<dyn ImageGenerationModelDyn + 'a>;
60    }
61
62    #[allow(deprecated)]
63    impl<T: ImageGenerationClient<ImageGenerationModel = M>, M: ImageGenerationModel + 'static>
64        ImageGenerationClientDyn for T
65    {
66        fn image_generation_model<'a>(&self, model: &str) -> Box<dyn ImageGenerationModelDyn + 'a> {
67            Box::new(self.image_generation_model(model))
68        }
69    }
70
71    #[deprecated(
72        since = "0.25.0",
73        note = "`DynClientBuilder` and related features have been deprecated and will be removed in a future release."
74    )]
75    /// Wraps a ImageGenerationModel in a dyn-compatible way for ImageGenerationRequestBuilder.
76    #[derive(Clone)]
77    pub struct ImageGenerationModelHandle<'a> {
78        #[allow(deprecated)]
79        pub(crate) inner: Arc<dyn ImageGenerationModelDyn + 'a>,
80    }
81
82    #[allow(deprecated)]
83    impl ImageGenerationModel for ImageGenerationModelHandle<'_> {
84        type Response = ();
85        type Client = Nothing;
86
87        /// **PANICS** if called
88        fn make(_client: &Self::Client, _model: impl Into<String>) -> Self {
89            panic!(
90                "'ImageGenerationModel::make' should not be called on 'ImageGenerationModelHandle'"
91            )
92        }
93
94        fn image_generation(
95            &self,
96            request: ImageGenerationRequest,
97        ) -> impl Future<
98            Output = Result<ImageGenerationResponse<Self::Response>, ImageGenerationError>,
99        > + Send {
100            self.inner.image_generation(request)
101        }
102    }
103}
104
105#[cfg(feature = "image")]
106pub use image::*;