adamrushy / OpenAISwift

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

no response from openAI webAPI? #119

Open a-elnajjar opened 9 months ago

a-elnajjar commented 9 months ago

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
kevwpl commented 8 months ago

i have the same issue

AnthonyBY commented 8 months ago

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

a-elnajjar commented 8 months ago

Put money to fix it that is great for free API