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
106 lines (96 loc) · 3.69 KB
/
OpenAIView.swift
File metadata and controls
106 lines (96 loc) · 3.69 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
import AppKit
import Client
import CopilotModel
import Preferences
import SwiftUI
final class OpenAIViewSettings: ObservableObject {
@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 {
Text("OpenAI API Key")
TextField(text: $settings.openAIAPIKey, prompt: Text("sk-*")) {
EmptyView()
}.textFieldStyle(.copilot)
Button(action: {
openURL(apiKeyURL)
}) {
Image(systemName: "questionmark.circle.fill")
}
.buttonStyle(.plain)
}
HStack {
Text("ChatGPT Model")
TextField(text: $settings.chatGPTModel, prompt: Text("gpt-3.5-turbo")) {
EmptyView()
}.textFieldStyle(.copilot)
Button(action: {
openURL(modelURL)
}) {
Image(systemName: "questionmark.circle.fill")
}
.buttonStyle(.plain)
}
HStack {
Text("ChatGPT Endpoint")
TextField(
text: $settings.chatGPTEndpoint,
prompt: Text("https://api.openai.com/v1/chat/completions")
) {
EmptyView()
}.textFieldStyle(.copilot)
}
HStack {
Text("Reply in Language")
TextField(
text: $settings.chatGPTLanguage,
prompt: Text("e.g. English. Leave it blank to let the bot decide.")
) {
EmptyView()
}.textFieldStyle(.copilot)
}
HStack {
Text("Max Token")
TextField(
text: .init(get: {
String(settings.chatGPTMaxToken)
}, set: { newValue in
settings.chatGPTMaxToken = Int(newValue) ?? 0
})
) {
EmptyView()
}.textFieldStyle(.copilot)
}
}
}
}
}
}
struct OpenAIView_Previews: PreviewProvider {
static var previews: some View {
VStack(alignment: .leading, spacing: 8) {
OpenAIView()
}
.frame(height: 800)
.padding(.all, 8)
.background(Color.black)
}
}