TemplateGenerativeModel.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. public final class TemplateGenerativeModel: Sendable {
  19. let generativeAIService: GenerativeAIService
  20. let apiConfig: APIConfig
  21. init(generativeAIService: GenerativeAIService, apiConfig: APIConfig) {
  22. self.generativeAIService = generativeAIService
  23. self.apiConfig = apiConfig
  24. }
  25. /// Generates content from a prompt template and variables.
  26. ///
  27. /// - Parameters:
  28. /// - template: The prompt template to use.
  29. /// - variables: A dictionary of variables to substitute into the template.
  30. /// - Returns: The content generated by the model.
  31. /// - Throws: A ``GenerateContentError`` if the request failed.
  32. public func generateContent(template: String,
  33. variables: [String: Any],
  34. options: RequestOptions = RequestOptions()) async throws
  35. -> GenerateContentResponse {
  36. let templateVariables = try variables.mapValues { try TemplateVariable(value: $0) }
  37. return try await generateContentWithHistory(
  38. history: [],
  39. template: template,
  40. variables: templateVariables,
  41. options: options
  42. )
  43. }
  44. /// Generates content from a prompt template, variables, and history.
  45. ///
  46. /// - Parameters:
  47. /// - history: The conversation history to use.
  48. /// - template: The prompt template to use.
  49. /// - variables: A dictionary of variables to substitute into the template.
  50. /// - Returns: The content generated by the model.
  51. /// - Throws: A ``GenerateContentError`` if the request failed.
  52. func generateContentWithHistory(history: [ModelContent], template: String,
  53. variables: [String: TemplateVariable],
  54. options: RequestOptions = RequestOptions()) async throws
  55. -> GenerateContentResponse {
  56. let request = TemplateGenerateContentRequest(
  57. template: template,
  58. variables: variables,
  59. history: history,
  60. projectID: generativeAIService.firebaseInfo.projectID,
  61. stream: false,
  62. apiConfig: apiConfig,
  63. options: options
  64. )
  65. let response: GenerateContentResponse = try await generativeAIService
  66. .loadRequest(request: request)
  67. return response
  68. }
  69. public func generateContentStream(template: String,
  70. variables: [String: Any],
  71. options: RequestOptions = RequestOptions()) throws
  72. -> AsyncThrowingStream<GenerateContentResponse, Error> {
  73. let templateVariables = try variables.mapValues { try TemplateVariable(value: $0) }
  74. let request = TemplateGenerateContentRequest(
  75. template: template,
  76. variables: templateVariables,
  77. history: [],
  78. projectID: generativeAIService.firebaseInfo.projectID,
  79. stream: true,
  80. apiConfig: apiConfig,
  81. options: options
  82. )
  83. return generativeAIService.loadRequestStream(request: request)
  84. }
  85. func generateContentStreamWithHistory(history: [ModelContent], template: String,
  86. variables: [String: TemplateVariable],
  87. options: RequestOptions = RequestOptions()) throws
  88. -> AsyncThrowingStream<GenerateContentResponse, Error> {
  89. let request = TemplateGenerateContentRequest(
  90. template: template,
  91. variables: variables,
  92. history: history,
  93. projectID: generativeAIService.firebaseInfo.projectID,
  94. stream: true,
  95. apiConfig: apiConfig,
  96. options: options
  97. )
  98. return generativeAIService.loadRequestStream(request: request)
  99. }
  100. /// Creates a new chat conversation using this model with the provided history and template.
  101. ///
  102. /// - Parameters:
  103. /// - template: The prompt template to use.
  104. /// - history: The conversation history to use.
  105. /// - Returns: A new ``TemplateChatSession`` instance.
  106. public func startChat(template: String,
  107. history: [ModelContent] = []) -> TemplateChatSession {
  108. return TemplateChatSession(
  109. model: self,
  110. template: template,
  111. history: history
  112. )
  113. }
  114. }