forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAIView.swift
More file actions
163 lines (153 loc) · 6.08 KB
/
OpenAIView.swift
File metadata and controls
163 lines (153 loc) · 6.08 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import AppKit
import Client
import CopilotModel
import Preferences
import SwiftUI
final class OpenAIViewSettings: ObservableObject {
static let availableLocalizedLocales = Locale.availableLocalizedLocales
@AppStorage(\.openAIAPIKey) var openAIAPIKey: String
@AppStorage(\.chatGPTModel) var chatGPTModel: String
@AppStorage(\.chatGPTEndpoint) var chatGPTEndpoint: String
@AppStorage(\.chatGPTLanguage) var chatGPTLanguage: String
@AppStorage(\.chatGPTMaxToken) var chatGPTMaxToken: Int
init() {}
}
struct OpenAIView: View {
let apiKeyURL = URL(string: "https://platform.openai.com/account/api-keys")!
let modelURL = URL(
string: "https://platform.openai.com/docs/models/model-endpoint-compatibility"
)!
@Environment(\.openURL) var openURL
@StateObject var settings = OpenAIViewSettings()
var body: some View {
Section {
VStack(alignment: .leading, spacing: 8) {
Text("OpenAI")
.font(.title)
.padding(.bottom, 12)
Form {
HStack {
TextField(text: $settings.openAIAPIKey, prompt: Text("sk-*")) {
Text("OpenAI API Key")
}.textFieldStyle(.roundedBorder)
Button(action: {
openURL(apiKeyURL)
}) {
Image(systemName: "questionmark.circle.fill")
}.buttonStyle(.plain)
}
HStack {
Picker(selection: $settings.chatGPTModel) {
if !settings.chatGPTModel.isEmpty,
ChatGPTModel(rawValue: settings.chatGPTModel) == nil
{
Text(settings.chatGPTModel).tag(settings.chatGPTModel)
}
ForEach(ChatGPTModel.allCases, id: \.self) { model in
Text(model.rawValue).tag(model.rawValue)
}
} label: {
Text("ChatGPT Model")
}.pickerStyle(.menu)
Button(action: {
openURL(modelURL)
}) {
Image(systemName: "questionmark.circle.fill")
}.buttonStyle(.plain)
}
.onChange(of: settings.chatGPTModel) { newValue in
if let model = ChatGPTModel(rawValue: newValue) {
settings.chatGPTEndpoint = model.endpoint
}
}
TextField(
text: $settings.chatGPTEndpoint,
prompt: Text("https://api.openai.com/v1/chat/completions")
) {
Text("ChatGPT Server")
}.textFieldStyle(.roundedBorder)
if #available(macOS 13.0, *) {
LabeledContent("Reply in Language") {
languagePicker
}
} else {
HStack {
Text("Reply in Language")
languagePicker
}
}
if let model = ChatGPTModel(rawValue: settings.chatGPTModel) {
let binding = Binding(
get: { String(settings.chatGPTMaxToken) },
set: {
if let selectionMaxToken = Int($0) {
settings.chatGPTMaxToken = model
.maxToken < selectionMaxToken ? model
.maxToken : selectionMaxToken
} else {
settings.chatGPTMaxToken = 0
}
}
)
HStack {
Stepper(
value: $settings.chatGPTMaxToken,
in: 0...model.maxToken,
step: 1
) {
Text("Max Token")
}
TextField(text: binding) {
EmptyView()
}
.labelsHidden()
.textFieldStyle(.roundedBorder)
}
}
}
}
}
}
var languagePicker: some View {
Menu {
if !settings.chatGPTLanguage.isEmpty,
!OpenAIViewSettings.availableLocalizedLocales
.contains(settings.chatGPTLanguage)
{
Button(
settings.chatGPTLanguage,
action: { self.settings.chatGPTLanguage = settings.chatGPTLanguage }
)
}
Button(
"Auto-detected by ChatGPT",
action: { self.settings.chatGPTLanguage = "" }
)
ForEach(
OpenAIViewSettings.availableLocalizedLocales,
id: \.self
) { localizedLocales in
Button(
localizedLocales,
action: { self.settings.chatGPTLanguage = localizedLocales }
)
}
} label: {
Text(
settings.chatGPTLanguage.isEmpty
? "Auto-detected by ChatGPT"
: settings.chatGPTLanguage
)
}
}
}
struct OpenAIView_Previews: PreviewProvider {
static var previews: some View {
VStack(alignment: .leading, spacing: 8) {
OpenAIView()
}
.frame(height: 800)
.padding(.all, 8)
.background(Color.black)
}
}