forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatGPTConfiguration.swift
More file actions
129 lines (112 loc) · 4.15 KB
/
ChatGPTConfiguration.swift
File metadata and controls
129 lines (112 loc) · 4.15 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
import Foundation
import AIModel
import Preferences
import Keychain
public protocol ChatGPTConfiguration {
var model: ChatModel? { get }
var temperature: Double { get }
var apiKey: String { get }
var stop: [String] { get }
var maxTokens: Int { get }
var minimumReplyTokens: Int { get }
var runFunctionsAutomatically: Bool { get }
var shouldEndTextWindow: (String) -> Bool { get }
}
public extension ChatGPTConfiguration {
var endpoint: String {
model?.endpoint ?? ""
}
var apiKey: String {
guard let name = model?.info.apiKeyName else { return "" }
return (try? Keychain.apiKey.get(name)) ?? ""
}
func overriding(
_ overrides: OverridingChatGPTConfiguration.Overriding
) -> OverridingChatGPTConfiguration {
.init(overriding: self, with: overrides)
}
func overriding(
_ update: (inout OverridingChatGPTConfiguration.Overriding) -> Void = { _ in }
) -> OverridingChatGPTConfiguration {
var overrides = OverridingChatGPTConfiguration.Overriding()
update(&overrides)
return .init(overriding: self, with: overrides)
}
}
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 else { return configuration.model }
if id == "com.github.copilot" {
return .init(id: id, name: "GitHub Copilot", format: .openAI, info: .init())
}
guard 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
}
}