Skip to content

Instantly share code, notes, and snippets.

@apatronl
Created May 1, 2024 14:06
Show Gist options
  • Save apatronl/e219f5ac899bc4086eef2d97b8cecde2 to your computer and use it in GitHub Desktop.
Save apatronl/e219f5ac899bc4086eef2d97b8cecde2 to your computer and use it in GitHub Desktop.
import Foundation
import GoogleGenerativeAI
class GeminiClient {
private lazy var model = GenerativeModel(name: "gemini-pro", apiKey: APIKey.default)
private lazy var chat: Chat = model.startChat()
func send(message: String) async -> Result<String, GenerateContentError> {
do {
let response = try await chat.sendMessage(message)
if let text = response.text {
return .success(text)
}
} catch let error as GenerateContentError {
return .failure(error)
} catch {
return .failure(GenerateContentError.internalError(underlying: GenericError()))
}
return .failure(GenerateContentError.internalError(underlying: GenericError()))
}
}
struct GenericError: Error {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment