Skip to content

Commit 0f1fdc8

Browse files
paulb777andrewheard
authored andcommitted
Manage location on VertexAI instead of model (#12630)
1 parent fdc2985 commit 0f1fdc8

File tree

6 files changed

+33
-37
lines changed

6 files changed

+33
-37
lines changed

FirebaseVertexAI/Sample/ChatSample/ViewModels/ConversationViewModel.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ class ConversationViewModel: ObservableObject {
3636
private var chatTask: Task<Void, Never>?
3737

3838
init() {
39-
model = VertexAI.vertexAI().generativeModel(
40-
modelName: "gemini-1.0-pro",
41-
location: "us-central1"
42-
)
39+
model = VertexAI.vertexAI(region: "us-central1").generativeModel(modelName: "gemini-1.0-pro")
4340
chat = model.startChat()
4441
}
4542

FirebaseVertexAI/Sample/GenerativeAIMultimodalSample/ViewModels/PhotoReasoningViewModel.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ class PhotoReasoningViewModel: ObservableObject {
4444
private var model: GenerativeModel?
4545

4646
init() {
47-
model = VertexAI.vertexAI().generativeModel(
48-
modelName: "gemini-1.0-pro-vision",
49-
location: "us-central1"
50-
)
47+
model = VertexAI.vertexAI(region: "us-central1").generativeModel(modelName: "gemini-1.0-pro")
5148
}
5249

5350
func reason() async {

FirebaseVertexAI/Sample/GenerativeAITextSample/ViewModels/SummarizeViewModel.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ class SummarizeViewModel: ObservableObject {
3232
private var model: GenerativeModel?
3333

3434
init() {
35-
model = VertexAI.vertexAI().generativeModel(
36-
modelName: "gemini-1.0-pro",
37-
location: "us-central1"
38-
)
35+
model = VertexAI.vertexAI(region: "us-central1").generativeModel(modelName: "gemini-1.0-pro")
3936
}
4037

4138
func summarize(inputText: String) async {

FirebaseVertexAI/Sources/VertexAI.swift

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,35 @@ public class VertexAI: NSObject {
2525

2626
/// The default `VertexAI` instance.
2727
///
28+
/// - Parameter region: The region identifier, e.g., `us-central1`; see
29+
/// [Vertex AI
30+
/// regions](https://cloud.google.com/vertex-ai/docs/general/locations#vertex-ai-regions)
31+
/// for a list of supported regions.
2832
/// - Returns: An instance of `VertexAI`, configured with the default `FirebaseApp`.
29-
public static func vertexAI() -> VertexAI {
33+
public static func vertexAI(region: String) -> VertexAI {
3034
guard let app = FirebaseApp.app() else {
3135
fatalError("No instance of the default Firebase app was found.")
3236
}
3337

34-
return vertexAI(app: app)
38+
return vertexAI(app: app, region: region)
3539
}
3640

3741
/// Creates an instance of `VertexAI` configured with a custom `FirebaseApp`.
3842
///
39-
/// - Parameter app: The custom `FirebaseApp` used for initialization.
43+
/// - Parameters:
44+
/// - app: The custom `FirebaseApp` used for initialization.
45+
/// - region: The region identifier, e.g., `us-central1`; see
46+
/// [Vertex AI
47+
/// regions](https://cloud.google.com/vertex-ai/docs/general/locations#vertex-ai-regions)
48+
/// for a list of supported regions.
4049
/// - Returns: A `VertexAI` instance, configured with the custom `FirebaseApp`.
41-
public static func vertexAI(app: FirebaseApp) -> VertexAI {
50+
public static func vertexAI(app: FirebaseApp, region: String) -> VertexAI {
4251
guard let provider = ComponentType<VertexAIProvider>.instance(for: VertexAIProvider.self,
4352
in: app.container) else {
4453
fatalError("No \(VertexAIProvider.self) instance found for Firebase app: \(app.name)")
4554
}
4655

47-
return provider.vertexAI()
56+
return provider.vertexAI(region)
4857
}
4958

5059
/// Initializes a generative model with the given parameters.
@@ -54,19 +63,15 @@ public class VertexAI: NSObject {
5463
/// [Gemini
5564
/// models](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models#gemini-models)
5665
/// for a list of supported model names.
57-
/// - location: The location identifier, e.g., `us-central1`; see
58-
/// [Vertex AI
59-
/// regions](https://cloud.google.com/vertex-ai/docs/general/locations#vertex-ai-regions)
60-
/// for a list of supported locations.
6166
/// - generationConfig: The content generation parameters your model should use.
6267
/// - safetySettings: A value describing what types of harmful content your model should allow.
6368
/// - requestOptions: Configuration parameters for sending requests to the backend.
64-
public func generativeModel(modelName: String, location: String,
69+
public func generativeModel(modelName: String,
6570
generationConfig: GenerationConfig? = nil,
6671
safetySettings: [SafetySetting]? = nil,
6772
requestOptions: RequestOptions = RequestOptions())
6873
-> GenerativeModel {
69-
let modelResourceName = modelResourceName(modelName: modelName, location: location)
74+
let modelResourceName = modelResourceName(modelName: modelName, region: region)
7075

7176
guard let apiKey = app.options.apiKey else {
7277
fatalError("The Firebase app named \"\(app.name)\" has no API key in its configuration.")
@@ -89,26 +94,29 @@ public class VertexAI: NSObject {
8994

9095
private let appCheck: AppCheckInterop?
9196

92-
init(app: FirebaseApp) {
97+
private let region: String
98+
99+
init(app: FirebaseApp, region: String) {
93100
self.app = app
101+
self.region = region
94102
appCheck = ComponentType<AppCheckInterop>.instance(for: AppCheckInterop.self, in: app.container)
95103
}
96104

97-
private func modelResourceName(modelName: String, location: String) -> String {
105+
private func modelResourceName(modelName: String, region: String) -> String {
98106
if modelName.contains("/") {
99107
return modelName
100108
}
101109
guard let projectID = app.options.projectID else {
102110
fatalError("The Firebase app named \"\(app.name)\" has no project ID in its configuration.")
103111
}
104-
guard !location.isEmpty else {
112+
guard !region.isEmpty else {
105113
fatalError("""
106-
No location specified; see
114+
No region specified; see
107115
https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations#available-regions for a
108116
list of available regions.
109117
""")
110118
}
111119

112-
return "projects/\(projectID)/locations/\(location)/publishers/google/models/\(modelName)"
120+
return "projects/\(projectID)/locations/\(region)/publishers/google/models/\(modelName)"
113121
}
114122
}

FirebaseVertexAI/Sources/VertexAIComponent.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Foundation
2222
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
2323
@objc(FIRVertexAIProvider)
2424
protocol VertexAIProvider {
25-
@objc func vertexAI() -> VertexAI
25+
@objc func vertexAI(_ location: String) -> VertexAI
2626
}
2727

2828
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
@@ -64,7 +64,7 @@ class VertexAIComponent: NSObject, Library, VertexAIProvider {
6464

6565
// MARK: - VertexAIProvider conformance
6666

67-
func vertexAI() -> VertexAI {
67+
func vertexAI(_ region: String) -> VertexAI {
6868
os_unfair_lock_lock(&instancesLock)
6969

7070
// Unlock before the function returns.
@@ -73,7 +73,7 @@ class VertexAIComponent: NSObject, Library, VertexAIProvider {
7373
if let instance = instances[app.name] {
7474
return instance
7575
}
76-
let newInstance = VertexAI(app: app)
76+
let newInstance = VertexAI(app: app, region: region)
7777
instances[app.name] = newInstance
7878
return newInstance
7979
}

FirebaseVertexAI/Tests/Unit/VertexAIAPITests.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,28 @@ final class VertexAIAPITests: XCTestCase {
3434
let filters = [SafetySetting(harmCategory: .dangerousContent, threshold: .blockOnlyHigh)]
3535

3636
// Instantiate Vertex AI SDK - Default App
37-
let vertexAI = VertexAI.vertexAI()
37+
let vertexAI = VertexAI.vertexAI(region: "my-region")
3838

3939
// Instantiate Vertex AI SDK - Custom App
40-
let _ = VertexAI.vertexAI(app: app!)
40+
let _ = VertexAI.vertexAI(app: app!, region: "my-region")
4141

4242
// Permutations without optional arguments.
4343

44-
let _ = vertexAI.generativeModel(modelName: "gemini-1.0-pro", location: "us-central1")
44+
let _ = vertexAI.generativeModel(modelName: "gemini-1.0-pro")
4545

4646
let _ = vertexAI.generativeModel(
4747
modelName: "gemini-1.0-pro",
48-
location: "us-central1",
4948
safetySettings: filters
5049
)
5150

5251
let _ = vertexAI.generativeModel(
5352
modelName: "gemini-1.0-pro",
54-
location: "us-central1",
5553
generationConfig: config
5654
)
5755

5856
// All arguments passed.
5957
let genAI = vertexAI.generativeModel(
6058
modelName: "gemini-1.0-pro",
61-
location: "us-central1",
6259
generationConfig: config, // Optional
6360
safetySettings: filters // Optional
6461
)

0 commit comments

Comments
 (0)