-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathViewModel.swift
176 lines (141 loc) · 4.99 KB
/
ViewModel.swift
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
//
// Copyright 2024 Picovoice Inc.
// You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
// file accompanying this source.
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//
import PicoLLM
import Combine
import Foundation
class ViewModel: ObservableObject {
private let ACCESS_KEY = "${YOUR_ACCESS_KEY_HERE}"
private var picollm: PicoLLM?
private var dialog: PicoLLMDialog?
private var timerTick = CFAbsoluteTimeGetCurrent()
private var timerTock = CFAbsoluteTimeGetCurrent()
private var numTokens = 0
static let modelLoadStatusTextDefault = """
Start by loading a `.pllm` model file.
You can download directly to your device or airdrop from a Mac.
"""
@Published var modelLoadStatusText = modelLoadStatusTextDefault
@Published var enableLoadModelButton = true
@Published var showFileImporter = false
@Published var selectedModelUrl: URL?
@Published var picoLLMLoaded = false
@Published var promptText = ""
@Published var isGenerating = false
@Published var chatText: [Message] = []
@Published var errorMessage = ""
deinit {
if picollm != nil {
picollm!.delete()
}
}
public func extractModelFile() {
showFileImporter = true
}
public func loadPicollm() {
errorMessage = ""
modelLoadStatusText = "Loading picoLLM..."
enableLoadModelButton = false
let modelAccess = selectedModelUrl!.startAccessingSecurityScopedResource()
if !modelAccess {
errorMessage = "Can't get permissions to access model file"
enableLoadModelButton = true
return
}
DispatchQueue.global(qos: .userInitiated).async { [self] in
do {
picollm = try PicoLLM(accessKey: ACCESS_KEY, modelPath: selectedModelUrl!.path)
dialog = try picollm!.getDialog()
DispatchQueue.main.async { [self] in
picoLLMLoaded = true
}
} catch {
DispatchQueue.main.async { [self] in
errorMessage = "\(error.localizedDescription)"
}
}
DispatchQueue.main.async { [self] in
selectedModelUrl!.stopAccessingSecurityScopedResource()
modelLoadStatusText = ViewModel.modelLoadStatusTextDefault
enableLoadModelButton = true
}
}
}
public func unloadPicollm() {
if picollm != nil {
picollm!.delete()
}
picollm = nil
errorMessage = ""
promptText = ""
chatText.removeAll()
picoLLMLoaded = false
}
private func streamCallback(completion: String) {
DispatchQueue.main.async { [self] in
chatText[chatText.count - 1].append(text: completion)
if numTokens == 0 {
timerTick = CFAbsoluteTimeGetCurrent()
}
timerTock = CFAbsoluteTimeGetCurrent()
numTokens += 1
}
}
public func generate() {
if promptText.isEmpty {
return
}
errorMessage = ""
isGenerating = true
numTokens = 0
DispatchQueue.global(qos: .userInitiated).async { [self] in
do {
try dialog!.addHumanRequest(content: promptText)
DispatchQueue.main.async { [self] in
chatText.append(Message(speaker: "You", msg: promptText))
chatText.append(Message(speaker: "picoLLM", msg: ""))
}
let result = try picollm!.generate(
prompt: dialog!.prompt(),
completionTokenLimit: 128,
streamCallback: streamCallback)
try dialog!.addLLMResponse(content: result.completion)
} catch {
DispatchQueue.main.async { [self] in
errorMessage = "\(error.localizedDescription)"
enableLoadModelButton = true
}
}
DispatchQueue.main.async { [self] in
promptText = ""
isGenerating = false
}
}
}
public func interrupt() {
do {
try picollm?.interrupt()
} catch {
DispatchQueue.main.async { [self] in
errorMessage = "\(error.localizedDescription)"
enableLoadModelButton = true
}
}
}
public func clearText() {
promptText = ""
chatText.removeAll()
}
}
struct Message: Equatable {
var speaker: String
var msg: String
mutating func append(text: String) {
self.msg.append(text)
}
}