@@ -44,21 +44,32 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
4444 /// Optional custom view builder template
4545 let tpl : ImageProcess ?
4646
47+ /// Dall-e model type
48+ let model : DalleModel
49+
4750 // MARK: - Life cycle
4851
52+ /// Initializes a view model for generating images using the OpenAI API with customizable parameters.
4953 /// - Parameters:
50- /// - prompt: A text description of the desired image(s). The maximum length is 1000 characters
51- /// - size: The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024
52- /// - tpl: Custom view builder template
53- /// - loader: Custom loader conforming to `IOpenAILoader`
54+ /// - prompt: A `Binding` to a `String` that represents a text description of the desired image(s).
55+ /// The maximum length for the prompt is 1000 characters.
56+ /// - size: The size of the generated images, specified as an `OpenAIImageSize`.
57+ /// Defaults to `.dpi256`. Must be one of `.dpi256` (256x256), `.dpi512` (512x512), or `.dpi1024` (1024x1024).
58+ /// - model: The `DalleModel` specifying which model to use for generating the image(s).
59+ /// Defaults to `.dalle2`.
60+ /// - tpl: A custom SwiftUI `ViewBuilder` template for processing or rendering the generated image(s).
61+ /// - loader: A custom loader conforming to the `IOpenAILoader` protocol, responsible for handling
62+ /// the image generation process, such as communicating with the OpenAI API.
5463 public init (
55- prompt : Binding < String > ,
56- size : OpenAIImageSize = . dpi256,
57- @ViewBuilder tpl : @escaping ImageProcess ,
58- loader : T
59- ) {
64+ prompt: Binding < String > ,
65+ size: OpenAIImageSize = . dpi256,
66+ model: DalleModel = . dalle2,
67+ @ViewBuilder tpl: @escaping ImageProcess ,
68+ loader: T
69+ ) {
6070 self . _prompt = prompt
6171 self . size = size
72+ self . model = model
6273 self . tpl = tpl
6374 self . loader = loader
6475 }
@@ -97,32 +108,41 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
97108 return . loading
98109 }
99110
100- /// Load using the default loader
111+ /// Loads an image using the default loader.
101112 /// - Parameters:
102- /// - prompt: The text prompt for generating the image
103- /// - size: The desired size of the image
104- /// - Returns: OpenAI image
105- private func loadImageDefault( _ prompt : String , with size : ImageSize ) async throws -> Image {
106- try await defaultLoader. load ( prompt, with: size)
113+ /// - prompt: The text prompt describing the desired image content.
114+ /// - size: The dimensions of the generated image, specified as `ImageSize`.
115+ /// - model: The `DalleModel` specifying the AI model to use for image generation.
116+ /// - Returns: A generated `Image` object if successful.
117+ /// - Throws: An error if the image generation fails.
118+ private func loadImageDefault(
119+ _ prompt: String ,
120+ with size: ImageSize ,
121+ model: DalleModel
122+ ) async throws -> Image {
123+ try await defaultLoader. load ( prompt, with: size, model: model)
107124 }
108-
109- /// Load image using the provided or default loader
125+
126+ /// Loads an image using a provided loader, or falls back to the default loader if none is provided.
110127 /// - Parameters:
111- /// - prompt: The text prompt for generating the image
112- /// - size: The desired size of the image
113- /// - Returns: OpenAI image if successful, otherwise nil
114- private func loadImage( _ prompt : String , with size : ImageSize ) async -> Image ? {
115- do {
116- if let loader = loader{
117- return try await loader. load ( prompt, with: size)
128+ /// - prompt: The text prompt describing the desired image content.
129+ /// - size: The dimensions of the generated image, specified as `ImageSize`.
130+ /// - model: The `DalleModel` specifying the AI model to use for image generation.
131+ /// - Returns: An `Image` object if successful, or `nil` if the operation fails or is cancelled.
132+ private func loadImage(
133+ _ prompt: String ,
134+ with size: ImageSize ,
135+ model: DalleModel
136+ ) async -> Image ? {
137+ do {
138+ if let loader = loader {
139+ return try await loader. load ( prompt, with: size, model: model)
118140 }
119-
120- return try await loadImageDefault ( prompt, with: size)
121- } catch {
122- if !Task. isCancelled{
141+ return try await loadImageDefault ( prompt, with: size, model: model)
142+ } catch {
143+ if !Task. isCancelled {
123144 self . error = error
124145 }
125-
126146 return nil
127147 }
128148 }
@@ -151,7 +171,7 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
151171 /// - Returns: A task that fetches the OpenAI image
152172 private func getTask( ) -> Task < Void , Never > {
153173 Task {
154- if let image = await loadImage ( prompt, with: size) {
174+ if let image = await loadImage ( prompt, with: size, model : model ) {
155175 await setImage ( image)
156176 }
157177 }
@@ -162,35 +182,42 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
162182
163183public extension OpenAIAsyncImage where Content == EmptyView , T == OpenAIDefaultLoader {
164184
165- /// Convenience initializer for default loader without custom view template
185+ /// Convenience initializer for creating an instance with the default loader and no custom view template.
166186 /// - Parameters:
167- /// - prompt: The text prompt for generating the image
168- /// - size: The desired size of the image
187+ /// - prompt: A `Binding` to a `String` containing the text prompt that describes the desired image content.
188+ /// - size: The desired size of the generated image, specified as an `OpenAIImageSize`.
189+ /// Defaults to `.dpi256`.
190+ /// - model: The `DalleModel` specifying the AI model to use for image generation. Defaults to `.dalle2`.
169191 init (
170- prompt : Binding < String > ,
171- size : OpenAIImageSize = . dpi256
172- ) {
192+ prompt: Binding < String > ,
193+ size: OpenAIImageSize = . dpi256,
194+ model: DalleModel = . dalle2
195+ ) {
173196 self . _prompt = prompt
174197 self . size = size
198+ self . model = model
175199 self . tpl = nil
176200 self . loader = nil
177201 }
178202}
179203
180204public extension OpenAIAsyncImage where T == OpenAIDefaultLoader {
181205
182- /// Convenience initializer for default loader with custom view template
206+ /// Convenience initializer for creating an instance with the default loader and a custom view template.
183207 /// - Parameters:
184- /// - prompt: The text prompt for generating the image
185- /// - size: The desired size of the image
186- /// - tpl: Custom view template
208+ /// - prompt: A `Binding` to a `String` containing the text prompt that describes the desired image content.
209+ /// - size: The desired size of the generated image, specified as an `OpenAIImageSize`. Defaults to `.dpi256`.
210+ /// - model: The `DalleModel` specifying the AI model to use for image generation. Defaults to `.dalle2`.
211+ /// - tpl: A SwiftUI `@ViewBuilder` closure that provides a custom view template for processing or rendering the generated image.
187212 init (
188- prompt : Binding < String > ,
189- size : OpenAIImageSize = . dpi256,
190- @ViewBuilder tpl : @escaping ImageProcess
191- ) {
213+ prompt: Binding < String > ,
214+ size: OpenAIImageSize = . dpi256,
215+ model: DalleModel = . dalle2,
216+ @ViewBuilder tpl: @escaping ImageProcess
217+ ) {
192218 self . _prompt = prompt
193219 self . size = size
220+ self . model = model
194221 self . tpl = tpl
195222 self . loader = nil
196223 }
0 commit comments