|
| 1 | +// |
| 2 | +// LLMConfigView.swift |
| 3 | +// XCAChatGPT |
| 4 | +// |
| 5 | +// Created by Alfian Losari on 03/06/23. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +struct LLMConfigView: View { |
| 11 | + |
| 12 | + let onStartChatTapped: (_ result: LLMConfig) -> Void |
| 13 | + @State var apiKey = "" |
| 14 | + @State var llmProvider = LLMProvider.chatGPT |
| 15 | + @State var chatGPTModel = ChatGPTModel.gpt3Turbo |
| 16 | + |
| 17 | + var body: some View { |
| 18 | + #if os(macOS) |
| 19 | + ScrollView { sectionsView } |
| 20 | + .padding(.horizontal) |
| 21 | + #else |
| 22 | + List { sectionsView } |
| 23 | + #endif |
| 24 | + } |
| 25 | + |
| 26 | + @ViewBuilder |
| 27 | + var sectionsView: some View { |
| 28 | + Section("LLM Provider") { |
| 29 | + Picker("Provider", selection: $llmProvider) { |
| 30 | + ForEach(LLMProvider.allCases) { |
| 31 | + Text($0.text).id($0) |
| 32 | + } |
| 33 | + } |
| 34 | + #if !os(watchOS) |
| 35 | + .pickerStyle(.segmented) |
| 36 | + #endif |
| 37 | + } |
| 38 | + |
| 39 | + Section("Configuration") { |
| 40 | + TextField("Enter API Key", text: $apiKey) |
| 41 | + .autocorrectionDisabled() |
| 42 | + #if os(macOS) |
| 43 | + .textCase(.none) |
| 44 | + .textFieldStyle(.roundedBorder) |
| 45 | + #else |
| 46 | + .textInputAutocapitalization(.never) |
| 47 | + #endif |
| 48 | + |
| 49 | + if llmProvider == .chatGPT { |
| 50 | + Picker("Model", selection: $chatGPTModel) { |
| 51 | + ForEach(ChatGPTModel.allCases) { |
| 52 | + Text($0.text).id($0) |
| 53 | + } |
| 54 | + } |
| 55 | + #if !os(watchOS) |
| 56 | + .pickerStyle(.segmented) |
| 57 | + #endif |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + Section { |
| 62 | + Button("Start Chat") { |
| 63 | + let type: LLMConfig.ConfigType |
| 64 | + switch llmProvider { |
| 65 | + case .chatGPT: |
| 66 | + type = .chatGPT(chatGPTModel) |
| 67 | + case .palm: |
| 68 | + type = .palm |
| 69 | + } |
| 70 | + |
| 71 | + self.onStartChatTapped(.init(apiKey: apiKey, type: type)) |
| 72 | + } |
| 73 | + #if os(macOS) |
| 74 | + .buttonStyle(.borderedProminent) |
| 75 | + #endif |
| 76 | + .frame(maxWidth: .infinity) |
| 77 | + .disabled(apiKey.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) |
| 78 | + |
| 79 | + } footer: { |
| 80 | + Text(llmProvider.footerInfo) |
| 81 | + .padding(.vertical) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +struct LLMConfigView_Previews: PreviewProvider { |
| 87 | + static var previews: some View { |
| 88 | + NavigationStack { |
| 89 | + LLMConfigView { result in |
| 90 | + |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
0 commit comments