We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
import OpenAISwift import SwiftUI final class ViewModel: ObservableObject { init(){} private var client: OpenAISwift? func setup(){ client = OpenAISwift(config: OpenAISwift.Config.makeDefaultOpenAI(apiKey: "key")) } func send(text: String, completion: @escaping (String) -> Void) { client?.sendCompletion(with: text, maxTokens: 500, temperature: 0.7, completionHandler: { result in switch result { case .success(let model): let output = model.choices?.first?.text ?? "blank message" print(model) completion(output) case .failure: break } })3 } } struct ContentView: View { @ObservedObject var viewModel = ViewModel() @State var text = "" @State var models = [String]() var body: some View { VStack(alignment: .leading) { ForEach(models, id: \.self) { string in Text(string) } Spacer() HStack { TextField("在此輸入", text: $text) Button("送出") { send() } } } .onAppear { viewModel.setup() } .padding() } func send() { guard !text.trimmingCharacters(in: .whitespaces).isEmpty else { return } models.append("Me: \(text)") viewModel.send(text: text) { response in DispatchQueue.main.async { self.models.append("ChatGPT: \(response)") self.text = "" } } } } #Preview { ContentView() }
The text was updated successfully, but these errors were encountered:
Same here
Sorry, something went wrong.
No branches or pull requests
The text was updated successfully, but these errors were encountered: