TemplateImagenModel.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2025 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 Foundation
  15. /// A type that represents a remote image generation model (like Imagen), with the ability to
  16. /// generate
  17. /// images based on various input types.
  18. ///
  19. /// **Public Preview**: This API is a public preview and may be subject to change.
  20. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  21. public final class TemplateImagenModel: Sendable {
  22. let generativeAIService: GenerativeAIService
  23. let apiConfig: APIConfig
  24. init(generativeAIService: GenerativeAIService, apiConfig: APIConfig) {
  25. self.generativeAIService = generativeAIService
  26. self.apiConfig = apiConfig
  27. }
  28. /// Generates images from a prompt template and variables.
  29. ///
  30. /// - Parameters:
  31. /// - template: The prompt template to use.
  32. /// - variables: A dictionary of variables to substitute into the template.
  33. /// - options: The ``RequestOptions`` for the request, currently used to override default
  34. /// request timeout.
  35. /// - Returns: The images generated by the model.
  36. /// - Throws: An error if the request failed.
  37. public func generateImages(templateID: String,
  38. inputs: [String: Any],
  39. options: RequestOptions = RequestOptions()) async throws
  40. -> ImagenGenerationResponse<ImagenInlineImage> {
  41. let templateInputs = try inputs.mapValues { try TemplateInput(value: $0) }
  42. let projectID = generativeAIService.firebaseInfo.projectID
  43. let request = TemplateImagenGenerationRequest<ImagenInlineImage>(
  44. template: templateID,
  45. inputs: templateInputs,
  46. projectID: projectID,
  47. apiConfig: apiConfig,
  48. options: options
  49. )
  50. return try await generativeAIService.loadRequest(request: request)
  51. }
  52. }