Skip to content

no response from openAI webAPI? #119

@a-elnajjar

Description

@a-elnajjar

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions