From d0d7fc708a0a5a5c9ba37e5872004613b18d9ed7 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Thu, 28 Mar 2024 17:15:10 -0400 Subject: [PATCH] Renamed the Vertex AI parameter `region` to `location` --- .../ViewModels/ConversationViewModel.swift | 2 +- .../ViewModels/PhotoReasoningViewModel.swift | 2 +- .../ViewModels/SummarizeViewModel.swift | 2 +- FirebaseVertexAI/Sources/VertexAI.swift | 28 +++++++++---------- .../Sources/VertexAIComponent.swift | 10 +++---- .../Tests/Unit/VertexAIAPITests.swift | 4 +-- .../Tests/Unit/VertexComponentTests.swift | 10 +++---- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/FirebaseVertexAI/Sample/ChatSample/ViewModels/ConversationViewModel.swift b/FirebaseVertexAI/Sample/ChatSample/ViewModels/ConversationViewModel.swift index 81667be2514..883cefb359f 100644 --- a/FirebaseVertexAI/Sample/ChatSample/ViewModels/ConversationViewModel.swift +++ b/FirebaseVertexAI/Sample/ChatSample/ViewModels/ConversationViewModel.swift @@ -36,7 +36,7 @@ class ConversationViewModel: ObservableObject { private var chatTask: Task? init() { - model = VertexAI.vertexAI(region: "us-central1").generativeModel(modelName: "gemini-1.0-pro") + model = VertexAI.vertexAI(location: "us-central1").generativeModel(modelName: "gemini-1.0-pro") chat = model.startChat() } diff --git a/FirebaseVertexAI/Sample/GenerativeAIMultimodalSample/ViewModels/PhotoReasoningViewModel.swift b/FirebaseVertexAI/Sample/GenerativeAIMultimodalSample/ViewModels/PhotoReasoningViewModel.swift index 1fb0097a808..bfa1cfa9028 100644 --- a/FirebaseVertexAI/Sample/GenerativeAIMultimodalSample/ViewModels/PhotoReasoningViewModel.swift +++ b/FirebaseVertexAI/Sample/GenerativeAIMultimodalSample/ViewModels/PhotoReasoningViewModel.swift @@ -44,7 +44,7 @@ class PhotoReasoningViewModel: ObservableObject { private var model: GenerativeModel? init() { - let vertexAI = VertexAI.vertexAI(region: "us-central1") + let vertexAI = VertexAI.vertexAI(location: "us-central1") model = vertexAI.generativeModel(modelName: "gemini-1.0-pro-vision") } diff --git a/FirebaseVertexAI/Sample/GenerativeAITextSample/ViewModels/SummarizeViewModel.swift b/FirebaseVertexAI/Sample/GenerativeAITextSample/ViewModels/SummarizeViewModel.swift index e3c78d09060..a90e1cf15b8 100644 --- a/FirebaseVertexAI/Sample/GenerativeAITextSample/ViewModels/SummarizeViewModel.swift +++ b/FirebaseVertexAI/Sample/GenerativeAITextSample/ViewModels/SummarizeViewModel.swift @@ -32,7 +32,7 @@ class SummarizeViewModel: ObservableObject { private var model: GenerativeModel? init() { - model = VertexAI.vertexAI(region: "us-central1").generativeModel(modelName: "gemini-1.0-pro") + model = VertexAI.vertexAI(location: "us-central1").generativeModel(modelName: "gemini-1.0-pro") } func summarize(inputText: String) async { diff --git a/FirebaseVertexAI/Sources/VertexAI.swift b/FirebaseVertexAI/Sources/VertexAI.swift index a0df2ab015e..3e5f5e67050 100644 --- a/FirebaseVertexAI/Sources/VertexAI.swift +++ b/FirebaseVertexAI/Sources/VertexAI.swift @@ -25,35 +25,35 @@ public class VertexAI: NSObject { /// The default `VertexAI` instance. /// - /// - Parameter region: The region identifier, e.g., `us-central1`; see + /// - Parameter location: The region identifier, e.g., `us-central1`; see /// [Vertex AI /// regions](https://cloud.google.com/vertex-ai/docs/general/locations#vertex-ai-regions) /// for a list of supported regions. /// - Returns: An instance of `VertexAI`, configured with the default `FirebaseApp`. - public static func vertexAI(region: String) -> VertexAI { + public static func vertexAI(location: String) -> VertexAI { guard let app = FirebaseApp.app() else { fatalError("No instance of the default Firebase app was found.") } - return vertexAI(app: app, region: region) + return vertexAI(app: app, location: location) } /// Creates an instance of `VertexAI` configured with a custom `FirebaseApp`. /// /// - Parameters: /// - app: The custom `FirebaseApp` used for initialization. - /// - region: The region identifier, e.g., `us-central1`; see + /// - location: The region identifier, e.g., `us-central1`; see /// [Vertex AI /// regions](https://cloud.google.com/vertex-ai/docs/general/locations#vertex-ai-regions) /// for a list of supported regions. /// - Returns: A `VertexAI` instance, configured with the custom `FirebaseApp`. - public static func vertexAI(app: FirebaseApp, region: String) -> VertexAI { + public static func vertexAI(app: FirebaseApp, location: String) -> VertexAI { guard let provider = ComponentType.instance(for: VertexAIProvider.self, in: app.container) else { fatalError("No \(VertexAIProvider.self) instance found for Firebase app: \(app.name)") } - return provider.vertexAI(region) + return provider.vertexAI(location) } /// Initializes a generative model with the given parameters. @@ -73,7 +73,7 @@ public class VertexAI: NSObject { tools: [Tool]? = nil, requestOptions: RequestOptions = RequestOptions()) -> GenerativeModel { - let modelResourceName = modelResourceName(modelName: modelName, region: region) + let modelResourceName = modelResourceName(modelName: modelName, location: location) guard let apiKey = app.options.apiKey else { fatalError("The Firebase app named \"\(app.name)\" has no API key in its configuration.") @@ -97,29 +97,29 @@ public class VertexAI: NSObject { private let appCheck: AppCheckInterop? - let region: String + let location: String - init(app: FirebaseApp, region: String) { + init(app: FirebaseApp, location: String) { self.app = app - self.region = region + self.location = location appCheck = ComponentType.instance(for: AppCheckInterop.self, in: app.container) } - private func modelResourceName(modelName: String, region: String) -> String { + private func modelResourceName(modelName: String, location: String) -> String { if modelName.contains("/") { return modelName } guard let projectID = app.options.projectID else { fatalError("The Firebase app named \"\(app.name)\" has no project ID in its configuration.") } - guard !region.isEmpty else { + guard !location.isEmpty else { fatalError(""" - No region specified; see + 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/\(region)/publishers/google/models/\(modelName)" + return "projects/\(projectID)/locations/\(location)/publishers/google/models/\(modelName)" } } diff --git a/FirebaseVertexAI/Sources/VertexAIComponent.swift b/FirebaseVertexAI/Sources/VertexAIComponent.swift index af8dbb56271..a6d487e1208 100644 --- a/FirebaseVertexAI/Sources/VertexAIComponent.swift +++ b/FirebaseVertexAI/Sources/VertexAIComponent.swift @@ -22,7 +22,7 @@ import Foundation @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *) @objc(FIRVertexAIProvider) protocol VertexAIProvider { - @objc func vertexAI(_ region: String) -> VertexAI + @objc func vertexAI(_ location: String) -> VertexAI } @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *) @@ -64,17 +64,17 @@ class VertexAIComponent: NSObject, Library, VertexAIProvider { // MARK: - VertexAIProvider conformance - func vertexAI(_ region: String) -> VertexAI { + func vertexAI(_ location: String) -> VertexAI { os_unfair_lock_lock(&instancesLock) // Unlock before the function returns. defer { os_unfair_lock_unlock(&instancesLock) } - if let instance = instances[region] { + if let instance = instances[location] { return instance } - let newInstance = VertexAI(app: app, region: region) - instances[region] = newInstance + let newInstance = VertexAI(app: app, location: location) + instances[location] = newInstance return newInstance } } diff --git a/FirebaseVertexAI/Tests/Unit/VertexAIAPITests.swift b/FirebaseVertexAI/Tests/Unit/VertexAIAPITests.swift index a4983eb8834..80236f405de 100644 --- a/FirebaseVertexAI/Tests/Unit/VertexAIAPITests.swift +++ b/FirebaseVertexAI/Tests/Unit/VertexAIAPITests.swift @@ -34,10 +34,10 @@ final class VertexAIAPITests: XCTestCase { let filters = [SafetySetting(harmCategory: .dangerousContent, threshold: .blockOnlyHigh)] // Instantiate Vertex AI SDK - Default App - let vertexAI = VertexAI.vertexAI(region: "my-region") + let vertexAI = VertexAI.vertexAI(location: "my-location") // Instantiate Vertex AI SDK - Custom App - let _ = VertexAI.vertexAI(app: app!, region: "my-region") + let _ = VertexAI.vertexAI(app: app!, location: "my-location") // Permutations without optional arguments. diff --git a/FirebaseVertexAI/Tests/Unit/VertexComponentTests.swift b/FirebaseVertexAI/Tests/Unit/VertexComponentTests.swift index e28b747b65c..20f2b15bee8 100644 --- a/FirebaseVertexAI/Tests/Unit/VertexComponentTests.swift +++ b/FirebaseVertexAI/Tests/Unit/VertexComponentTests.swift @@ -48,7 +48,7 @@ class VertexComponentTests: XCTestCase { func testVertexInstanceCreation() throws { let app = try XCTUnwrap(VertexComponentTests.app) let component = VertexAIComponent(app: app) - let vertex = component.vertexAI("my-region") + let vertex = component.vertexAI("my-location") XCTAssertNotNil(vertex) } @@ -82,14 +82,14 @@ class VertexComponentTests: XCTestCase { in: container) XCTAssertNotNil(provider) - let vertex1 = provider?.vertexAI("randomRegion") - let vertex2 = provider?.vertexAI("randomRegion") + let vertex1 = provider?.vertexAI("randomLocation") + let vertex2 = provider?.vertexAI("randomLocation") XCTAssertNotNil(vertex1) // Ensure they're the same instance. XCTAssert(vertex1 === vertex2) - let vertex3 = provider?.vertexAI("differentRegion") + let vertex3 = provider?.vertexAI("differentLocation") XCTAssertNotNil(vertex3) XCTAssert(vertex1 !== vertex3) @@ -105,7 +105,7 @@ class VertexComponentTests: XCTestCase { options.projectID = "myProjectID" let app1 = FirebaseApp(instanceWithName: "transitory app", options: options) weakApp = try XCTUnwrap(app1) - let vertex = VertexAI(app: app1, region: "transitory region") + let vertex = VertexAI(app: app1, location: "transitory location") weakVertex = vertex XCTAssertNotNil(weakVertex) }