FirebaseAI.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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: Sendable {
  23. // MARK: - Public APIs
  24. /// Creates an instance of `FirebaseAI`.
  25. ///
  26. /// - Parameters:
  27. /// - app: A custom `FirebaseApp` used for initialization; if not specified, uses the default
  28. /// ``FirebaseApp``.
  29. /// - backend: The backend API for the Firebase AI SDK; if not specified, uses the default
  30. /// ``Backend/googleAI()`` (Gemini Developer API).
  31. /// - useLimitedUseAppCheckTokens: When sending tokens to the backend, this option enables
  32. /// the usage of App Check's limited-use tokens instead of the standard cached tokens. Learn
  33. /// more about [limited-use tokens](https://firebase.google.com/docs/ai-logic/app-check),
  34. /// including their nuances, when to use them, and best practices for integrating them into
  35. /// your app.
  36. ///
  37. /// _This flag is set to `false` by default._
  38. /// > Migrating to limited-use tokens sooner minimizes disruption when support for replay
  39. /// > protection is added.
  40. /// - Returns: A `FirebaseAI` instance, configured with the custom `FirebaseApp`.
  41. public static func firebaseAI(app: FirebaseApp? = nil,
  42. backend: Backend = .googleAI(),
  43. useLimitedUseAppCheckTokens: Bool = false) -> FirebaseAI {
  44. let instance = createInstance(
  45. app: app,
  46. apiConfig: backend.apiConfig,
  47. useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
  48. )
  49. // Verify that the `FirebaseAI` instance is always configured with the production endpoint since
  50. // this is the public API surface for creating an instance.
  51. assert(instance.apiConfig.service.endpoint == .firebaseProxyProd)
  52. assert(instance.apiConfig.version == .v1beta)
  53. return instance
  54. }
  55. /// Initializes a generative model with the given parameters.
  56. ///
  57. /// - Note: Refer to [Gemini models](https://firebase.google.com/docs/vertex-ai/gemini-models) for
  58. /// guidance on choosing an appropriate model for your use case.
  59. ///
  60. /// - Parameters:
  61. /// - modelName: The name of the model to use, for example `"gemini-1.5-flash"`; see
  62. /// [available model names
  63. /// ](https://firebase.google.com/docs/vertex-ai/gemini-models#available-model-names) for a
  64. /// list of supported model names.
  65. /// - generationConfig: The content generation parameters your model should use.
  66. /// - safetySettings: A value describing what types of harmful content your model should allow.
  67. /// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
  68. /// - toolConfig: Tool configuration for any `Tool` specified in the request.
  69. /// - systemInstruction: Instructions that direct the model to behave a certain way; currently
  70. /// only text content is supported.
  71. /// - requestOptions: Configuration parameters for sending requests to the backend.
  72. public func generativeModel(modelName: String,
  73. generationConfig: GenerationConfig? = nil,
  74. safetySettings: [SafetySetting]? = nil,
  75. tools: [Tool]? = nil,
  76. toolConfig: ToolConfig? = nil,
  77. systemInstruction: ModelContent? = nil,
  78. requestOptions: RequestOptions = RequestOptions())
  79. -> GenerativeModel {
  80. if !modelName.starts(with: GenerativeModel.geminiModelNamePrefix)
  81. && !modelName.starts(with: GenerativeModel.gemmaModelNamePrefix) {
  82. AILog.warning(code: .unsupportedGeminiModel, """
  83. Unsupported Gemini model "\(modelName)"; see \
  84. https://firebase.google.com/docs/vertex-ai/models for a list supported Gemini model names.
  85. """)
  86. }
  87. return GenerativeModel(
  88. modelName: modelName,
  89. modelResourceName: modelResourceName(modelName: modelName),
  90. firebaseInfo: firebaseInfo,
  91. apiConfig: apiConfig,
  92. generationConfig: generationConfig,
  93. safetySettings: safetySettings,
  94. tools: tools,
  95. toolConfig: toolConfig,
  96. systemInstruction: systemInstruction,
  97. requestOptions: requestOptions
  98. )
  99. }
  100. /// Initializes an ``ImagenModel`` with the given parameters.
  101. ///
  102. /// > Important: Only Imagen 3 models (named `imagen-3.0-*`) are supported.
  103. ///
  104. /// - Parameters:
  105. /// - modelName: The name of the Imagen 3 model to use, for example `"imagen-3.0-generate-002"`;
  106. /// see [model versions](https://firebase.google.com/docs/vertex-ai/models) for a list of
  107. /// supported Imagen 3 models.
  108. /// - generationConfig: Configuration options for generating images with Imagen.
  109. /// - safetySettings: Settings describing what types of potentially harmful content your model
  110. /// should allow.
  111. /// - requestOptions: Configuration parameters for sending requests to the backend.
  112. public func imagenModel(modelName: String, generationConfig: ImagenGenerationConfig? = nil,
  113. safetySettings: ImagenSafetySettings? = nil,
  114. requestOptions: RequestOptions = RequestOptions()) -> ImagenModel {
  115. if !modelName.starts(with: ImagenModel.imagenModelNamePrefix) {
  116. AILog.warning(code: .unsupportedImagenModel, """
  117. Unsupported Imagen model "\(modelName)"; see \
  118. https://firebase.google.com/docs/vertex-ai/models for a list supported Imagen model names.
  119. """)
  120. }
  121. return ImagenModel(
  122. modelResourceName: modelResourceName(modelName: modelName),
  123. firebaseInfo: firebaseInfo,
  124. apiConfig: apiConfig,
  125. generationConfig: generationConfig,
  126. safetySettings: safetySettings,
  127. requestOptions: requestOptions
  128. )
  129. }
  130. /// Initializes a new `TemplateGenerativeModel`.
  131. ///
  132. /// - Returns: A new `TemplateGenerativeModel` instance.
  133. public func templateGenerativeModel() -> TemplateGenerativeModel {
  134. return TemplateGenerativeModel(
  135. generativeAIService: GenerativeAIService(firebaseInfo: firebaseInfo,
  136. urlSession: GenAIURLSession.default),
  137. apiConfig: apiConfig
  138. )
  139. }
  140. /// Initializes a new `TemplateImagenModel`.
  141. ///
  142. /// - Returns: A new `TemplateImagenModel` instance.
  143. public func templateImagenModel() -> TemplateImagenModel {
  144. return TemplateImagenModel(
  145. generativeAIService: GenerativeAIService(firebaseInfo: firebaseInfo,
  146. urlSession: GenAIURLSession.default),
  147. apiConfig: apiConfig
  148. )
  149. }
  150. /// **[Public Preview]** Initializes a ``LiveGenerativeModel`` with the given parameters.
  151. ///
  152. /// > Warning: Using the Firebase AI Logic SDKs with the Gemini Live API is in Public
  153. /// Preview, which means that the feature is not subject to any SLA or deprecation policy and
  154. /// could change in backwards-incompatible ways.
  155. ///
  156. /// > Important: Only models that support the Gemini Live API (typically containing `live-*` in
  157. /// the name) are supported.
  158. ///
  159. /// - Parameters:
  160. /// - modelName: The name of the model to use, for example
  161. /// `"gemini-live-2.5-flash-preview"`;
  162. /// see [model versions](https://firebase.google.com/docs/ai-logic/live-api?api=dev#models-that-support-capability)
  163. /// for a list of supported models.
  164. /// - generationConfig: The content generation parameters your model should use.
  165. /// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
  166. /// - toolConfig: Tool configuration for any ``Tool`` specified in the request.
  167. /// - systemInstruction: Instructions that direct the model to behave a certain way; currently
  168. /// only text content is supported.
  169. /// - requestOptions: Configuration parameters for sending requests to the backend.
  170. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, *)
  171. @available(watchOS, unavailable)
  172. public func liveModel(modelName: String,
  173. generationConfig: LiveGenerationConfig? = nil,
  174. tools: [Tool]? = nil,
  175. toolConfig: ToolConfig? = nil,
  176. systemInstruction: ModelContent? = nil,
  177. requestOptions: RequestOptions = RequestOptions()) -> LiveGenerativeModel {
  178. return LiveGenerativeModel(
  179. modelResourceName: modelResourceName(modelName: modelName),
  180. firebaseInfo: firebaseInfo,
  181. apiConfig: apiConfig,
  182. generationConfig: generationConfig,
  183. tools: tools,
  184. toolConfig: toolConfig,
  185. systemInstruction: systemInstruction,
  186. requestOptions: requestOptions
  187. )
  188. }
  189. /// Class to enable FirebaseAI to register via the Objective-C based Firebase component system
  190. /// to include FirebaseAI in the userAgent.
  191. @objc(FIRVertexAIComponent) class FirebaseVertexAIComponent: NSObject {}
  192. // MARK: - Private
  193. /// Firebase data relevant to Firebase AI.
  194. let firebaseInfo: FirebaseInfo
  195. let apiConfig: APIConfig
  196. /// A map of active `FirebaseAI` instances keyed by the `FirebaseApp`, the `APIConfig`, and
  197. /// `useLimitedUseAppCheckTokens`.
  198. private nonisolated(unsafe) static var instances: [InstanceKey: FirebaseAI] = [:]
  199. /// Lock to manage access to the `instances` array to avoid race conditions.
  200. private nonisolated(unsafe) static var instancesLock: os_unfair_lock = .init()
  201. static func createInstance(app: FirebaseApp?,
  202. apiConfig: APIConfig,
  203. useLimitedUseAppCheckTokens: Bool) -> FirebaseAI {
  204. guard let app = app ?? FirebaseApp.app() else {
  205. fatalError("No instance of the default Firebase app was found.")
  206. }
  207. os_unfair_lock_lock(&instancesLock)
  208. // Unlock before the function returns.
  209. defer { os_unfair_lock_unlock(&instancesLock) }
  210. let instanceKey = InstanceKey(
  211. appName: app.name,
  212. apiConfig: apiConfig,
  213. useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
  214. )
  215. if let instance = instances[instanceKey] {
  216. return instance
  217. }
  218. let newInstance = FirebaseAI(
  219. app: app,
  220. apiConfig: apiConfig,
  221. useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
  222. )
  223. instances[instanceKey] = newInstance
  224. return newInstance
  225. }
  226. init(app: FirebaseApp, apiConfig: APIConfig,
  227. useLimitedUseAppCheckTokens: Bool) {
  228. guard let projectID = app.options.projectID else {
  229. fatalError("The Firebase app named \"\(app.name)\" has no project ID in its configuration.")
  230. }
  231. guard let apiKey = app.options.apiKey else {
  232. fatalError("The Firebase app named \"\(app.name)\" has no API key in its configuration.")
  233. }
  234. firebaseInfo = FirebaseInfo(
  235. appCheck: ComponentType<AppCheckInterop>.instance(
  236. for: AppCheckInterop.self,
  237. in: app.container
  238. ),
  239. auth: ComponentType<AuthInterop>.instance(for: AuthInterop.self, in: app.container),
  240. projectID: projectID,
  241. apiKey: apiKey,
  242. firebaseAppID: app.options.googleAppID,
  243. firebaseApp: app,
  244. useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
  245. )
  246. self.apiConfig = apiConfig
  247. }
  248. func modelResourceName(modelName: String) -> String {
  249. guard !modelName.isEmpty && modelName
  250. .allSatisfy({ !$0.isWhitespace && !$0.isNewline && $0 != "/" }) else {
  251. fatalError("""
  252. Invalid model name "\(modelName)" specified; see \
  253. https://firebase.google.com/docs/vertex-ai/gemini-model#available-models for a list of \
  254. available models.
  255. """)
  256. }
  257. switch apiConfig.service {
  258. case let .vertexAI(endpoint: _, location: location):
  259. return vertexAIModelResourceName(modelName: modelName, location: location)
  260. case .googleAI:
  261. return developerModelResourceName(modelName: modelName)
  262. }
  263. }
  264. private func vertexAIModelResourceName(modelName: String, location: String) -> String {
  265. guard !location.isEmpty && location
  266. .allSatisfy({ !$0.isWhitespace && !$0.isNewline && $0 != "/" }) else {
  267. fatalError("""
  268. Invalid location "\(location)" specified; see \
  269. https://firebase.google.com/docs/vertex-ai/locations?platform=ios#available-locations \
  270. for a list of available locations.
  271. """)
  272. }
  273. let projectID = firebaseInfo.projectID
  274. return "projects/\(projectID)/locations/\(location)/publishers/google/models/\(modelName)"
  275. }
  276. private func developerModelResourceName(modelName: String) -> String {
  277. switch apiConfig.service.endpoint {
  278. case .firebaseProxyStaging, .firebaseProxyProd:
  279. let projectID = firebaseInfo.projectID
  280. return "projects/\(projectID)/models/\(modelName)"
  281. case .googleAIBypassProxy:
  282. return "models/\(modelName)"
  283. }
  284. }
  285. /// Identifier for a unique instance of ``FirebaseAI``.
  286. ///
  287. /// This type is `Hashable` so that it can be used as a key in the `instances` dictionary.
  288. private struct InstanceKey: Sendable, Hashable {
  289. let appName: String
  290. let apiConfig: APIConfig
  291. let useLimitedUseAppCheckTokens: Bool
  292. }
  293. }