TemplateGenerativeModel.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 TemplateInputRepresentable],
  40. options: RequestOptions = RequestOptions()) async throws
  41. -> GenerateContentResponse {
  42. return try await generateContentWithHistory(
  43. history: [],
  44. template: templateID,
  45. inputs: inputs.mapValues { $0.templateInputRepresentation },
  46. options: options
  47. )
  48. }
  49. /// Generates content from a prompt template, inputs, and history.
  50. ///
  51. /// - Parameters:
  52. /// - history: The conversation history to use.
  53. /// - template: The prompt template to use.
  54. /// - inputs: A dictionary of variables to substitute into the template.
  55. /// - Returns: The content generated by the model.
  56. /// - Throws: A ``GenerateContentError`` if the request failed.
  57. func generateContentWithHistory(history: [ModelContent], template: String,
  58. inputs: [String: TemplateInput],
  59. options: RequestOptions = RequestOptions()) async throws
  60. -> GenerateContentResponse {
  61. let request = TemplateGenerateContentRequest(
  62. template: template,
  63. inputs: inputs,
  64. history: history,
  65. projectID: generativeAIService.firebaseInfo.projectID,
  66. stream: false,
  67. apiConfig: apiConfig,
  68. options: options
  69. )
  70. let response: GenerateContentResponse = try await generativeAIService
  71. .loadRequest(request: request)
  72. return response
  73. }
  74. /// Generates content from a prompt template and inputs, with streaming responses.
  75. ///
  76. /// **Public Preview**: This API is a public preview and may be subject to change.
  77. ///
  78. /// - Parameters:
  79. /// - templateID: The ID of the prompt template to use.
  80. /// - inputs: A dictionary of variables to substitute into the template.
  81. /// - options: The ``RequestOptions`` for the request, currently used to override default
  82. /// request timeout.
  83. /// - Returns: An `AsyncThrowingStream` that yields `GenerateContentResponse` objects.
  84. /// - Throws: A ``GenerateContentError`` if the request failed.
  85. public func generateContentStream(templateID: String,
  86. inputs: [String: any TemplateInputRepresentable],
  87. options: RequestOptions = RequestOptions()) throws
  88. -> AsyncThrowingStream<GenerateContentResponse, Error> {
  89. let request = TemplateGenerateContentRequest(
  90. template: templateID,
  91. inputs: inputs.mapValues { $0.templateInputRepresentation },
  92. history: [],
  93. projectID: generativeAIService.firebaseInfo.projectID,
  94. stream: true,
  95. apiConfig: apiConfig,
  96. options: options
  97. )
  98. return generativeAIService.loadRequestStream(request: request)
  99. }
  100. func generateContentStreamWithHistory(history: [ModelContent], template: String,
  101. inputs: [String: TemplateInput],
  102. options: RequestOptions = RequestOptions()) throws
  103. -> AsyncThrowingStream<GenerateContentResponse, Error> {
  104. let request = TemplateGenerateContentRequest(
  105. template: template,
  106. inputs: inputs,
  107. history: history,
  108. projectID: generativeAIService.firebaseInfo.projectID,
  109. stream: true,
  110. apiConfig: apiConfig,
  111. options: options
  112. )
  113. return generativeAIService.loadRequestStream(request: request)
  114. }
  115. // TODO: Restore `public` determined to be releaseable along with the contents of TemplateChatSession.
  116. /// Creates a new chat conversation using this model with the provided history and template.
  117. ///
  118. /// - Parameters:
  119. /// - templateID: The ID of the prompt template to use.
  120. /// - history: The conversation history to use.
  121. /// - Returns: A new ``TemplateChatSession`` instance.
  122. func startChat(templateID: String,
  123. history: [ModelContent] = []) -> TemplateChatSession {
  124. return TemplateChatSession(
  125. model: self,
  126. templateID: templateID,
  127. history: history
  128. )
  129. }
  130. }