-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
166 additions
and
7 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
Sources/SwiftOpenAI/OpenAI/DataModels/Message/MessageChatImageInput.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Foundation | ||
|
||
public struct MessageChatImageInput: Identifiable, Hashable { | ||
public var id: UUID | ||
public var text: String | ||
public var imageURL: String | ||
public var role: MessageRoleType | ||
|
||
public init(text: String, imageURL: String, role: MessageRoleType) { | ||
self.id = UUID() | ||
self.text = text | ||
self.role = role | ||
self.imageURL = imageURL | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Sources/SwiftOpenAI/OpenAI/OpenAIEndpoints/List/ChatCompletionsImageInputEndpoint.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import Foundation | ||
|
||
struct ChatCompletionsImageInputEndpoint: Endpoint { | ||
private let model: OpenAIModelType | ||
private var messages: [[String: Any]] = [] | ||
|
||
private let optionalParameters: ChatCompletionsOptionalParameters? | ||
|
||
var method: HTTPMethod { | ||
.POST | ||
} | ||
|
||
var path: String = "chat/completions" | ||
|
||
init(model: OpenAIModelType, | ||
messages: [MessageChatImageInput], | ||
optionalParameters: ChatCompletionsOptionalParameters?) { | ||
self.model = model | ||
self.messages = Self.mapMessageModelToDictionary(messages: messages) | ||
self.optionalParameters = optionalParameters | ||
} | ||
|
||
var parameters: [String: Any]? { | ||
["model": self.model.name as Any, | ||
"messages": self.messages as Any, | ||
"temperature": self.optionalParameters?.temperature as Any, | ||
"top_p": self.optionalParameters?.topP as Any, | ||
"n": self.optionalParameters?.n as Any, | ||
"stop": self.optionalParameters?.stop as Any, | ||
"stream": self.optionalParameters?.stream as Any, | ||
"max_tokens": self.optionalParameters?.maxTokens as Any] | ||
} | ||
|
||
private static func mapMessageModelToDictionary(messages: [MessageChatImageInput]) -> [[String: Any]] { | ||
return messages.map { message in | ||
var contentArray: [[String: Any]] = [] | ||
contentArray.append(["type": "text", "text": message.text]) | ||
|
||
if !message.imageURL.isEmpty { | ||
contentArray.append(["type": "image_url", "image_url": ["url": message.imageURL]]) | ||
} | ||
|
||
return ["role": message.role.rawValue, "content": contentArray] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
.../SwiftOpenAI/OpenAI/Requests/ChatCompletions/CreateChatCompletionsImageInputRequest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Foundation | ||
import Foundation | ||
|
||
protocol CreateChatCompletionsImageInputRequestProtocol { | ||
func execute(api: API, | ||
apiKey: String, | ||
model: OpenAIModelType, | ||
messages: [MessageChatImageInput], | ||
optionalParameters: ChatCompletionsOptionalParameters?) async throws -> ChatCompletionsDataModel? | ||
} | ||
|
||
final public class CreateChatCompletionsImageInputRequest: CreateChatCompletionsImageInputRequestProtocol { | ||
public typealias Init = (_ api: API, | ||
_ apiKey: String, | ||
_ model: OpenAIModelType, | ||
_ messages: [MessageChatImageInput], | ||
_ optionalParameters: ChatCompletionsOptionalParameters?) async throws -> ChatCompletionsDataModel? | ||
|
||
public init() { } | ||
|
||
public func execute(api: API, | ||
apiKey: String, | ||
model: OpenAIModelType, | ||
messages: [MessageChatImageInput], | ||
optionalParameters: ChatCompletionsOptionalParameters?) async throws -> ChatCompletionsDataModel? { | ||
var endpoint = OpenAIEndpoints.chatCompletionsWithImageInput(model: model, messages: messages, optionalParameters: optionalParameters).endpoint | ||
api.routeEndpoint(&endpoint, environment: OpenAIEnvironmentV1()) | ||
|
||
var urlRequest = api.buildURLRequest(endpoint: endpoint) | ||
api.addHeaders(urlRequest: &urlRequest, | ||
headers: ["Content-Type": "application/json", | ||
"Authorization": "Bearer \(apiKey)"]) | ||
|
||
let result = await api.execute(with: urlRequest) | ||
|
||
let jsonDecoder = JSONDecoder() | ||
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase | ||
|
||
return try api.parse(result, | ||
type: ChatCompletionsDataModel.self, | ||
jsonDecoder: jsonDecoder, | ||
errorType: OpenAIAPIError.self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters