GenAI.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 FirebaseAuthInterop
  16. import FirebaseCore
  17. import Foundation
  18. // Avoids exposing internal FirebaseCore APIs to Swift users.
  19. internal import FirebaseCoreExtension
  20. /// The Firebase AI SDK provides access to Gemini models directly from your app.
  21. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  22. public final class FirebaseAI: VertexAIProtocol, Sendable {
  23. // MARK: - Public APIs
  24. /// Creates an instance of `VertexAI`.
  25. ///
  26. /// - Parameters:
  27. /// - app: A custom `FirebaseApp` used for initialization; if not specified, uses the default
  28. /// ``FirebaseApp``.
  29. /// - location: The region identifier, defaulting to `us-central1`; see
  30. /// [Vertex AI locations]
  31. /// (https://firebase.google.com/docs/vertex-ai/locations?platform=ios#available-locations)
  32. /// for a list of supported locations.
  33. /// - Returns: A `VertexAI` instance, configured with the custom `FirebaseApp`.
  34. public static func vertexAI(app: FirebaseApp? = nil,
  35. location: String = "us-central1") -> FirebaseAI {
  36. let vertexInstance = cachedInstance(
  37. app: app,
  38. location: location,
  39. apiConfig: defaultVertexAIAPIConfig
  40. )
  41. // Verify that the `VertexAI` instance is always configured with the production endpoint since
  42. // this is the public API surface for creating an instance.
  43. assert(vertexInstance.apiConfig.service == .vertexAI(endpoint: .firebaseVertexAIProd))
  44. assert(vertexInstance.apiConfig.service.endpoint == .firebaseVertexAIProd)
  45. assert(vertexInstance.apiConfig.version == .v1beta)
  46. return vertexInstance
  47. }
  48. /// Initializes a generative model with the given parameters.
  49. ///
  50. /// - Note: Refer to [Gemini models](https://firebase.google.com/docs/vertex-ai/gemini-models) for
  51. /// guidance on choosing an appropriate model for your use case.
  52. ///
  53. /// - Parameters:
  54. /// - modelName: The name of the model to use, for example `"gemini-1.5-flash"`; see
  55. /// [available model names
  56. /// ](https://firebase.google.com/docs/vertex-ai/gemini-models#available-model-names) for a
  57. /// list of supported model names.
  58. /// - generationConfig: The content generation parameters your model should use.
  59. /// - safetySettings: A value describing what types of harmful content your model should allow.
  60. /// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
  61. /// - toolConfig: Tool configuration for any `Tool` specified in the request.
  62. /// - systemInstruction: Instructions that direct the model to behave a certain way; currently
  63. /// only text content is supported.
  64. /// - requestOptions: Configuration parameters for sending requests to the backend.
  65. public func generativeModel(modelName: String,
  66. generationConfig: GenerationConfig? = nil,
  67. safetySettings: [SafetySetting]? = nil,
  68. tools: [Tool]? = nil,
  69. toolConfig: ToolConfig? = nil,
  70. systemInstruction: ModelContent? = nil,
  71. requestOptions: RequestOptions = RequestOptions())
  72. -> GenerativeModel {
  73. if !modelName.starts(with: GenerativeModel.geminiModelNamePrefix) {
  74. VertexLog.warning(code: .unsupportedGeminiModel, """
  75. Unsupported Gemini model "\(modelName)"; see \
  76. https://firebase.google.com/docs/vertex-ai/models for a list supported Gemini model names.
  77. """)
  78. }
  79. return GenerativeModel(
  80. modelName: modelName,
  81. modelResourceName: modelResourceName(modelName: modelName),
  82. firebaseInfo: firebaseInfo,
  83. apiConfig: apiConfig,
  84. generationConfig: generationConfig,
  85. safetySettings: safetySettings,
  86. tools: tools,
  87. toolConfig: toolConfig,
  88. systemInstruction: systemInstruction,
  89. requestOptions: requestOptions
  90. )
  91. }
  92. /// **[Public Preview]** Initializes an ``ImagenModel`` with the given parameters.
  93. ///
  94. /// > Warning: For Vertex AI in Firebase, image generation using Imagen 3 models is in Public
  95. /// Preview, which means that the feature is not subject to any SLA or deprecation policy and
  96. /// could change in backwards-incompatible ways.
  97. ///
  98. /// > Important: Only Imagen 3 models (named `imagen-3.0-*`) are supported.
  99. ///
  100. /// - Parameters:
  101. /// - modelName: The name of the Imagen 3 model to use, for example `"imagen-3.0-generate-002"`;
  102. /// see [model versions](https://firebase.google.com/docs/vertex-ai/models) for a list of
  103. /// supported Imagen 3 models.
  104. /// - generationConfig: Configuration options for generating images with Imagen.
  105. /// - safetySettings: Settings describing what types of potentially harmful content your model
  106. /// should allow.
  107. /// - requestOptions: Configuration parameters for sending requests to the backend.
  108. public func imagenModel(modelName: String, generationConfig: ImagenGenerationConfig? = nil,
  109. safetySettings: ImagenSafetySettings? = nil,
  110. requestOptions: RequestOptions = RequestOptions()) -> ImagenModel {
  111. if !modelName.starts(with: ImagenModel.imagenModelNamePrefix) {
  112. VertexLog.warning(code: .unsupportedImagenModel, """
  113. Unsupported Imagen model "\(modelName)"; see \
  114. https://firebase.google.com/docs/vertex-ai/models for a list supported Imagen model names.
  115. """)
  116. }
  117. return ImagenModel(
  118. modelResourceName: modelResourceName(modelName: modelName),
  119. firebaseInfo: firebaseInfo,
  120. apiConfig: apiConfig,
  121. generationConfig: generationConfig,
  122. safetySettings: safetySettings,
  123. requestOptions: requestOptions
  124. )
  125. }
  126. /// Class to enable VertexAI to register via the Objective-C based Firebase component system
  127. /// to include VertexAI in the userAgent.
  128. @objc(FIRVertexAIComponent) class FirebaseVertexAIComponent: NSObject {}
  129. // MARK: - Private
  130. /// Firebase data relevant to Vertex AI.
  131. let firebaseInfo: FirebaseInfo
  132. let apiConfig: APIConfig
  133. #if compiler(>=6)
  134. /// A map of active `VertexAI` instances keyed by the `FirebaseApp` name and the `location`, in
  135. /// the format `appName:location`.
  136. private nonisolated(unsafe) static var instances: [InstanceKey: FirebaseAI] = [:]
  137. /// Lock to manage access to the `instances` array to avoid race conditions.
  138. private nonisolated(unsafe) static var instancesLock: os_unfair_lock = .init()
  139. #else
  140. /// A map of active `VertexAI` instances keyed by the `FirebaseApp` name and the `location`, in
  141. /// the format `appName:location`.
  142. private static var instances: [InstanceKey: VertexAI] = [:]
  143. /// Lock to manage access to the `instances` array to avoid race conditions.
  144. private static var instancesLock: os_unfair_lock = .init()
  145. #endif
  146. let location: String?
  147. static let defaultVertexAIAPIConfig = APIConfig(
  148. service: .vertexAI(endpoint: .firebaseVertexAIProd),
  149. version: .v1beta
  150. )
  151. static func cachedInstance(app: FirebaseApp?, location: String?,
  152. apiConfig: APIConfig) -> FirebaseAI {
  153. guard let app = app ?? FirebaseApp.app() else {
  154. fatalError("No instance of the default Firebase app was found.")
  155. }
  156. os_unfair_lock_lock(&instancesLock)
  157. // Unlock before the function returns.
  158. defer { os_unfair_lock_unlock(&instancesLock) }
  159. let instanceKey = InstanceKey(appName: app.name, location: location, apiConfig: apiConfig)
  160. if let instance = instances[instanceKey] {
  161. return instance
  162. }
  163. let newInstance = FirebaseAI(app: app, location: location, apiConfig: apiConfig)
  164. instances[instanceKey] = newInstance
  165. return newInstance
  166. }
  167. init(app: FirebaseApp, location: String?, apiConfig: APIConfig) {
  168. guard let projectID = app.options.projectID else {
  169. fatalError("The Firebase app named \"\(app.name)\" has no project ID in its configuration.")
  170. }
  171. guard let apiKey = app.options.apiKey else {
  172. fatalError("The Firebase app named \"\(app.name)\" has no API key in its configuration.")
  173. }
  174. firebaseInfo = FirebaseInfo(
  175. appCheck: ComponentType<AppCheckInterop>.instance(
  176. for: AppCheckInterop.self,
  177. in: app.container
  178. ),
  179. auth: ComponentType<AuthInterop>.instance(for: AuthInterop.self, in: app.container),
  180. projectID: projectID,
  181. apiKey: apiKey,
  182. firebaseAppID: app.options.googleAppID,
  183. firebaseApp: app
  184. )
  185. self.apiConfig = apiConfig
  186. self.location = location
  187. }
  188. func modelResourceName(modelName: String) -> String {
  189. guard !modelName.isEmpty && modelName
  190. .allSatisfy({ !$0.isWhitespace && !$0.isNewline && $0 != "/" }) else {
  191. fatalError("""
  192. Invalid model name "\(modelName)" specified; see \
  193. https://firebase.google.com/docs/vertex-ai/gemini-model#available-models for a list of \
  194. available models.
  195. """)
  196. }
  197. switch apiConfig.service {
  198. case .vertexAI:
  199. return vertexAIModelResourceName(modelName: modelName)
  200. case .developer:
  201. return developerModelResourceName(modelName: modelName)
  202. }
  203. }
  204. private func vertexAIModelResourceName(modelName: String) -> String {
  205. guard let location else {
  206. fatalError("Location must be specified for the Vertex AI service.")
  207. }
  208. guard !location.isEmpty && location
  209. .allSatisfy({ !$0.isWhitespace && !$0.isNewline && $0 != "/" }) else {
  210. fatalError("""
  211. Invalid location "\(location)" specified; see \
  212. https://firebase.google.com/docs/vertex-ai/locations?platform=ios#available-locations \
  213. for a list of available locations.
  214. """)
  215. }
  216. let projectID = firebaseInfo.projectID
  217. return "projects/\(projectID)/locations/\(location)/publishers/google/models/\(modelName)"
  218. }
  219. private func developerModelResourceName(modelName: String) -> String {
  220. switch apiConfig.service.endpoint {
  221. case .firebaseVertexAIStaging, .firebaseVertexAIProd:
  222. let projectID = firebaseInfo.projectID
  223. return "projects/\(projectID)/models/\(modelName)"
  224. case .generativeLanguage:
  225. return "models/\(modelName)"
  226. }
  227. }
  228. /// Identifier for a unique instance of ``VertexAI``.
  229. ///
  230. /// This type is `Hashable` so that it can be used as a key in the `instances` dictionary.
  231. private struct InstanceKey: Sendable, Hashable {
  232. let appName: String
  233. let location: String?
  234. let apiConfig: APIConfig
  235. }
  236. }