Skip to content

Commit

Permalink
added support for Create image edit
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-zhuravel committed Apr 20, 2023
1 parent d3f6050 commit 9431444
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 4 deletions.
2 changes: 1 addition & 1 deletion OpenAIKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'OpenAIKit'
s.version = '1.4.0'
s.version = '1.5.0'
s.summary = 'OpenAI is a community-maintained repository containing Swift implementation over OpenAI public API.'

s.description = <<-DESC
Expand Down
6 changes: 3 additions & 3 deletions Sources/OpenAIKit/Helpers/NetworkRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ protocol Endpoint {

enum OpenAIEndpoint {
case completions

case chatCompletions

case edits

case dalleImage
case dalleImageEdit
}

extension OpenAIEndpoint: Endpoint {
Expand All @@ -49,6 +47,8 @@ extension OpenAIEndpoint: Endpoint {
return "/v1/edits"
case .dalleImage:
return "/v1/images/generations"
case .dalleImageEdit:
return "/v1/images/edits"
}
}

Expand Down
27 changes: 27 additions & 0 deletions Sources/OpenAIKit/Models/ImagesModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,30 @@ public struct ImagesResponse: Codable {
public let created: TimeInterval
public var data: [AIImage]
}

public struct ImageEditRequest: Codable {
/// The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask.
public var image: String
/// An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
public var mask: String?
/// A text description of the desired image(s). The maximum length is 1000 characters.
public var prompt: String
/// The number of images to generate. Must be between 1 and 10.
public var n: Int? = nil
/// The size of the generated images. Must be one of `size256`, `size512`, or `size1024`.
public var size: AIImageSize = .size1024
/// The format in which the generated images are returned. Must be one of `url` or `b64_json`
/// DEFAULT: url
public var responseFormat: String?
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
public var user: String? = nil
}

public struct ImageEditResponse: Codable {
public struct AIImage: Codable {
public let url: String
}

public let created: TimeInterval
public var data: [AIImage]
}
14 changes: 14 additions & 0 deletions Sources/OpenAIKit/OpenAIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,18 @@ extension OpenAIKit {

return headers
}

var baseMultipartHeaders: OpenAIHeaders {
var headers: OpenAIHeaders = [:]

headers["Authorization"] = "Bearer \(apiToken)"

if let organization {
headers["OpenAI-Organization"] = organization
}

headers["content-type"] = "multipart/form-data"

return headers
}
}
50 changes: 50 additions & 0 deletions Sources/OpenAIKit/OpenAIKitRequests/Images.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation
@available(swift 5.5)
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
public extension OpenAIKit {
// MARK: - Create image
/// Given a prompt and/or an input image, the model will generate a new image.
///
/// - Parameters:
Expand Down Expand Up @@ -47,4 +48,53 @@ public extension OpenAIKit {
}
}
}

// MARK: - Create image edit
/// Given a prompt and an input image, the model will genreate generate a modified version of the input image
///
/// - Parameters:
/// - image: The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask.
/// - prompt: A text description of the desired image(s). The maximum length is 1000 characters.
/// - size: The size of the generated images. Must be one of `size256`, `size512`, or `size1024`.
/// - responseFormat: The format in which the generated images are returned. Must be one of url or b64_json.
/// - mask: An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
/// - user: A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// - n : How many completions to generate for each prompt.
func sendImageEditRequest(image: String,
prompt: String,
size: AIImageSize = .size1024,
responseFormat: String? = nil,
mask: String? = nil,
user: String? = nil,
n: Int? = nil,
completion: @escaping (Result<ImagesResponse, Error>) -> Void)
{
let endpoint = OpenAIEndpoint.dalleImageEdit

let requestBody = ImageEditRequest(image: image, mask: mask, prompt: prompt, n: n, size: size, responseFormat: responseFormat, user: user)

let requestData = try? jsonEncoder.encode(requestBody)

let headers = baseMultipartHeaders

network.request(endpoint.method, url: endpoint.urlPath, body: requestData, headers: headers, completion: completion)
}

@available(swift 5.5)
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
func sendImageEditRequest(image: String,
prompt: String,
size: AIImageSize = .size1024,
responseFormat: String? = nil,
mask: String? = nil,
user: String? = nil,
n: Int? = nil) async -> Result<ImagesResponse, Error>
{
return await withCheckedContinuation { continuation in
sendImageEditRequest(image: image, prompt: prompt, size: size, responseFormat: responseFormat, mask: mask, user: user, n: n) {
result in
continuation.resume(returning: result)
}
}
}
}

0 comments on commit 9431444

Please sign in to comment.