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
139 lines (115 loc) · 4.16 KB
/
UserPreferenceChatGPTConfiguration.swift
File metadata and controls
139 lines (115 loc) · 4.16 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
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
import AIModel
import Foundation
import Keychain
import Preferences
public struct UserPreferenceChatGPTConfiguration: ChatGPTConfiguration {
public var chatModelKey: KeyPath<UserDefaultPreferenceKeys, PreferenceKey<String>>?
public var temperature: Double {
min(max(0, UserDefaults.shared.value(for: \.chatGPTTemperature)), 2)
}
public var model: ChatModel? {
let models = UserDefaults.shared.value(for: \.chatModels)
if let chatModelKey {
let id = UserDefaults.shared.value(for: chatModelKey)
if let model = models.first(where: { $0.id == id }) {
return model
}
}
let id = UserDefaults.shared.value(for: \.defaultChatFeatureChatModelId)
return models.first { $0.id == id }
?? models.first
}
public var maxTokens: Int {
model?.info.maxTokens ?? 0
}
public var stop: [String] {
[]
}
public var minimumReplyTokens: Int {
maxTokens / 5
}
public var runFunctionsAutomatically: Bool {
true
}
public var shouldEndTextWindow: (String) -> Bool {
{ _ in true }
}
public init(chatModelKey: KeyPath<UserDefaultPreferenceKeys, PreferenceKey<String>>? = nil) {
self.chatModelKey = chatModelKey
}
}
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 var apiKey: String?
public init(
temperature: Double? = nil,
modelId: String? = nil,
model: ChatModel? = nil,
stop: [String]? = nil,
maxTokens: Int? = nil,
minimumReplyTokens: Int? = nil,
runFunctionsAutomatically: Bool? = nil,
apiKey: String? = nil
) {
self.temperature = temperature
self.modelId = modelId
self.model = model
self.stop = stop
self.maxTokens = maxTokens
self.minimumReplyTokens = minimumReplyTokens
self.runFunctionsAutomatically = runFunctionsAutomatically
self.apiKey = apiKey
}
}
private let configuration: ChatGPTConfiguration
public var overriding = Overriding()
public var textWindowTerminator: ((String) -> Bool)?
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 {
if let maxTokens = overriding.maxTokens { return maxTokens }
if let model { return model.info.maxTokens }
return configuration.maxTokens
}
public var minimumReplyTokens: Int {
if let minimumReplyTokens = overriding.minimumReplyTokens { return minimumReplyTokens }
return maxTokens / 5
}
public var runFunctionsAutomatically: Bool {
overriding.runFunctionsAutomatically ?? configuration.runFunctionsAutomatically
}
public var apiKey: String {
if let apiKey = overriding.apiKey { return apiKey }
guard let name = model?.info.apiKeyName else { return configuration.apiKey }
return (try? Keychain.apiKey.get(name)) ?? configuration.apiKey
}
public var shouldEndTextWindow: (String) -> Bool {
textWindowTerminator ?? configuration.shouldEndTextWindow
}
}