VertexAI.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseAppCheckInterop
  15. import FirebaseCore
  16. import Foundation
  17. // Avoids exposing internal FirebaseCore APIs to Swift users.
  18. @_implementationOnly import FirebaseCoreExtension
  19. /// The Vertex AI service for Firebase.
  20. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
  21. public class VertexAI: NSObject {
  22. // MARK: - Public APIs
  23. /// The default `VertexAI` instance.
  24. ///
  25. /// - Returns: An instance of `VertexAI`, configured with the default `FirebaseApp`.
  26. public static func vertexAI() -> VertexAI {
  27. guard let app = FirebaseApp.app() else {
  28. fatalError("No instance of the default Firebase app was found.")
  29. }
  30. return vertexAI(app: app)
  31. }
  32. /// Creates an instance of `VertexAI` configured with a custom `FirebaseApp`.
  33. ///
  34. /// - Parameters:
  35. /// - app: The custom `FirebaseApp` used for initialization.
  36. /// - Returns: A `VertexAI` instance, configured with the custom `FirebaseApp`.
  37. public static func vertexAI(app: FirebaseApp) -> VertexAI {
  38. guard let provider = ComponentType<VertexAIProvider>.instance(for: VertexAIProvider.self,
  39. in: app.container) else {
  40. fatalError("No \(VertexAIProvider.self) instance found for Firebase app: \(app.name)")
  41. }
  42. return provider.vertexAI("us-central1")
  43. }
  44. /// Initializes a generative model with the given parameters.
  45. ///
  46. /// - Parameters:
  47. /// - modelName: The name of the model to use, for example `"gemini-1.0-pro"`; see
  48. /// [Gemini models](https://firebase.google.com/docs/vertex-ai/gemini-model#available-models)
  49. /// for a list of supported model names.
  50. /// - generationConfig: The content generation parameters your model should use.
  51. /// - safetySettings: A value describing what types of harmful content your model should allow.
  52. /// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
  53. /// - toolConfig: Tool configuration for any `Tool` specified in the request.
  54. /// - systemInstruction: Instructions that direct the model to behave a certain way; currently
  55. /// only text content is supported.
  56. /// - requestOptions: Configuration parameters for sending requests to the backend.
  57. public func generativeModel(modelName: String,
  58. generationConfig: GenerationConfig? = nil,
  59. safetySettings: [SafetySetting]? = nil,
  60. tools: [Tool]? = nil,
  61. toolConfig: ToolConfig? = nil,
  62. systemInstruction: ModelContent? = nil,
  63. requestOptions: RequestOptions = RequestOptions())
  64. -> GenerativeModel {
  65. let modelResourceName = modelResourceName(modelName: modelName, location: location)
  66. guard let apiKey = app.options.apiKey else {
  67. fatalError("The Firebase app named \"\(app.name)\" has no API key in its configuration.")
  68. }
  69. return GenerativeModel(
  70. name: modelResourceName,
  71. apiKey: apiKey,
  72. generationConfig: generationConfig,
  73. safetySettings: safetySettings,
  74. tools: tools,
  75. toolConfig: toolConfig,
  76. systemInstruction: systemInstruction,
  77. requestOptions: requestOptions,
  78. appCheck: appCheck
  79. )
  80. }
  81. // MARK: - Private
  82. /// The `FirebaseApp` associated with this `VertexAI` instance.
  83. private let app: FirebaseApp
  84. private let appCheck: AppCheckInterop?
  85. let location: String
  86. init(app: FirebaseApp, location: String) {
  87. self.app = app
  88. self.location = location
  89. appCheck = ComponentType<AppCheckInterop>.instance(for: AppCheckInterop.self, in: app.container)
  90. }
  91. private func modelResourceName(modelName: String, location: String) -> String {
  92. if modelName.contains("/") {
  93. return modelName
  94. }
  95. guard let projectID = app.options.projectID else {
  96. fatalError("The Firebase app named \"\(app.name)\" has no project ID in its configuration.")
  97. }
  98. guard !location.isEmpty else {
  99. fatalError("""
  100. No location specified; see
  101. https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations#available-regions for a
  102. list of available regions.
  103. """)
  104. }
  105. return "projects/\(projectID)/locations/\(location)/publishers/google/models/\(modelName)"
  106. }
  107. }