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
207 lines (182 loc) · 5.54 KB
/
ChatCompletionsAPIDefinition.swift
File metadata and controls
207 lines (182 loc) · 5.54 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
import AIModel
import CodableWrappers
import Foundation
import Preferences
import ChatBasic
struct ChatCompletionsRequestBody: Codable, Equatable {
struct Message: Codable, Equatable {
enum Role: String, Codable, 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
}
}
}
/// 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?
/// 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 MessageFunctionCall: Codable, Equatable {
/// The name of the
var name: String
/// A JSON string.
var arguments: String?
}
struct MessageToolCall: Codable, 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: Codable, 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 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 toolCalls: [ToolCall]?
}
var id: String?
var object: String?
var model: String?
var message: Delta?
var finishReason: String?
}
// MARK: - Non Stream API
protocol ChatCompletionsAPI {
func callAsFunction() async throws -> ChatCompletionResponseBody
}
struct ChatCompletionResponseBody: Codable, Equatable {
typealias Message = ChatCompletionsRequestBody.Message
var id: String?
var object: String
var model: String
var message: Message
var otherChoices: [Message]
var finishReason: String
}