Skip to content

Commit

Permalink
Prototype and sample fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard committed Mar 15, 2024
1 parent 8308b96 commit 7598e89
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
19 changes: 2 additions & 17 deletions FirebaseVertexAI/Sample/ChatSample/Views/ErrorDetailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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).
"""
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 18 additions & 6 deletions FirebaseVertexAI/Sources/VertexAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
)
}()
Expand All @@ -86,23 +92,29 @@ 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)"
}

// 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
}

Expand Down

0 comments on commit 7598e89

Please sign in to comment.