Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Size enum for ImagesQuery #163

Merged
merged 10 commits into from
Feb 21, 2024
8 changes: 4 additions & 4 deletions Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,7 +37,7 @@ public struct ImageCreationView: View {
HStack {
Picker("Size", selection: $size) {
ForEach(sizes, id: \.self) {
Text($0)
Text($0.rawValue)
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions Sources/OpenAI/Public/Models/ImagesQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ 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.
public let style: String?
/// 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
Expand All @@ -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

}
}
6 changes: 3 additions & 3 deletions Sources/OpenAI/Public/Protocols/OpenAIProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/OpenAITests/OpenAITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
])
Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions Tests/OpenAITests/OpenAITestsDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class OpenAITestsDecoder: XCTestCase {
model: .dall_e_2,
responseFormat: .b64_json,
n: 1,
size: "10",
size: ._512,
style: "vivid",
user: "user"
)
Expand All @@ -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"
Expand Down
Loading