Skip to content

Commit

Permalink
perf: improve code, store openAIVaildModels in Defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Apr 6, 2024
1 parent 4458d2e commit 9d514ae
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ extension Defaults.Keys {
EZOpenAIAvailableModelsKey,
default: OpenAIModel.allCases.map { $0.rawValue }.joined(separator: ",")
)
static let openAIVaildModels = Key<Array>(
"EZOpenAIValidModelsKey",
default: OpenAIModel.allCases.map { $0.rawValue }
)

// Custom OpenAI
static let customOpenAINameKey = Key<String?>(
Expand All @@ -223,6 +227,10 @@ extension Defaults.Keys {
static let customOpenAIEndPoint = Key<String?>(EZCustomOpenAIEndPointKey, default: "")
static let customOpenAIModel = Key<String>(EZCustomOpenAIModelKey, default: "")
static let customOpenAIModelsAvailable = Key<String?>(EZCustomOpenAIModelssAvailableKey, default: "")
static let customOpenAIVaildModels = Key<Array>(
"EZCustomOpenAIValidModelsKey",
default: [""]
)

// DeepL
static let deepLAuth = Key<String?>(EZDeepLAuthKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ class CustomOpenAIService: BaseOpenAIService {
}

override var availableModels: [String] {
let models = Defaults[.customOpenAIModelsAvailable]
guard let models, !models.isEmpty else { return [] }
return models.components(separatedBy: ",").filter { !$0.isEmpty }
Defaults[.customOpenAIVaildModels]
}

override func serviceType() -> ServiceType {
Expand Down
5 changes: 1 addition & 4 deletions Easydict/Swift/Service/OpenAI/BaseOpenAIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ public class BaseOpenAIService: QueryService {
// MARK: Internal

var availableModels: [String] {
let models = Defaults[.openAIAvailableModels]
guard let models, !models.isEmpty else { return [] }
return models.components(separatedBy: ",").map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
Defaults[.openAIVaildModels]
}

var model: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,13 @@ private class CustomOpenAIViewModel: ObservableObject {
validModels = []
return
}
validModels = availableModels.components(separatedBy: ",").filter { !$0.isEmpty }
validModels = availableModels.components(separatedBy: ",")
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty }
if validModels.count == 1 || !validModels.contains(model) {
model = validModels[0]
}

Defaults[.customOpenAIVaildModels] = validModels
}

private func serviceConfigChanged() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ private class OpenAIServiceViewModel: ObservableObject {
if validModels.count == 1 || !validModels.contains(model) {
model = validModels[0]
}

Defaults[.openAIVaildModels] = validModels
}

private func serviceConfigChanged() {
Expand Down

0 comments on commit 9d514ae

Please sign in to comment.