forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatCompletionsAPIDefinition.swift
More file actions
93 lines (78 loc) · 2.16 KB
/
ChatCompletionsAPIDefinition.swift
File metadata and controls
93 lines (78 loc) · 2.16 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
import CodableWrappers
import Foundation
import Preferences
struct ChatCompletionsRequestBody: Codable, Equatable {
struct Message: Codable, Equatable {
enum Role: String, Codable, Equatable {
case user
case assistant
var asChatMessageRole: ChatMessage.Role {
switch self {
case .user:
return .user
case .assistant:
return .assistant
}
}
}
/// The role of the message.
var role: Role
/// The content of the message.
var content: String
}
var messages: [Message]
var temperature: Double?
var stream: Bool?
var stop: [String]?
init(
messages: [Message],
temperature: Double? = nil,
stream: Bool? = nil,
stop: [String]? = nil
) {
self.messages = messages
self.temperature = temperature
self.stream = stream
self.stop = stop
}
}
// MARK: - Stream API
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 {
var role: ChatCompletionsRequestBody.Message.Role?
var content: String?
}
var id: String?
var object: String?
var model: String?
var message: Delta?
var finishReason: String?
}
// MARK: - Non Stream API
struct ChatCompletionResponseBody: Codable, Equatable {
typealias Message = ChatCompletionsRequestBody.Message
var id: String?
var object: String
var message: Message
var otherChoices: [Message]
var finishReason: String
}