Skip to content

Commit 971c337

Browse files
authored
Fix serialization for DALL-E edits (#235)
1 parent 001cb04 commit 971c337

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

Sources/AIProxy/AIProxy.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import UIKit
88
public enum AIProxy {
99

1010
/// The current sdk version
11-
nonisolated public static let sdkVersion = "0.131.0"
11+
nonisolated public static let sdkVersion = "0.131.1"
1212

1313
/// Configures the AIProxy SDK. Call this during app launch by adding an `init` to your SwiftUI MyApp.swift file, e.g.
1414
///

Sources/AIProxy/OpenAI/OpenAICreateImageEditRequestBody.swift

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,7 @@ nonisolated public struct OpenAICreateImageEditRequestBody: MultipartFormEncodab
7979

8080
public var formFields: [FormField] {
8181
var builder: [FormField] = []
82-
for i in 0..<self.images.count {
83-
let img = self.images[i]
84-
builder.append(
85-
.fileField(
86-
name: "image[]",
87-
content: img.content,
88-
contentType: img.contentType,
89-
filename: "tmpfile\(i)"
90-
)
91-
)
92-
}
93-
82+
self.setImageFormField(builder: &builder)
9483
return builder + [
9584
.textField(name: "prompt", content: self.prompt),
9685
self.background.flatMap { .textField(name: "background", content: $0.rawValue) },
@@ -136,6 +125,33 @@ nonisolated public struct OpenAICreateImageEditRequestBody: MultipartFormEncodab
136125
self.size = size
137126
self.user = user
138127
}
128+
129+
private func setImageFormField(builder: inout [FormField]) {
130+
// If the user wants a dalle-2 edit, we can only allow a single image.
131+
// Also, OpenAI expects that dalle-2 sends an `image` not `image[]` part:
132+
if self.model == .dallE2, let img = self.images.first {
133+
builder.append(
134+
.fileField(
135+
name: "image",
136+
content: img.content,
137+
contentType: img.contentType,
138+
filename: "tmpfile"
139+
)
140+
)
141+
} else {
142+
for i in 0..<self.images.count {
143+
let img = self.images[i]
144+
builder.append(
145+
.fileField(
146+
name: "image[]",
147+
content: img.content,
148+
contentType: img.contentType,
149+
filename: "tmpfile\(i)"
150+
)
151+
)
152+
}
153+
}
154+
}
139155
}
140156

141157

0 commit comments

Comments
 (0)