Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename the Vertex AI parameter region to location #12656

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ConversationViewModel: ObservableObject {
private var chatTask: Task<Void, Never>?

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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 14 additions & 14 deletions FirebaseVertexAI/Sources/VertexAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<VertexAIProvider>.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.
Expand All @@ -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.")
Expand All @@ -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<AppCheckInterop>.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)"
}
}
10 changes: 5 additions & 5 deletions FirebaseVertexAI/Sources/VertexAIComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, *)
Expand Down Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions FirebaseVertexAI/Tests/Unit/VertexAIAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
10 changes: 5 additions & 5 deletions FirebaseVertexAI/Tests/Unit/VertexComponentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
Loading