3.2 C
New York
Wednesday, December 4, 2024

ios – Getting an error with OpenAI integration into my Swift app


I’m attempting to make an app to supply a trivia query to the customers utilizing high 5 medical journal abstracts and the query can be generated utilizing openAI gpt 3.5 turbo. I maintain getting an error within the assertion:

let chatQuery = ChatQuery(messages: [message], mannequin: "gpt-3.5-turbo") 

which says

Can not convert worth of sort ‘ChatQuery.ChatCompletionMessageParam?’ to anticipated factor sort ‘Array.ArrayLiteralElement’ (aka ‘ChatQuery.ChatCompletionMessageParam’)

Thanks upfront!

Right here is the complete code:

class PubMedViewModel: ObservableObject {
    @Printed var articles: [Article] = []
    @Printed var triviaScore: Int = 0
    non-public var currentTrivia: TriviaQuestion?
    non-public let openAI: OpenAI

    init() {
        self.openAI = OpenAI(apiToken: "YOUR_OPENAI_API_KEY")
        loadTriviaScore()
        generateDailyTrivia()
    }

    func generateDailyTrivia() {
        guard articles.rely >= 5 else { return }
        
        let abstracts = articles.prefix(5).map { $0.summary }.joined(separator: " ")
        let immediate = "Create a trivia query with 4 choices from the next abstracts: (abstracts)"
        
        Activity {
            do {
                let message = ChatQuery.ChatCompletionMessageParam(function: .person, content material: immediate)
                let chatQuery = ChatQuery(messages: [message], mannequin: "gpt-3.5-turbo")
                let outcome = strive await openAI.chats(question: chatQuery)
                
                if let triviaContent = outcome.selections.first?.message.content material {
                    // Parse triviaContent to create a TriviaQuestion
                    // Instance parsing logic:
                    let query = "What's the primary subject?"
                    let choices = ["Option 1", "Option 2", "Option 3", "Option 4"]
                    let correctAnswer = "Choice 1"
                    
                    currentTrivia = TriviaQuestion(query: query, choices: choices, correctAnswer: correctAnswer)
                }
            } catch {
                print("Error producing trivia: (error)")
            }
        }
    }

    func answerTrivia(with reply: String) {
        if let trivia = currentTrivia, reply == trivia.correctAnswer {
            triviaScore += 1
            saveTriviaScore()
        }
    }

    non-public func saveTriviaScore() {
        UserDefaults.normal.set(triviaScore, forKey: "triviaScore")
    }

    non-public func loadTriviaScore() {
        triviaScore = UserDefaults.normal.integer(forKey: "triviaScore")
    }
}

I attempted utilizing AI itself to assist me reply but it surely retains going spherical suggesting “Guarantee Non-Non-compulsory Initialization: Make certain the ChatQuery.ChatCompletionMessageParam is initialized straight with out optionals.” which I did however didn’t repair the issue

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles