forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatCompletionsAPIDefinition.swift
More file actions
312 lines (275 loc) · 8.89 KB
/
ChatCompletionsAPIDefinition.swift
File metadata and controls
312 lines (275 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import AIModel
import ChatBasic
import CodableWrappers
import Foundation
import Preferences
struct ChatCompletionsRequestBody: Equatable {
struct Message: Equatable {
enum Role: String, Equatable {
case system
case user
case assistant
case tool
var asChatMessageRole: ChatMessage.Role {
switch self {
case .system:
return .system
case .user:
return .user
case .assistant:
return .assistant
case .tool:
return .user
}
}
}
struct Image: Equatable {
enum Format: String {
case png = "image/png"
case jpeg = "image/jpeg"
case gif = "image/gif"
}
var base64EncodeData: String
var format: Format
var dataURLString: String {
return "data:\(format.rawValue);base64,\(base64EncodeData)"
}
}
struct Audio: Equatable {
enum Format: String {
case wav
case mp3
}
var data: Data
var format: Format
}
/// The role of the message.
var role: Role
/// The content of the message.
var content: String
/// When we want to reply to a function call with the result, we have to provide the
/// name of the function call, and include the result in `content`.
///
/// - important: It's required when the role is `function`.
var name: String? = nil
/// Tool calls in an assistant message.
var toolCalls: [MessageToolCall]? = nil
/// When we want to call a tool, we have to provide the id of the call.
///
/// - important: It's required when the role is `tool`.
var toolCallId: String? = nil
/// Images to include in the message.
var images: [Image] = []
/// Audios to include in the message.
var audios: [Audio] = []
/// Cache the message if possible.
var cacheIfPossible: Bool = false
}
struct MessageFunctionCall: Equatable {
/// The name of the
var name: String
/// A JSON string.
var arguments: String?
}
struct MessageToolCall: Equatable {
/// The id of the tool call.
var id: String
/// The type of the tool.
var type: String
/// The function call.
var function: MessageFunctionCall
}
struct Tool: Equatable {
var type: String = "function"
var function: ChatGPTFunctionSchema
}
var model: String
var messages: [Message]
var temperature: Double?
var stream: Bool?
var stop: [String]?
var maxTokens: Int?
/// Pass nil to let the bot decide.
var toolChoice: FunctionCallStrategy?
var tools: [Tool]?
init(
model: String,
messages: [Message],
temperature: Double? = nil,
stream: Bool? = nil,
stop: [String]? = nil,
maxTokens: Int? = nil,
toolChoice: FunctionCallStrategy? = nil,
tools: [Tool] = []
) {
self.model = model
self.messages = messages
self.temperature = temperature
self.stream = stream
self.stop = stop
self.maxTokens = maxTokens
if UserDefaults.shared.value(for: \.disableFunctionCalling) {
self.toolChoice = nil
self.tools = nil
} else {
self.toolChoice = toolChoice
self.tools = tools.isEmpty ? nil : tools
}
}
}
struct EmptyMessageFunctionCall: FallbackValueProvider {
static var defaultValue: ChatCompletionsRequestBody.MessageFunctionCall {
.init(name: "")
}
}
public enum FunctionCallStrategy: Codable, Equatable {
/// Forbid the bot to call any function.
case none
/// Let the bot choose what function to call.
case auto
/// Force the bot to call a function with the given name.
case function(name: String)
struct CallFunctionNamed: Codable {
var type = "function"
let function: Function
struct Function: Codable {
var name: String
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .none:
try container.encode("none")
case .auto:
try container.encode("auto")
case let .function(name):
try container.encode(CallFunctionNamed(function: .init(name: name)))
}
}
}
// MARK: - Stream API
protocol ChatCompletionsStreamAPI {
func callAsFunction() async throws -> AsyncThrowingStream<ChatCompletionsStreamDataChunk, Error>
}
extension ChatCompletionsStreamAPI {
static func setupExtraHeaderFields(
_ request: inout URLRequest,
model: ChatModel,
apiKey: String
) async {
let parser = HeaderValueParser()
for field in model.info.customHeaderInfo.headers where !field.key.isEmpty {
let value = await parser.parse(
field.value,
context: .init(modelName: model.info.modelName, apiKey: apiKey)
)
request.setValue(value, forHTTPHeaderField: field.key)
}
}
}
extension AsyncSequence {
func toStream() -> AsyncThrowingStream<Element, Error> {
AsyncThrowingStream { continuation in
let task = Task {
do {
for try await element in self {
continuation.yield(element)
}
continuation.finish()
} catch {
continuation.finish(throwing: error)
}
}
continuation.onTermination = { _ in
task.cancel()
}
}
}
}
struct ChatCompletionsStreamDataChunk {
struct Delta {
struct FunctionCall {
var name: String?
var arguments: String?
}
struct ToolCall {
var index: Int?
var id: String?
var type: String?
var function: FunctionCall?
}
var role: ChatCompletionsRequestBody.Message.Role?
var content: String?
var reasoningContent: String?
var toolCalls: [ToolCall]?
}
struct Usage: Codable, Equatable {
var promptTokens: Int?
var completionTokens: Int?
var cachedTokens: Int?
var otherUsage: [String: Int]
}
var id: String?
var object: String?
var model: String?
var message: Delta?
var finishReason: String?
var usage: Usage?
}
// MARK: - Non Stream API
protocol ChatCompletionsAPI {
func callAsFunction() async throws -> ChatCompletionResponseBody
}
struct ChatCompletionResponseBody: Equatable {
struct Message: Equatable {
typealias Role = ChatCompletionsRequestBody.Message.Role
typealias MessageToolCall = ChatCompletionsRequestBody.MessageToolCall
/// The role of the message.
var role: Role
/// The content of the message.
var content: String?
/// The reasoning content of the message.
var reasoningContent: String?
/// When we want to reply to a function call with the result, we have to provide the
/// name of the function call, and include the result in `content`.
///
/// - important: It's required when the role is `function`.
var name: String?
/// Tool calls in an assistant message.
var toolCalls: [MessageToolCall]?
/// When we want to call a tool, we have to provide the id of the call.
///
/// - important: It's required when the role is `tool`.
var toolCallId: String?
}
struct Usage: Equatable {
var promptTokens: Int
var completionTokens: Int
var cachedTokens: Int
var otherUsage: [String: Int]
mutating func merge(with other: ChatCompletionsStreamDataChunk.Usage) {
promptTokens += other.promptTokens ?? 0
completionTokens += other.completionTokens ?? 0
cachedTokens += other.cachedTokens ?? 0
for (key, value) in other.otherUsage {
otherUsage[key, default: 0] += value
}
}
mutating func merge(with other: Self) {
promptTokens += other.promptTokens
completionTokens += other.completionTokens
cachedTokens += other.cachedTokens
for (key, value) in other.otherUsage {
otherUsage[key, default: 0] += value
}
}
}
var id: String?
var object: String
var model: String
var message: Message
var otherChoices: [Message]
var finishReason: String
var usage: Usage?
}