@@ -20,70 +20,81 @@ import Foundation
20
20
@_implementationOnly import FirebaseCoreExtension
21
21
22
22
@available ( iOS 15 . 0 , macOS 11 . 0 , macCatalyst 15 . 0 , * )
23
- @objc ( FIRVertexAI)
24
- open class VertexAI : NSObject {
23
+ public class VertexAI : NSObject {
25
24
// MARK: - Public APIs
26
25
27
- /// Returns an instance of `GoogleGenerativeAI.GenerativeModel` that uses the Vertex AI API .
26
+ /// The default `VertexAI` instance .
28
27
///
29
- /// This instance is configured with the default `FirebaseApp`.
30
- ///
31
- /// TODO: Add RequestOptions to public API.
32
- public static func generativeModel( modelName: String , location: String ) -> GenerativeModel {
28
+ /// - Returns: An instance of `VertexAI`, configured with the default `FirebaseApp`.
29
+ public static func vertexAI( ) -> VertexAI {
33
30
guard let app = FirebaseApp . app ( ) else {
34
31
fatalError ( " No instance of the default Firebase app was found. " )
35
32
}
36
- return generativeModel ( app: app, modelName: modelName, location: location)
33
+
34
+ return vertexAI ( app: app)
37
35
}
38
36
39
- /// Returns an instance of `GoogleGenerativeAI.GenerativeModel` that uses the Vertex AI API .
37
+ /// Creates an instance of `VertexAI` configured with a custom `FirebaseApp` .
40
38
///
41
- /// TODO: Add RequestOptions to public API .
42
- public static func generativeModel ( app : FirebaseApp , modelName : String ,
43
- location : String ) -> GenerativeModel {
39
+ /// - Parameter app: The custom `FirebaseApp` used for initialization .
40
+ /// - Returns: A `VertexAI` instance, configured with the custom `FirebaseApp`.
41
+ public static func vertexAI ( app : FirebaseApp ) -> VertexAI {
44
42
guard let provider = ComponentType< VertexAIProvider> . instance( for: VertexAIProvider . self,
45
43
in: app. container) else {
46
44
fatalError ( " No \( VertexAIProvider . self) instance found for Firebase app: \( app. name) " )
47
45
}
48
- let modelResourceName = modelResourceName ( app: app, modelName: modelName, location: location)
49
- let vertexAI = provider. vertexAI ( location: location, modelResourceName: modelResourceName)
50
46
51
- return vertexAI . model
47
+ return provider . vertexAI ( )
52
48
}
53
49
54
- // MARK: - Private
55
-
56
- /// The `FirebaseApp` associated with this `VertexAI` instance.
57
- private let app : FirebaseApp
58
-
59
- private let appCheck : AppCheckInterop ?
60
-
61
- private let location : String
62
-
63
- private let modelResouceName : String
50
+ /// Initializes a generative model with the given parameters.
51
+ ///
52
+ /// - Parameters:
53
+ /// - modelName: The name of the model to use, e.g., `"gemini-1.0-pro"`; see
54
+ /// [Gemini
55
+ /// models](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models#gemini-models)
56
+ /// 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.
61
+ /// - generationConfig: The content generation parameters your model should use.
62
+ /// - safetySettings: A value describing what types of harmful content your model should allow.
63
+ /// - requestOptions: Configuration parameters for sending requests to the backend.
64
+ public func generativeModel( modelName: String , location: String ,
65
+ generationConfig: GenerationConfig ? = nil ,
66
+ safetySettings: [ SafetySetting ] ? = nil ,
67
+ requestOptions: RequestOptions = RequestOptions ( ) )
68
+ -> GenerativeModel {
69
+ let modelResourceName = modelResourceName ( modelName: modelName, location: location)
64
70
65
- lazy var model : GenerativeModel = {
66
71
guard let apiKey = app. options. apiKey else {
67
72
fatalError ( " The Firebase app named \" \( app. name) \" has no API key in its configuration. " )
68
73
}
74
+
69
75
return GenerativeModel (
70
- name: modelResouceName ,
76
+ name: modelResourceName ,
71
77
apiKey: apiKey,
72
- // TODO: Add RequestOptions to public API.
73
- requestOptions: RequestOptions ( ) ,
78
+ generationConfig: generationConfig,
79
+ safetySettings: safetySettings,
80
+ requestOptions: requestOptions,
74
81
appCheck: appCheck
75
82
)
76
- } ( )
83
+ }
84
+
85
+ // MARK: - Private
86
+
87
+ /// The `FirebaseApp` associated with this `VertexAI` instance.
88
+ private let app : FirebaseApp
89
+
90
+ private let appCheck : AppCheckInterop ?
77
91
78
- init ( app: FirebaseApp , location : String , modelResourceName : String ) {
92
+ init ( app: FirebaseApp ) {
79
93
self . app = app
80
94
appCheck = ComponentType< AppCheckInterop> . instance( for: AppCheckInterop . self, in: app. container)
81
- self . location = location
82
- modelResouceName = modelResourceName
83
95
}
84
96
85
- private static func modelResourceName( app: FirebaseApp , modelName: String ,
86
- location: String ) -> String {
97
+ private func modelResourceName( modelName: String , location: String ) -> String {
87
98
if modelName. contains ( " / " ) {
88
99
return modelName
89
100
}
0 commit comments