TemplateGenerativeModel.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 multimodal model (like Gemini), with the ability to generate
  16. /// content based on various input types.
  17. ///
  18. /// **Public Preview**: This API is a public preview and may be subject to change.
  19. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  20. public final class TemplateGenerativeModel: Sendable {
  21. let generativeAIService: GenerativeAIService
  22. let apiConfig: APIConfig
  23. init(generativeAIService: GenerativeAIService, apiConfig: APIConfig) {
  24. self.generativeAIService = generativeAIService
  25. self.apiConfig = apiConfig
  26. }
  27. /// Generates content from a prompt template and inputs.
  28. ///
  29. /// **Public Preview**: This API is a public preview and may be subject to change.
  30. ///
  31. /// - Parameters:
  32. /// - templateID: The ID of the prompt template to use.
  33. /// - inputs: A dictionary of variables to substitute into the template.
  34. /// - options: The ``RequestOptions`` for the request, currently used to override default
  35. /// request timeout.
  36. /// - Returns: The content generated by the model.
  37. /// - Throws: A ``GenerateContentError`` if the request failed.
  38. public func generateContent(templateID: String,
  39. inputs: [String: Any],
  40. options: RequestOptions = RequestOptions()) async throws
  41. -> GenerateContentResponse {
  42. let templateInputs = try inputs.mapValues { try TemplateInput(value: $0) }
  43. return try await generateContentWithHistory(
  44. history: [],
  45. template: templateID,
  46. inputs: templateInputs,
  47. options: options
  48. )
  49. }
  50. /// Generates content from a prompt template, inputs, and history.
  51. ///
  52. /// - Parameters:
  53. /// - history: The conversation history to use.
  54. /// - template: The prompt template to use.
  55. /// - inputs: A dictionary of variables to substitute into the template.
  56. /// - Returns: The content generated by the model.
  57. /// - Throws: A ``GenerateContentError`` if the request failed.
  58. func generateContentWithHistory(history: [ModelContent], template: String,
  59. inputs: [String: TemplateInput],
  60. options: RequestOptions = RequestOptions()) async throws
  61. -> GenerateContentResponse {
  62. let request = TemplateGenerateContentRequest(
  63. template: template,
  64. inputs: inputs,
  65. history: history,
  66. projectID: generativeAIService.firebaseInfo.projectID,
  67. stream: false,
  68. apiConfig: apiConfig,
  69. options: options
  70. )
  71. let response: GenerateContentResponse = try await generativeAIService
  72. .loadRequest(request: request)
  73. return response
  74. }
  75. /// Generates content from a prompt template and inputs, with streaming responses.
  76. ///
  77. /// **Public Preview**: This API is a public preview and may be subject to change.
  78. ///
  79. /// - Parameters:
  80. /// - templateID: The ID of the prompt template to use.
  81. /// - inputs: A dictionary of variables to substitute into the template.
  82. /// - options: The ``RequestOptions`` for the request, currently used to override default
  83. /// request timeout.
  84. /// - Returns: An `AsyncThrowingStream` that yields `GenerateContentResponse` objects.
  85. /// - Throws: A ``GenerateContentError`` if the request failed.
  86. public func generateContentStream(templateID: String,
  87. inputs: [String: Any],
  88. options: RequestOptions = RequestOptions()) throws
  89. -> AsyncThrowingStream<GenerateContentResponse, Error> {
  90. let templateInputs = try inputs.mapValues { try TemplateInput(value: $0) }
  91. let request = TemplateGenerateContentRequest(
  92. template: templateID,
  93. inputs: templateInputs,
  94. history: [],
  95. projectID: generativeAIService.firebaseInfo.projectID,
  96. stream: true,
  97. apiConfig: apiConfig,
  98. options: options
  99. )
  100. return generativeAIService.loadRequestStream(request: request)
  101. }
  102. func generateContentStreamWithHistory(history: [ModelContent], template: String,
  103. inputs: [String: TemplateInput],
  104. options: RequestOptions = RequestOptions()) throws
  105. -> AsyncThrowingStream<GenerateContentResponse, Error> {
  106. let request = TemplateGenerateContentRequest(
  107. template: template,
  108. inputs: inputs,
  109. history: history,
  110. projectID: generativeAIService.firebaseInfo.projectID,
  111. stream: true,
  112. apiConfig: apiConfig,
  113. options: options
  114. )
  115. return generativeAIService.loadRequestStream(request: request)
  116. }
  117. // TODO: Restore `public` determined to be releaseable along with the contents of TemplateChatSession.
  118. /// Creates a new chat conversation using this model with the provided history and template.
  119. ///
  120. /// - Parameters:
  121. /// - templateID: The ID of the prompt template to use.
  122. /// - history: The conversation history to use.
  123. /// - Returns: A new ``TemplateChatSession`` instance.
  124. func startChat(templateID: String,
  125. history: [ModelContent] = []) -> TemplateChatSession {
  126. return TemplateChatSession(
  127. model: self,
  128. templateID: templateID,
  129. history: history
  130. )
  131. }
  132. }