From 7598e89d2bb48fb8a7faee7395d6e7d48b44dedd Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 4 Mar 2024 16:50:37 -0500 Subject: [PATCH] Prototype and sample fixes --- .../ChatSample/Views/ErrorDetailsView.swift | 19 ++------------- .../ViewModels/PhotoReasoningViewModel.swift | 2 +- FirebaseVertexAI/Sources/VertexAI.swift | 24 ++++++++++++++----- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/FirebaseVertexAI/Sample/ChatSample/Views/ErrorDetailsView.swift b/FirebaseVertexAI/Sample/ChatSample/Views/ErrorDetailsView.swift index e601876b2d9..399777ad1e8 100644 --- a/FirebaseVertexAI/Sample/ChatSample/Views/ErrorDetailsView.swift +++ b/FirebaseVertexAI/Sample/ChatSample/Views/ErrorDetailsView.swift @@ -152,23 +152,8 @@ struct ErrorDetailsView: View { SubtitleMarkdownFormRow( title: "Help", value: """ - Please provide a valid value for `API_KEY` in the `GenerativeAI-Info.plist` file. - """ - ) - } - - case GenerateContentError.unsupportedUserLocation: - Section("Error Type") { - Text("Unsupported User Location") - } - - Section("Details") { - SubtitleFormRow(title: "Error description", value: error.localizedDescription) - SubtitleMarkdownFormRow( - title: "Help", - value: """ - The API is unsupported in your location (country / territory); please see the list of - [available regions](https://ai.google.dev/available_regions#available_regions). + The `API_KEY` provided in the `GoogleService-Info.plist` file is invalid. Download a + new copy of the file from the [Firebase Console](https://console.firebase.google.com). """ ) } diff --git a/FirebaseVertexAI/Sample/GenerativeAIMultimodalSample/ViewModels/PhotoReasoningViewModel.swift b/FirebaseVertexAI/Sample/GenerativeAIMultimodalSample/ViewModels/PhotoReasoningViewModel.swift index aa646c127df..dc41e00444a 100644 --- a/FirebaseVertexAI/Sample/GenerativeAIMultimodalSample/ViewModels/PhotoReasoningViewModel.swift +++ b/FirebaseVertexAI/Sample/GenerativeAIMultimodalSample/ViewModels/PhotoReasoningViewModel.swift @@ -62,7 +62,7 @@ class PhotoReasoningViewModel: ObservableObject { let prompt = "Look at the image(s), and then answer the following question: \(userInput)" - var images = [PartsRepresentable]() + var images = [any ThrowingPartsRepresentable]() for item in selectedItems { if let data = try? await item.loadTransferable(type: Data.self) { guard let image = UIImage(data: data) else { diff --git a/FirebaseVertexAI/Sources/VertexAI.swift b/FirebaseVertexAI/Sources/VertexAI.swift index 9d1b8b4016c..8fb0f9e8c68 100644 --- a/FirebaseVertexAI/Sources/VertexAI.swift +++ b/FirebaseVertexAI/Sources/VertexAI.swift @@ -33,7 +33,10 @@ open class VertexAI: NSObject { /// This instance is configured with the default `FirebaseApp`. public static func generativeModel(modelName: String, location: String) -> GoogleGenerativeAI .GenerativeModel { - return generativeModel(app: FirebaseApp.app()!, modelName: modelName, location: location) + guard let app = FirebaseApp.app() else { + fatalError("No instance of the default Firebase app was found.") + } + return generativeModel(app: app, modelName: modelName, location: location) } /// Returns an instance of `GoogleGenerativeAI.GenerativeModel` that uses the Vertex AI API. @@ -66,9 +69,12 @@ open class VertexAI: NSObject { endpoint: "staging-firebaseml.sandbox.googleapis.com", hooks: [addAppCheckHeader] ) + guard let apiKey = app.options.apiKey else { + fatalError("The Firebase app named \"\(app.name)\" has no API key in its configuration.") + } return GenerativeModel( name: modelResouceName, - apiKey: app.options.apiKey!, + apiKey: apiKey, requestOptions: options ) }() @@ -86,8 +92,14 @@ open class VertexAI: NSObject { return modelName } guard let projectID = app.options.projectID else { - print("The FirebaseApp is missing a project ID.") - return modelName + fatalError("The Firebase app named \"\(app.name)\" has no project ID in its configuration.") + } + guard !location.isEmpty else { + fatalError(""" + No location specified; see + https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations#available-regions for a + list of available regions. + """) } return "projects/\(projectID)/locations/\(location)/publishers/google/models/\(modelName)" @@ -95,14 +107,14 @@ open class VertexAI: NSObject { // MARK: Request Hooks - /// Adds an App Check token to the provided request, if App Check is included in the app. + /// Adds an App Check token to the provided request if App Check is included in the app. /// /// This demonstrates how an App Check token can be added to requests; it is currently ignored by /// the backend. /// /// - Parameter request: The `URLRequest` to modify by adding an App Check token header. func addAppCheckHeader(request: inout URLRequest) async { - guard let appCheck = appCheck else { + guard let appCheck else { return }