-
Notifications
You must be signed in to change notification settings - Fork 364
/
BaseOpenAIService.swift
254 lines (207 loc) · 8.38 KB
/
BaseOpenAIService.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
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
//
// BaseOpenAIService.swift
// Easydict
//
// Created by tisfeng on 2024/3/28.
// Copyright © 2024 izual. All rights reserved.
//
import Defaults
import Foundation
import OpenAI
// MARK: - BaseOpenAIService
// In order to solve the problems caused by inheriting the OpenAI service for custom OpenAI services, we had to add a new base class. FIX https://github.com/tisfeng/Easydict/pull/473#issuecomment-2022587699
@objcMembers
@objc(EZBaseOpenAIService)
public class BaseOpenAIService: QueryService {
// MARK: Public
override public func isStream() -> Bool {
true
}
override public func intelligentQueryTextType() -> EZQueryTextType {
Configuration.shared.intelligentQueryTextTypeForServiceType(serviceType())
}
override public func supportLanguagesDictionary() -> MMOrderedDictionary<AnyObject, AnyObject> {
let allLangauges = EZLanguageManager.shared().allLanguages
let supportedLanguages = allLangauges.filter { language in
!unsupportedLanguages.contains(language)
}
let orderedDict = MMOrderedDictionary<AnyObject, AnyObject>()
for language in supportedLanguages {
orderedDict.setObject(language.rawValue as NSString, forKey: language.rawValue as NSString)
}
return orderedDict
}
override public func queryTextType() -> EZQueryTextType {
var typeOptions: EZQueryTextType = []
let isTranslationEnabled = UserDefaults.bool(forKey: EZTranslationKey, serviceType: serviceType())
let isSentenceEnabled = UserDefaults.bool(forKey: EZSentenceKey, serviceType: serviceType())
let isDictionaryEnabled = UserDefaults.bool(forKey: EZDictionaryKey, serviceType: serviceType())
if isTranslationEnabled {
typeOptions.insert(.translation)
}
if isSentenceEnabled {
typeOptions.insert(.sentence)
}
if isDictionaryEnabled {
typeOptions.insert(.dictionary)
}
return typeOptions
}
override public func serviceUsageStatus() -> EZServiceUsageStatus {
let usageStatus = UserDefaults.string(forKey: EZServiceUsageStatusKey, serviceType: serviceType()) ?? ""
guard let value = UInt(usageStatus) else { return .default }
return EZServiceUsageStatus(rawValue: value) ?? .default
}
// swiftlint:disable identifier_name
override public func translate(
_ text: String,
from: Language,
to: Language,
completion: @escaping (EZQueryResult, Error?) -> ()
) {
let url = URL(string: endpoint)
let invalidURLError = EZError(type: .param, description: "\(serviceType().rawValue) URL is invalid")
guard let url, url.isValid else {
completion(result, invalidURLError)
return
}
updateCompletion = completion
var resultText = ""
result.from = from
result.to = to
result.isStreamFinished = false
let queryType = queryType(text: text, from: from, to: to)
let chats = chatMessages(queryType: queryType, text: text, from: from, to: to)
let query = ChatQuery(messages: chats, model: model, temperature: 0)
let openAI = OpenAI(apiToken: apiKey)
openAI.chatsStream(query: query, url: url) { [weak self] res in
guard let self else { return }
if !result.isStreamFinished {
switch res {
case let .success(chatResult):
if let content = chatResult.choices.first?.delta.content {
resultText += content
}
handleResult(queryType: queryType, resultText: resultText, error: nil, completion: completion)
case let .failure(error):
// For stream requests, certain special cases may be normal for the first part of the data transfer, but the final parsing is incorrect.
var text: String?
var err: Error? = error
if !resultText.isEmpty {
text = resultText
err = nil
logError("\(name())-(\(model)) error: \(error.localizedDescription)")
logError(String(describing: error))
}
handleResult(
queryType: queryType,
resultText: text,
error: err,
completion: completion
)
}
}
} completion: { [weak self] error in
guard let self else { return }
if !result.isStreamFinished {
if let error {
handleResult(queryType: queryType, resultText: nil, error: error, completion: completion)
} else {
// If already has error, we do not need to update it.
if result.error == nil {
resultText = getFinalResultText(text: resultText)
// log("\(name())-(\(model)): \(resultText)")
handleResult(queryType: queryType, resultText: resultText, error: nil, completion: completion)
result.isStreamFinished = true
}
}
}
}
}
// swiftlint:enable identifier_name
// MARK: Internal
let throttler = Throttler()
var updateCompletion: ((EZQueryResult, Error?) -> ())?
var model = ""
var unsupportedLanguages: [Language] = []
var availableModels: [String] {
[""]
}
var apiKey: String {
""
}
var endpoint: String {
""
}
// MARK: Private
/// Get query type by text and from && to langauge.
private func queryType(text: String, from: Language, to _: Language) -> EZQueryTextType {
let enableDictionary = queryTextType().contains(.dictionary)
var isQueryDictionary = false
if enableDictionary {
isQueryDictionary = (text as NSString).shouldQueryDictionary(withLanguage: from, maxWordCount: 2)
if isQueryDictionary {
return .dictionary
}
}
let enableSentence = queryTextType().contains(.sentence)
var isQueryEnglishSentence = false
if !isQueryDictionary, enableSentence {
let isEnglishText = from == .english
if isEnglishText {
isQueryEnglishSentence = (text as NSString).shouldQuerySentence(withLanguage: from)
if isQueryEnglishSentence {
return .sentence
}
}
}
return .translation
}
private func handleResult(
queryType: EZQueryTextType,
resultText: String?,
error: Error?,
completion: @escaping (EZQueryResult, Error?) -> ()
) {
var normalResults: [String]?
if let resultText {
normalResults = [resultText.trim()]
}
result.isStreamFinished = error != nil
result.translatedResults = normalResults
let updateCompletion = {
self.throttler.throttle { [unowned self] in
self.updateCompletion?(result, error)
}
}
switch queryType {
case .sentence, .translation:
updateCompletion()
case .dictionary:
if error != nil {
result.showBigWord = false
result.translateResultsTopInset = 0
updateCompletion()
return
}
result.showBigWord = true
result.queryText = queryModel.queryText
result.translateResultsTopInset = 6
updateCompletion()
default:
updateCompletion()
}
}
private func getFinalResultText(text: String) -> String {
var resultText = text.trim()
// Remove last </s>, fix Groq model mixtral-8x7b-32768
let stopFlag = "</s>"
if !queryModel.queryText.hasSuffix(stopFlag), resultText.hasSuffix(stopFlag) {
resultText = String(resultText.dropLast(stopFlag.count)).trim()
}
// Since it is more difficult to accurately remove redundant quotes in streaming, we wait until the end of the request to remove the quotes
let nsText = resultText as NSString
resultText = nsText.tryToRemoveQuotes().trim()
return resultText
}
}