From 3e50aaaa3a24fb8c379e434737f8053f0c2f8a50 Mon Sep 17 00:00:00 2001 From: James J Kalafus Date: Mon, 5 Feb 2024 14:08:08 -0500 Subject: [PATCH 1/5] make Demo buildable --- Demo/DemoChat/Sources/UI/DetailView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Demo/DemoChat/Sources/UI/DetailView.swift b/Demo/DemoChat/Sources/UI/DetailView.swift index 9e2a07e9..3cd119ea 100644 --- a/Demo/DemoChat/Sources/UI/DetailView.swift +++ b/Demo/DemoChat/Sources/UI/DetailView.swift @@ -19,7 +19,7 @@ struct DetailView: View { @State private var showsModelSelectionSheet = false @State private var selectedChatModel: Model = .gpt4_0613 - private let availableChatModels: [Model] = [.gpt3_5Turbo0613, .gpt4_0613] + private let availableChatModels: [Model] = [.gpt3_5Turbo, .gpt4_0613] let conversation: Conversation let error: Error? From bd2f3a5e4ccb80e48741dc6a9749b454b1c00ccd Mon Sep 17 00:00:00 2001 From: James J Kalafus Date: Mon, 5 Feb 2024 14:32:44 -0500 Subject: [PATCH 2/5] add CaseIterable enums for types in use as magic numbers (ImagesQuery.Size only) --- .../Sources/UI/Images/ImageCreationView.swift | 8 ++++---- Sources/OpenAI/Public/Models/ImagesQuery.swift | 13 +++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift b/Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift index 98ea5216..db471261 100644 --- a/Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift +++ b/Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift @@ -13,9 +13,9 @@ public struct ImageCreationView: View { @State private var prompt: String = "" @State private var n: Int = 1 - @State private var size: String - - private var sizes = ["256x256", "512x512", "1024x1024"] + @State private var size: ImagesQuery.Size + + private var sizes = ImagesQuery.Size.allCases public init(store: ImageStore) { self.store = store @@ -37,7 +37,7 @@ public struct ImageCreationView: View { HStack { Picker("Size", selection: $size) { ForEach(sizes, id: \.self) { - Text($0) + Text($0.rawValue) } } } diff --git a/Sources/OpenAI/Public/Models/ImagesQuery.swift b/Sources/OpenAI/Public/Models/ImagesQuery.swift index 6f9bd788..652e7f00 100644 --- a/Sources/OpenAI/Public/Models/ImagesQuery.swift +++ b/Sources/OpenAI/Public/Models/ImagesQuery.swift @@ -26,7 +26,7 @@ public struct ImagesQuery: Codable { /// The number of images to generate. Must be between 1 and 10. public let n: Int? /// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024. - public let size: String? + public let size: Self.Size? /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. public let user: String? /// The style of the generated images. Must be one of vivid or natural. Vivid causes the model to lean towards generating hyper-real and dramatic images. Natural causes the model to produce more natural, less hyper-real looking images. This param is only supported for dall-e-3. @@ -34,7 +34,7 @@ public struct ImagesQuery: Codable { /// The quality of the image that will be generated. hd creates images with finer details and greater consistency across the image. This param is only supported for dall-e-3. public let quality: String? - public init(prompt: String, model: Model?=nil, responseFormat: Self.ResponseFormat?=nil, n: Int?, size: String?, style: String?=nil, user:String?=nil, quality:String?=nil) { + public init(prompt: String, model: Model?=nil, responseFormat: Self.ResponseFormat?=nil, n: Int?, size: Self.Size?, style: String?=nil, user:String?=nil, quality:String?=nil) { self.style = style self.prompt = prompt self.n = n @@ -55,4 +55,13 @@ public struct ImagesQuery: Codable { case responseFormat = "response_format" case quality } + + public enum Size: String, Codable, CaseIterable { + case _256 = "256x256" + case _512 = "512x512" + case _1024 = "1024x1024" + //case _1792_1024 = "1792x1024" // for dall-e-3 models + //case _1024_1792 = "1024x1792" // for dall-e-3 models + + } } From a47051959a245e39565981304e75723e6849afc0 Mon Sep 17 00:00:00 2001 From: James J Kalafus Date: Mon, 5 Feb 2024 16:15:55 -0500 Subject: [PATCH 3/5] update references from "1024x1024" > ImagesQuery.Size._1024 in protocols examples --- Sources/OpenAI/Public/Protocols/OpenAIProtocol.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/OpenAI/Public/Protocols/OpenAIProtocol.swift b/Sources/OpenAI/Public/Protocols/OpenAIProtocol.swift index caf97090..1602a4de 100644 --- a/Sources/OpenAI/Public/Protocols/OpenAIProtocol.swift +++ b/Sources/OpenAI/Public/Protocols/OpenAIProtocol.swift @@ -49,7 +49,7 @@ public protocol OpenAIProtocol { Example: ``` - let query = ImagesQuery(prompt: "White cat with heterochromia sitting on the kitchen table", n: 1, size: "1024x1024") + let query = ImagesQuery(prompt: "White cat with heterochromia sitting on the kitchen table", n: 1, size: ImagesQuery.Size._1024) openAI.images(query: query) { result in //Handle result here } @@ -66,7 +66,7 @@ public protocol OpenAIProtocol { Example: ``` - let query = ImagesEditQuery(image: "@whitecat.png", prompt: "White cat with heterochromia sitting on the kitchen table with a bowl of food", n: 1, size: "1024x1024") + let query = ImagesEditQuery(image: "@whitecat.png", prompt: "White cat with heterochromia sitting on the kitchen table with a bowl of food", n: 1, size: ImagesQuery.Size._1024) openAI.imageEdits(query: query) { result in //Handle result here } @@ -83,7 +83,7 @@ public protocol OpenAIProtocol { Example: ``` - let query = ImagesVariationQuery(image: "@whitecat.png", n: 1, size: "1024x1024") + let query = ImagesVariationQuery(image: "@whitecat.png", n: 1, size: ImagesQuery.Size._1024) openAI.imageVariations(query: query) { result in //Handle result here } From bd6c420627903bf294361c9e3a1a2e95165bd6f8 Mon Sep 17 00:00:00 2001 From: James J Kalafus Date: Mon, 5 Feb 2024 22:01:33 -0500 Subject: [PATCH 4/5] ImageQuery.Size image test updates --- Tests/OpenAITests/OpenAITests.swift | 4 ++-- Tests/OpenAITests/OpenAITestsDecoder.swift | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/OpenAITests/OpenAITests.swift b/Tests/OpenAITests/OpenAITests.swift index 8a552b78..7c4c8ea1 100644 --- a/Tests/OpenAITests/OpenAITests.swift +++ b/Tests/OpenAITests/OpenAITests.swift @@ -44,7 +44,7 @@ class OpenAITests: XCTestCase { } func testImages() async throws { - let query = ImagesQuery(prompt: "White cat with heterochromia sitting on the kitchen table", model: .dall_e_2, n: 1, size: "1024x1024") + let query = ImagesQuery(prompt: "White cat with heterochromia sitting on the kitchen table", model: .dall_e_2, n: 1, size: ._1024) let imagesResult = ImagesResult(created: 100, data: [ .init(url: "http://foo.bar", b64_json: nil) ]) @@ -54,7 +54,7 @@ class OpenAITests: XCTestCase { } func testImagesError() async throws { - let query = ImagesQuery(prompt: "White cat with heterochromia sitting on the kitchen table", n: 1, size: "1024x1024") + let query = ImagesQuery(prompt: "White cat with heterochromia sitting on the kitchen table", n: 1, size: ._1024) let inError = APIError(message: "foo", type: "bar", param: "baz", code: "100") self.stub(error: inError) diff --git a/Tests/OpenAITests/OpenAITestsDecoder.swift b/Tests/OpenAITests/OpenAITestsDecoder.swift index 253a4486..c9d0aa5c 100644 --- a/Tests/OpenAITests/OpenAITestsDecoder.swift +++ b/Tests/OpenAITests/OpenAITestsDecoder.swift @@ -117,7 +117,7 @@ class OpenAITestsDecoder: XCTestCase { model: .dall_e_2, responseFormat: .b64_json, n: 1, - size: "10", + size: ._512, style: "vivid", user: "user" ) @@ -127,7 +127,7 @@ class OpenAITestsDecoder: XCTestCase { "model": "dall-e-2", "prompt": "test", "n": 1, - "size": "10", + "size": "512x512", "style": "vivid", "user": "user", "response_format": "b64_json" From ee0885f8c4f33c0f6feec066a6de2e4c8c945fcb Mon Sep 17 00:00:00 2001 From: James J Kalafus Date: Fri, 16 Feb 2024 19:09:58 -0500 Subject: [PATCH 5/5] include the dall-e-3 ImageQuery.Size cases --- Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift | 2 +- Sources/OpenAI/Public/Models/ImagesQuery.swift | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift b/Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift index e8d98ee3..06ca5416 100644 --- a/Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift +++ b/Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift @@ -15,7 +15,7 @@ public struct ImageCreationView: View { @State private var n: Int = 1 @State private var size = ImagesQuery.Size._1024 - private var sizes = ImagesQuery.Size.allCases + private var sizes = ImagesQuery.Size.allCases.dropLast(2) public init(store: ImageStore) { self.store = store diff --git a/Sources/OpenAI/Public/Models/ImagesQuery.swift b/Sources/OpenAI/Public/Models/ImagesQuery.swift index 2bf4093b..dae7cba2 100644 --- a/Sources/OpenAI/Public/Models/ImagesQuery.swift +++ b/Sources/OpenAI/Public/Models/ImagesQuery.swift @@ -85,7 +85,7 @@ public enum ResponseFormat: String, Codable, Equatable { case _256 = "256x256" case _512 = "512x512" case _1024 = "1024x1024" - //case _1792_1024 = "1792x1024" // for dall-e-3 models - //case _1024_1792 = "1024x1792" // for dall-e-3 models + case _1792_1024 = "1792x1024" // for dall-e-3 models + case _1024_1792 = "1024x1792" // for dall-e-3 models } }