forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserPreferenceChatGPTConfiguration.swift
More file actions
105 lines (87 loc) · 2.98 KB
/
UserPreferenceChatGPTConfiguration.swift
File metadata and controls
105 lines (87 loc) · 2.98 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
93
94
95
96
97
98
99
100
101
102
103
104
import AIModel
import Foundation
import Preferences
public struct UserPreferenceChatGPTConfiguration: ChatGPTConfiguration {
public var temperature: Double {
min(max(0, UserDefaults.shared.value(for: \.chatGPTTemperature)), 2)
}
public var model: ChatModel {
let models = UserDefaults.shared.value(for: \.chatModels)
let id = UserDefaults.shared.value(for: \.defaultChatFeatureChatModelId)
return models.first { $0.id == id }
?? models.first ?? .init(id: "", name: "", format: .openAI, info: .init())
}
public var maxTokens: Int {
model.info.maxTokens
}
public var stop: [String] {
[]
}
public var minimumReplyTokens: Int {
300
}
public var runFunctionsAutomatically: Bool {
true
}
public init() {}
}
public class OverridingChatGPTConfiguration: ChatGPTConfiguration {
public struct Overriding: Codable {
public var temperature: Double?
public var modelId: String?
public var model: ChatModel?
public var stop: [String]?
public var maxTokens: Int?
public var minimumReplyTokens: Int?
public var runFunctionsAutomatically: Bool?
public init(
temperature: Double? = nil,
modelId: String? = nil,
model: ChatModel? = nil,
stop: [String]? = nil,
maxTokens: Int? = nil,
minimumReplyTokens: Int? = nil,
runFunctionsAutomatically: Bool? = nil
) {
self.temperature = temperature
self.modelId = modelId
self.model = model
self.stop = stop
self.maxTokens = maxTokens
self.minimumReplyTokens = minimumReplyTokens
self.runFunctionsAutomatically = runFunctionsAutomatically
}
}
private let configuration: ChatGPTConfiguration
public var overriding = Overriding()
public init(
overriding configuration: any ChatGPTConfiguration,
with overrides: Overriding = .init()
) {
overriding = overrides
self.configuration = configuration
}
public var temperature: Double {
overriding.temperature ?? configuration.temperature
}
public var model: ChatModel {
if let model = overriding.model { return model }
let models = UserDefaults.shared.value(for: \.chatModels)
guard let id = overriding.modelId,
let model = models.first(where: { $0.id == id })
else { return configuration.model }
return model
}
public var stop: [String] {
overriding.stop ?? configuration.stop
}
public var maxTokens: Int {
overriding.maxTokens ?? configuration.maxTokens
}
public var minimumReplyTokens: Int {
overriding.minimumReplyTokens ?? configuration.minimumReplyTokens
}
public var runFunctionsAutomatically: Bool {
overriding.runFunctionsAutomatically ?? configuration.runFunctionsAutomatically
}
}