11import AIModel
2+ import ChatBasic
23import CodableWrappers
34import Foundation
45import Preferences
5- import ChatBasic
66
7- struct ChatCompletionsRequestBody : Codable , Equatable {
8- struct Message : Codable , Equatable {
9- enum Role : String , Codable , Equatable {
7+ struct ChatCompletionsRequestBody : Equatable {
8+ struct Message : Equatable {
9+ enum Role : String , Equatable {
1010 case system
1111 case user
1212 case assistant
1313 case tool
14-
14+
1515 var asChatMessageRole : ChatMessage . Role {
1616 switch self {
1717 case . system:
@@ -25,6 +25,31 @@ struct ChatCompletionsRequestBody: Codable, Equatable {
2525 }
2626 }
2727 }
28+
29+ struct Image : Equatable {
30+ enum Format : String {
31+ case png = " image/png "
32+ case jpeg = " image/jpeg "
33+ case gif = " image/gif "
34+ }
35+ var data : Data
36+ var format : Format
37+
38+ var dataURLString : String {
39+ let base64 = data. base64EncodedString ( )
40+ return " data: \( format. rawValue) ;base64, \( base64) "
41+ }
42+ }
43+
44+ struct Audio : Equatable {
45+ enum Format : String {
46+ case wav
47+ case mp3
48+ }
49+
50+ var data : Data
51+ var format : Format
52+ }
2853
2954 /// The role of the message.
3055 var role : Role
@@ -34,25 +59,29 @@ struct ChatCompletionsRequestBody: Codable, Equatable {
3459 /// name of the function call, and include the result in `content`.
3560 ///
3661 /// - important: It's required when the role is `function`.
37- var name : String ?
62+ var name : String ? = nil
3863 /// Tool calls in an assistant message.
39- var toolCalls : [ MessageToolCall ] ?
64+ var toolCalls : [ MessageToolCall ] ? = nil
4065 /// When we want to call a tool, we have to provide the id of the call.
4166 ///
4267 /// - important: It's required when the role is `tool`.
43- var toolCallId : String ?
68+ var toolCallId : String ? = nil
69+ /// Images to include in the message.
70+ var images : [ Image ] = [ ]
71+ /// Audios to include in the message.
72+ var audios : [ Audio ] = [ ]
4473 /// Cache the message if possible.
4574 var cacheIfPossible : Bool = false
4675 }
4776
48- struct MessageFunctionCall : Codable , Equatable {
77+ struct MessageFunctionCall : Equatable {
4978 /// The name of the
5079 var name : String
5180 /// A JSON string.
5281 var arguments : String ?
5382 }
5483
55- struct MessageToolCall : Codable , Equatable {
84+ struct MessageToolCall : Equatable {
5685 /// The id of the tool call.
5786 var id : String
5887 /// The type of the tool.
@@ -61,7 +90,7 @@ struct ChatCompletionsRequestBody: Codable, Equatable {
6190 var function : MessageFunctionCall
6291 }
6392
64- struct Tool : Codable , Equatable {
93+ struct Tool : Equatable {
6594 var type : String = " function "
6695 var function : ChatGPTFunctionSchema
6796 }
@@ -182,11 +211,11 @@ struct ChatCompletionsStreamDataChunk {
182211 var content : String ?
183212 var toolCalls : [ ToolCall ] ?
184213 }
185-
214+
186215 struct Usage : Codable , Equatable {
187216 var promptTokens : Int ?
188217 var completionTokens : Int ?
189-
218+
190219 var cachedTokens : Int ?
191220 var otherUsage : [ String : Int ]
192221 }
@@ -205,16 +234,35 @@ protocol ChatCompletionsAPI {
205234 func callAsFunction( ) async throws -> ChatCompletionResponseBody
206235}
207236
208- struct ChatCompletionResponseBody : Codable , Equatable {
209- typealias Message = ChatCompletionsRequestBody . Message
210-
211- struct Usage : Codable , Equatable {
237+ struct ChatCompletionResponseBody : Equatable {
238+ struct Message : Equatable {
239+ typealias Role = ChatCompletionsRequestBody . Message . Role
240+ typealias MessageToolCall = ChatCompletionsRequestBody . MessageToolCall
241+
242+ /// The role of the message.
243+ var role : Role
244+ /// The content of the message.
245+ var content : String ?
246+ /// When we want to reply to a function call with the result, we have to provide the
247+ /// name of the function call, and include the result in `content`.
248+ ///
249+ /// - important: It's required when the role is `function`.
250+ var name : String ?
251+ /// Tool calls in an assistant message.
252+ var toolCalls : [ MessageToolCall ] ?
253+ /// When we want to call a tool, we have to provide the id of the call.
254+ ///
255+ /// - important: It's required when the role is `tool`.
256+ var toolCallId : String ?
257+ }
258+
259+ struct Usage : Equatable {
212260 var promptTokens : Int
213261 var completionTokens : Int
214-
262+
215263 var cachedTokens : Int
216264 var otherUsage : [ String : Int ]
217-
265+
218266 mutating func merge( with other: ChatCompletionsStreamDataChunk . Usage ) {
219267 promptTokens += other. promptTokens ?? 0
220268 completionTokens += other. completionTokens ?? 0
@@ -223,7 +271,7 @@ struct ChatCompletionResponseBody: Codable, Equatable {
223271 otherUsage [ key, default: 0 ] += value
224272 }
225273 }
226-
274+
227275 mutating func merge( with other: Self ) {
228276 promptTokens += other. promptTokens
229277 completionTokens += other. completionTokens
0 commit comments