OpenAISwift icon indicating copy to clipboard operation
OpenAISwift copied to clipboard

no response from openAI webAPI?

Open a-elnajjar opened this issue 2 years ago • 3 comments

I'm encountering an issue with OpenAI's Swift library (OpenAISwift) where I'm receiving a nil response. I've created a ChatGPT clone to utilize the ChatGPT API through OpenAISwift to communicate with the API and receive responses. However, despite receiving an HTTP 200 success status code, I'm getting nil in the 'success.choices' response. Can you help me troubleshoot this problem?

struct MainView: View {
    
    @State private var chatText: String = ""
    var openAI: OpenAISwift = OpenAISwift(config: OpenAISwift.Config.makeDefaultOpenAI(apiKey: "sk-"))
    
    @EnvironmentObject private var model: Model
    
    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) ?? ""
                    
                    let query = Query(question: chatText, answer: answer)
                    DispatchQueue.main.async {
                        model.queries.append(query)
                    }
                    
                    do {
                        try model.saveQuery(query)
                    } catch {
                        print(error.localizedDescription)
                    }
                  
                    chatText = ""
                    
                case .failure(let failure):
                    print(failure)
            }
        }
    }
Screenshot 2023-09-13 at 3 16 58 PM

a-elnajjar avatar Sep 14 '23 03:09 a-elnajjar

i have the same issue

kevwpl avatar Oct 20 '23 18:10 kevwpl

I have fixed the issue by adding $5 to my OpenAI account. It began working in about 5 minutes.

AnthonyBY avatar Oct 23 '23 20:10 AnthonyBY

Put money to fix it that is great for free API

a-elnajjar avatar Oct 23 '23 20:10 a-elnajjar