I am currently designing a prototype chatbot for a school project, but I was confused as to why the API call was lagging so much. It takes a minimum of 2 minutes to respond. Here is my code below.
import Foundation
import SwiftUI
import GoogleGenerativeAI
enum ChatRole {
case user
case model
}
struct ChatMessage: Identifiable, Equatable {
let id = UUID().uuidString
var role: ChatRole
var message: String
}
class ChatService: ObservableObject {
@Published private var chat: Chat?
@Published private(set) var messages = [ChatMessage]()
@Published private(set) var loadingResponse = false
func sendMessage(_ message: String) {
loadingResponse = true
if (chat == nil) {
let history: [ModelContent] = messages.map { ModelContent(role: $0.role == .user ? "user" : "model", parts: $0.message)}
chat = GenerativeModel(name: "gemini-2.0-flash-lite-preview-02-05", apiKey: APIKey.default, systemInstruction: "You are a financial AI assistant. You are called Assist, and is supposed to do nothing other than answer questions.").startChat(history: history)
}
// MARK: Add user's message to the list
messages.append(.init(role: .user, message: message))
Task {
do {
let response = try await chat?.sendMessage(message)
loadingResponse = false
guard let text = response?.text else {
messages.append(.init(role: .model, message: "Something went wrong, please try again."))
return
}
messages.append(.init(role: .model, message: text))
}
catch {
loadingResponse = false
messages.append(.init(role: .model, message: "Something went wrong, please try again."))
}
}
}
}
I’ve tried to give it specific instructions, changed models, but nothing is working. I’ve also tried it on different devices and WiFi networks for no avail. Is it my code or is it a API problem?