VertexAI.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. @_exported public import FirebaseAI
  15. import FirebaseCore
  16. /// The Vertex AI for Firebase SDK provides access to Gemini models directly from your app.
  17. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. public class VertexAI {
  19. // MARK: - Public APIs
  20. /// Creates an instance of `VertexAI`.
  21. ///
  22. /// - Parameters:
  23. /// - app: A custom `FirebaseApp` used for initialization; if not specified, uses the default
  24. /// ``FirebaseApp``.
  25. /// - location: The region identifier, defaulting to `us-central1`; see
  26. /// [Vertex AI locations]
  27. /// (https://firebase.google.com/docs/vertex-ai/locations?platform=ios#available-locations)
  28. /// for a list of supported locations.
  29. /// - Returns: A `VertexAI` instance, configured with the custom `FirebaseApp`.
  30. public static func vertexAI(app: FirebaseApp? = nil,
  31. location: String = "us-central1") -> VertexAI {
  32. let firebaseAI = FirebaseAI.firebaseAI(app: app, backend: .vertexAI(location: location))
  33. return VertexAI(firebaseAI: firebaseAI)
  34. }
  35. /// Initializes a generative model with the given parameters.
  36. ///
  37. /// - Note: Refer to [Gemini models](https://firebase.google.com/docs/vertex-ai/gemini-models) for
  38. /// guidance on choosing an appropriate model for your use case.
  39. ///
  40. /// - Parameters:
  41. /// - modelName: The name of the model to use, for example `"gemini-1.5-flash"`; see
  42. /// [available model names
  43. /// ](https://firebase.google.com/docs/vertex-ai/gemini-models#available-model-names) for a
  44. /// list of supported model names.
  45. /// - generationConfig: The content generation parameters your model should use.
  46. /// - safetySettings: A value describing what types of harmful content your model should allow.
  47. /// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
  48. /// - toolConfig: Tool configuration for any `Tool` specified in the request.
  49. /// - systemInstruction: Instructions that direct the model to behave a certain way; currently
  50. /// only text content is supported.
  51. /// - requestOptions: Configuration parameters for sending requests to the backend.
  52. public func generativeModel(modelName: String,
  53. generationConfig: GenerationConfig? = nil,
  54. safetySettings: [SafetySetting]? = nil,
  55. tools: [Tool]? = nil,
  56. toolConfig: ToolConfig? = nil,
  57. systemInstruction: ModelContent? = nil,
  58. requestOptions: RequestOptions = RequestOptions())
  59. -> GenerativeModel {
  60. return firebaseAI.generativeModel(
  61. modelName: modelName,
  62. generationConfig: generationConfig,
  63. safetySettings: safetySettings,
  64. tools: tools,
  65. toolConfig: toolConfig,
  66. systemInstruction: systemInstruction,
  67. requestOptions: requestOptions
  68. )
  69. }
  70. /// **[Public Preview]** Initializes an ``ImagenModel`` with the given parameters.
  71. ///
  72. /// > Warning: For Vertex AI in Firebase, image generation using Imagen 3 models is in Public
  73. /// Preview, which means that the feature is not subject to any SLA or deprecation policy and
  74. /// could change in backwards-incompatible ways.
  75. ///
  76. /// > Important: Only Imagen 3 models (named `imagen-3.0-*`) are supported.
  77. ///
  78. /// - Parameters:
  79. /// - modelName: The name of the Imagen 3 model to use, for example `"imagen-3.0-generate-002"`;
  80. /// see [model versions](https://firebase.google.com/docs/vertex-ai/models) for a list of
  81. /// supported Imagen 3 models.
  82. /// - generationConfig: Configuration options for generating images with Imagen.
  83. /// - safetySettings: Settings describing what types of potentially harmful content your model
  84. /// should allow.
  85. /// - requestOptions: Configuration parameters for sending requests to the backend.
  86. public func imagenModel(modelName: String, generationConfig: ImagenGenerationConfig? = nil,
  87. safetySettings: ImagenSafetySettings? = nil,
  88. requestOptions: RequestOptions = RequestOptions()) -> ImagenModel {
  89. return firebaseAI.imagenModel(
  90. modelName: modelName,
  91. generationConfig: generationConfig,
  92. safetySettings: safetySettings,
  93. requestOptions: requestOptions
  94. )
  95. }
  96. // MARK: - Internal APIs
  97. let firebaseAI: FirebaseAI
  98. init(firebaseAI: FirebaseAI) {
  99. self.firebaseAI = firebaseAI
  100. }
  101. }