OpenAISwift icon indicating copy to clipboard operation
OpenAISwift copied to clipboard

OpenAI Token is not accepted

Open 3009mt opened this issue 1 year ago • 3 comments

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:

  1. Cannot convert value of type 'String' to expected argument type 'OpenAISwift.Config'
  2. Incorrect argument label in call (have 'authToken:', expected 'config:')

I cannot find the solution. Can you help me please?

3009mt avatar Jul 17 '23 20:07 3009mt

The documentation wasn't updated with the last release.

let key = "sk-...... " var openAI: OpenAISwift = OpenAISwift(config: OpenAISwift.Config.makeDefaultOpenAI(apiKey: key))

MarkHoath avatar Jul 18 '23 10:07 MarkHoath

Thank you. It works when I'm writing like this: var openAI: OpenAISwift = OpenAISwift(config: OpenAISwift.Config.makeDefaultOpenAI(apiKey: "MY SECRET KEY"))

3009mt avatar Jul 18 '23 19:07 3009mt

Please update the README with this crucial information

perbrondum avatar Oct 05 '23 10:10 perbrondum