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
132 lines (108 loc) · 3.58 KB
/
UserPreferenceChatGPTConfiguration.swift
File metadata and controls
132 lines (108 loc) · 3.58 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
import Foundation
import Preferences
public struct UserPreferenceChatGPTConfiguration: ChatGPTConfiguration {
public var featureProvider: ChatFeatureProvider {
UserDefaults.shared.value(for: \.chatFeatureProvider)
}
public var temperature: Double {
min(max(0, UserDefaults.shared.value(for: \.chatGPTTemperature)), 2)
}
public var model: String {
let value = UserDefaults.shared.value(for: \.chatGPTModel)
if value.isEmpty { return "gpt-3.5-turbo" }
return value
}
public var endpoint: String {
endpoint(for: featureProvider)
}
public var apiKey: String {
apiKey(for: featureProvider)
}
public var maxTokens: Int {
UserDefaults.shared.value(for: \.chatGPTMaxToken)
}
public var stop: [String] {
[]
}
public var minimumReplyTokens: Int {
300
}
public var runFunctionsAutomatically: Bool {
true
}
public init() {}
}
public class OverridingChatGPTConfiguration: ChatGPTConfiguration {
public struct Overriding {
public var featureProvider: ChatFeatureProvider?
public var temperature: Double?
public var model: String?
public var endPoint: String?
public var apiKey: String?
public var stop: [String]?
public var maxTokens: Int?
public var minimumReplyTokens: Int?
public var runFunctionsAutomatically: Bool?
public init(
temperature: Double? = nil,
model: String? = nil,
stop: [String]? = nil,
maxTokens: Int? = nil,
minimumReplyTokens: Int? = nil,
featureProvider: ChatFeatureProvider? = nil,
endPoint: String? = nil,
apiKey: String? = nil,
runFunctionsAutomatically: Bool? = nil
) {
self.temperature = temperature
self.model = model
self.stop = stop
self.maxTokens = maxTokens
self.minimumReplyTokens = minimumReplyTokens
self.featureProvider = featureProvider
self.endPoint = endPoint
self.apiKey = apiKey
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 featureProvider: ChatFeatureProvider {
overriding.featureProvider ?? configuration.featureProvider
}
public var temperature: Double {
overriding.temperature ?? configuration.temperature
}
public var model: String {
overriding.model ?? configuration.model
}
public var endpoint: String {
overriding.endPoint
?? overriding.featureProvider.map(endpoint(for:))
?? configuration.endpoint
}
public var apiKey: String {
overriding.apiKey
?? overriding.featureProvider.map(apiKey(for:))
?? configuration.apiKey
}
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
}
}