OpenAISwift
OpenAISwift copied to clipboard
OpenAI Token is not accepted
Hi, I'm trying to develop an app and I have created a secret key by the OpenAI website and used the below code:
import SwiftUI import OpenAISwift
struct MainView: View {
@State private var chatText: String = ""
let openAI = OpenAISwift(authToken: "MY SECRET KEY FROM THE OPENAI WEBSITE")
@State private var answers: [String] = []
private var isFormValid: Bool {
!chatText.isEmptyOrWhiteSpace
}
private func performSearch() {
openAI.sendCompletion(with: chatText, maxTokens: 500) { result in
switch result {
case .success(let success):
let answer = success.choices.first?.text.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
answers.append(answer)
case .failure(let failure):
print(failure)
}
}
}
var body: some View {
VStack {
List(answers, id: \.self) { answer in
Text(answer)
}
Spacer()
HStack {
TextField("Search...", text: $chatText)
.textFieldStyle(.roundedBorder)
Button {
// action
performSearch()
} label: {
Image(systemName: "paperplane.circle.fill")
.font(.title)
.rotationEffect(Angle(degrees: 45))
}.buttonStyle(.borderless)
.tint(.blue)
.disabled(!isFormValid)
}
}.padding()
}
}
struct MainView_Previews: PreviewProvider { static var previews: some View { MainView() } }
but it giving me couple of errors:
- Cannot convert value of type 'String' to expected argument type 'OpenAISwift.Config'
- Incorrect argument label in call (have 'authToken:', expected 'config:')
I cannot find the solution. Can you help me please?
The documentation wasn't updated with the last release.
let key = "sk-...... " var openAI: OpenAISwift = OpenAISwift(config: OpenAISwift.Config.makeDefaultOpenAI(apiKey: key))
Thank you. It works when I'm writing like this: var openAI: OpenAISwift = OpenAISwift(config: OpenAISwift.Config.makeDefaultOpenAI(apiKey: "MY SECRET KEY"))
Please update the README with this crucial information