adamrushy / OpenAISwift

This is a wrapper library around the ChatGPT and OpenAI HTTP API
MIT License
1.6k stars 242 forks source link

OpenAI Token is not accepted #102

Open 3009mt opened 11 months ago

3009mt commented 11 months ago

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?

MarkHoath commented 11 months ago

The documentation wasn't updated with the last release.

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

3009mt commented 11 months ago

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

perbrondum commented 9 months ago

Please update the README with this crucial information