@@ -2012,6 +2012,200 @@ let fileContent = try await service.retrieveContentForFileWith(id: fileID)
20122012
20132013### Images
20142014
2015+ This library supports latest OpenAI Image generation
2016+
2017+ - Parameters Create
2018+
2019+ ```swift
2020+ /// 'Create Image':
2021+ /// https://platform.openai.com/docs/api-reference/images/create
2022+ public struct CreateImageParameters: Encodable {
2023+
2024+ /// A text description of the desired image(s).
2025+ /// The maximum length is 32000 characters for `gpt-image-1`, 1000 characters for `dall-e-2` and 4000 characters for `dall-e-3`.
2026+ public let prompt: String
2027+
2028+ // MARK: - Optional properties
2029+
2030+ /// Allows to set transparency for the background of the generated image(s).
2031+ /// This parameter is only supported for `gpt-image-1`.
2032+ /// Must be one of `transparent`, `opaque` or `auto` (default value).
2033+ /// When `auto` is used, the model will automatically determine the best background for the image.
2034+ /// If `transparent`, the output format needs to support transparency, so it should be set to either `png` (default value) or `webp`.
2035+ public let background: Background?
2036+
2037+ /// The model to use for image generation. One of `dall-e-2`, `dall-e-3`, or `gpt-image-1`.
2038+ /// Defaults to `dall-e-2` unless a parameter specific to `gpt-image-1` is used.
2039+ public let model: Model?
2040+
2041+ /// Control the content-moderation level for images generated by `gpt-image-1`.
2042+ /// Must be either low for less restrictive filtering or auto (default value).
2043+ public let moderation: Moderation?
2044+
2045+ /// The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported.
2046+ /// Defaults to `1`
2047+ public let n: Int ?
2048+
2049+ /// The compression level (0-100%) for the generated images.
2050+ /// This parameter is only supported for `gpt-image-1` with the `webp` or `jpeg` output formats, and defaults to 100.
2051+ public let outputCompression: Int ?
2052+
2053+ /// The format in which the generated images are returned.
2054+ /// This parameter is only supported for `gpt-image-1`.
2055+ /// Must be one of `png`, `jpeg`, or `webp`.
2056+ public let outputFormat: OutputFormat?
2057+
2058+ /// The quality of the image that will be generated.
2059+ /// - `auto` (default value) will automatically select the best quality for the given model.
2060+ /// - `high`, `medium` and `low` are supported for gpt-image-1.
2061+ /// - `hd` and `standard` are supported for dall-e-3.
2062+ /// - `standard` is the only option for dall-e-2.
2063+ public let quality: Quality?
2064+
2065+ /// The format in which generated images with dall-e-2 and dall-e-3 are returned.
2066+ /// Must be one of `url` or `b64_json`.
2067+ /// URLs are only valid for 60 minutes after the image has been generated.
2068+ /// This parameter isn't supported for `gpt-image-1` which will always return base64-encoded images.
2069+ public let responseFormat: ResponseFormat?
2070+
2071+ /// The size of the generated images.
2072+ /// - For gpt-image-1, one of `1024x1024`, `1536x1024` (landscape), `1024x1536` (portrait), or `auto` (default value)
2073+ /// - For dall-e-3, one of `1024x1024`, `1792x1024`, or `1024x1792`
2074+ /// - For dall-e-2, one of `256x256`, `512x512`, or `1024x1024`
2075+ public let size: String ?
2076+
2077+ /// The style of the generated images.
2078+ /// This parameter is only supported for `dall-e-3`.
2079+ /// Must be one of `vivid` or `natural`.
2080+ /// Vivid causes the model to lean towards generating hyper-real and dramatic images.
2081+ /// Natural causes the model to produce more natural, less hyper-real looking images.
2082+ public let style: Style?
2083+
2084+ /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
2085+ public let user: String ?
2086+ }
2087+ ```
2088+
2089+ - Parameters Edit
2090+
2091+ ```swift
2092+ /// Creates an edited or extended image given one or more source images and a prompt.
2093+ /// This endpoint only supports `gpt-image-1` and `dall-e-2`.
2094+ public struct CreateImageEditParameters: Encodable {
2095+
2096+ /// The image(s) to edit.
2097+ /// For `gpt-image-1`, each image should be a `png`, `webp`, or `jpg` file less than 25MB.
2098+ /// For `dall-e-2`, you can only provide one image, and it should be a square `png` file less than 4MB.
2099+ let image: [Data]
2100+
2101+ /// A text description of the desired image(s).
2102+ /// The maximum length is 1000 characters for `dall-e-2`, and 32000 characters for `gpt-image-1`.
2103+ let prompt: String
2104+
2105+ /// An additional image whose fully transparent areas indicate where `image` should be edited.
2106+ /// If there are multiple images provided, the mask will be applied on the first image.
2107+ /// Must be a valid PNG file, less than 4MB, and have the same dimensions as `image`.
2108+ let mask: Data?
2109+
2110+ /// The model to use for image generation. Only `dall-e-2` and `gpt-image-1` are supported.
2111+ /// Defaults to `dall-e-2` unless a parameter specific to `gpt-image-1` is used.
2112+ let model: String ?
2113+
2114+ /// The number of images to generate. Must be between 1 and 10.
2115+ /// Defaults to 1.
2116+ let n: Int ?
2117+
2118+ /// The quality of the image that will be generated.
2119+ /// `high`, `medium` and `low` are only supported for `gpt-image-1`.
2120+ /// `dall-e-2` only supports `standard` quality.
2121+ /// Defaults to `auto`.
2122+ let quality: String ?
2123+
2124+ /// The format in which the generated images are returned.
2125+ /// Must be one of `url` or `b64_json`.
2126+ /// URLs are only valid for 60 minutes after the image has been generated.
2127+ /// This parameter is only supported for `dall-e-2`, as `gpt-image-1` will always return base64-encoded images.
2128+ let responseFormat: String ?
2129+
2130+ /// The size of the generated images.
2131+ /// Must be one of `1024x1024`, `1536x1024` (landscape), `1024x1536` (portrait), or `auto` (default value) for `gpt-image-1`,
2132+ /// and one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`.
2133+ let size: String ?
2134+
2135+ /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
2136+ let user: String ?
2137+ }
2138+ ```
2139+
2140+ - Parameters Variations
2141+
2142+ ```swift
2143+ /// Creates a variation of a given image.
2144+ /// This endpoint only supports `dall-e-2`.
2145+ public struct CreateImageVariationParameters: Encodable {
2146+
2147+ /// The image to use as the basis for the variation(s).
2148+ /// Must be a valid PNG file, less than 4MB, and square.
2149+ let image: Data
2150+
2151+ /// The model to use for image generation. Only `dall-e-2` is supported at this time.
2152+ /// Defaults to `dall-e-2`.
2153+ let model: String ?
2154+
2155+ /// The number of images to generate. Must be between 1 and 10.
2156+ /// Defaults to 1.
2157+ let n: Int ?
2158+
2159+ /// The format in which the generated images are returned.
2160+ /// Must be one of `url` or `b64_json`.
2161+ /// URLs are only valid for 60 minutes after the image has been generated.
2162+ /// Defaults to `url`.
2163+ let responseFormat: String ?
2164+
2165+ /// The size of the generated images.
2166+ /// Must be one of `256x256`, `512x512`, or `1024x1024`.
2167+ /// Defaults to `1024x1024`.
2168+ let size: String ?
2169+
2170+ /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
2171+ let user: String ?
2172+ }
2173+ ```
2174+
2175+ - Request example
2176+
2177+ ```swift
2178+ import SwiftOpenAI
2179+
2180+ let service = OpenAIServiceFactory.service (apiKey : " <YOUR_KEY>" )
2181+
2182+ // ❶ Describe the image you want
2183+ let prompt = " A watercolor dragon-unicorn hybrid flying above snowy mountains"
2184+
2185+ // ❷ Build parameters with the brand-new types (commit 880a15c)
2186+ let params = CreateImageParameters (
2187+ prompt : prompt,
2188+ model : .gptImage1 , // .dallE3 / .dallE2 also valid
2189+ n : 1 , // 1-10 (only 1 for DALL-E 3)
2190+ quality : .high , // .hd / .standard for DALL-E 3
2191+ size : " 1024x1024" // use "1792x1024" or "1024x1792" for wide / tall
2192+ )
2193+
2194+ do {
2195+ // ❸ Fire the request – returns a `CreateImageResponse`
2196+ let result = try await service.createImages (parameters : params)
2197+ let url = result.data ? .first ? .url // or `b64Json` for base-64
2198+ print (" Image URL:" , url ?? " none" )
2199+ } catch {
2200+ print (" Generation failed:" , error)
2201+ }
2202+ ```
2203+
2204+ For a sample app example go to the `Examples/ SwiftOpenAIExample` project on this repo.
2205+
2206+ ⚠️ This library Also keeps compatinility with previous Image generation.
2207+
2208+
20152209For handling image sizes, we utilize the `Dalle` model. An enum with associated values has been defined to represent its size constraints accurately.
20162210
20172211 [DALL·E](https :// platform.openai.com/docs/models/dall-e)
@@ -2213,22 +2407,22 @@ Usage
22132407/// Create image
22142408let prompt = " A mix of a dragon and an unicorn"
22152409let createParameters = ImageCreateParameters (prompt : prompt, model : .dalle3 (.largeSquare ))
2216- let imageURLS = try await service.createImages (parameters : createParameters).data .map (\.url )
2410+ let imageURLS = try await service.legacyCreateImages (parameters : createParameters).data .map (\.url )
22172411```
22182412```swift
22192413/// Edit image
22202414let data = Data (contentsOfURL :_ ) // the data from an image.
22212415let image = UIImage (data : data)
22222416let prompt = " Add a background filled with pink balloons."
22232417let editParameters = ImageEditParameters (image : image, prompt : prompt, numberOfImages : 4 )
2224- let imageURLS = try await service.editImage (parameters : parameters).data .map (\.url )
2418+ let imageURLS = try await service.legacyEditImage (parameters : parameters).data .map (\.url )
22252419```
22262420```swift
22272421/// Image variations
22282422let data = Data (contentsOfURL :_ ) // the data from an image.
22292423let image = UIImage (data : data)
22302424let variationParameters = ImageVariationParameters (image : image, numberOfImages : 4 )
2231- let imageURLS = try await service.createImageVariations (parameters : parameters).data .map (\.url )
2425+ let imageURLS = try await service.legacyCreateImageVariations (parameters : parameters).data .map (\.url )
22322426```
22332427
22342428### Models
0 commit comments