forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserPreferenceEmbeddingConfiguration.swift
More file actions
60 lines (50 loc) · 1.75 KB
/
UserPreferenceEmbeddingConfiguration.swift
File metadata and controls
60 lines (50 loc) · 1.75 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
import AIModel
import Foundation
import Preferences
public struct UserPreferenceEmbeddingConfiguration: EmbeddingConfiguration {
public var model: EmbeddingModel {
let models = UserDefaults.shared.value(for: \.embeddingModels)
let id = UserDefaults.shared.value(for: \.defaultChatFeatureEmbeddingModelId)
return models.first { $0.id == id }
?? models.first ?? .init(id: "", name: "", format: .openAI, info: .init())
}
public var maxToken: Int {
model.info.maxTokens
}
public init() {}
}
public class OverridingEmbeddingConfiguration<
Configuration: EmbeddingConfiguration
>: EmbeddingConfiguration {
public struct Overriding {
public var modelId: String?
public var model: EmbeddingModel?
public var maxTokens: Int?
public init(
modelId: String? = nil,
model: EmbeddingModel? = nil,
maxTokens: Int? = nil
) {
self.modelId = modelId
self.model = model
self.maxTokens = maxTokens
}
}
private let configuration: Configuration
public var overriding = Overriding()
public init(overriding configuration: Configuration, with overrides: Overriding = .init()) {
overriding = overrides
self.configuration = configuration
}
public var model: EmbeddingModel {
if let model = overriding.model { return model }
let models = UserDefaults.shared.value(for: \.embeddingModels)
guard let id = overriding.modelId,
let model = models.first(where: { $0.id == id })
else { return configuration.model }
return model
}
public var maxToken: Int {
overriding.maxTokens ?? configuration.maxToken
}
}