Chat.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2023 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. /// An object that represents a back-and-forth chat with a model, capturing the history and saving
  16. /// the context in memory between each message sent.
  17. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. public class Chat {
  19. private let model: GenerativeModel
  20. /// Initializes a new chat representing a 1:1 conversation between model and user.
  21. init(model: GenerativeModel, history: [ModelContent]) {
  22. self.model = model
  23. self.history = history
  24. }
  25. /// The previous content from the chat that has been successfully sent and received from the
  26. /// model. This will be provided to the model for each message sent as context for the discussion.
  27. public var history: [ModelContent]
  28. /// Sends a message using the existing history of this chat as context. If successful, the message
  29. /// and response will be added to the history. If unsuccessful, history will remain unchanged.
  30. /// - Parameter parts: The new content to send as a single chat message.
  31. /// - Returns: The model's response if no error occurred.
  32. /// - Throws: A ``GenerateContentError`` if an error occurred.
  33. public func sendMessage(_ parts: any ThrowingPartsRepresentable...) async throws
  34. -> GenerateContentResponse {
  35. return try await sendMessage([ModelContent(parts: parts)])
  36. }
  37. /// Sends a message using the existing history of this chat as context. If successful, the message
  38. /// and response will be added to the history. If unsuccessful, history will remain unchanged.
  39. /// - Parameter content: The new content to send as a single chat message.
  40. /// - Returns: The model's response if no error occurred.
  41. /// - Throws: A ``GenerateContentError`` if an error occurred.
  42. public func sendMessage(_ content: @autoclosure () throws -> [ModelContent]) async throws
  43. -> GenerateContentResponse {
  44. // Ensure that the new content has the role set.
  45. let newContent: [ModelContent]
  46. do {
  47. newContent = try content().map(populateContentRole(_:))
  48. } catch let underlying {
  49. if let contentError = underlying as? ImageConversionError {
  50. throw GenerateContentError.promptImageContentError(underlying: contentError)
  51. } else {
  52. throw GenerateContentError.internalError(underlying: underlying)
  53. }
  54. }
  55. // Send the history alongside the new message as context.
  56. let request = history + newContent
  57. let result = try await model.generateContent(request)
  58. guard let reply = result.candidates.first?.content else {
  59. let error = NSError(domain: "com.google.generative-ai",
  60. code: -1,
  61. userInfo: [
  62. NSLocalizedDescriptionKey: "No candidates with content available.",
  63. ])
  64. throw GenerateContentError.internalError(underlying: error)
  65. }
  66. // Make sure we inject the role into the content received.
  67. let toAdd = ModelContent(role: "model", parts: reply.parts)
  68. // Append the request and successful result to history, then return the value.
  69. history.append(contentsOf: newContent)
  70. history.append(toAdd)
  71. return result
  72. }
  73. /// Sends a message using the existing history of this chat as context. If successful, the message
  74. /// and response will be added to the history. If unsuccessful, history will remain unchanged.
  75. /// - Parameter parts: The new content to send as a single chat message.
  76. /// - Returns: A stream containing the model's response or an error if an error occurred.
  77. @available(macOS 12.0, *)
  78. public func sendMessageStream(_ parts: any ThrowingPartsRepresentable...)
  79. -> AsyncThrowingStream<GenerateContentResponse, Error> {
  80. return try sendMessageStream([ModelContent(parts: parts)])
  81. }
  82. /// Sends a message using the existing history of this chat as context. If successful, the message
  83. /// and response will be added to the history. If unsuccessful, history will remain unchanged.
  84. /// - Parameter content: The new content to send as a single chat message.
  85. /// - Returns: A stream containing the model's response or an error if an error occurred.
  86. @available(macOS 12.0, *)
  87. public func sendMessageStream(_ content: @autoclosure () throws -> [ModelContent])
  88. -> AsyncThrowingStream<GenerateContentResponse, Error> {
  89. let resolvedContent: [ModelContent]
  90. do {
  91. resolvedContent = try content()
  92. } catch let underlying {
  93. return AsyncThrowingStream { continuation in
  94. let error: Error
  95. if let contentError = underlying as? ImageConversionError {
  96. error = GenerateContentError.promptImageContentError(underlying: contentError)
  97. } else {
  98. error = GenerateContentError.internalError(underlying: underlying)
  99. }
  100. continuation.finish(throwing: error)
  101. }
  102. }
  103. return AsyncThrowingStream { continuation in
  104. Task {
  105. var aggregatedContent: [ModelContent] = []
  106. // Ensure that the new content has the role set.
  107. let newContent: [ModelContent] = resolvedContent.map(populateContentRole(_:))
  108. // Send the history alongside the new message as context.
  109. let request = history + newContent
  110. let stream = model.generateContentStream(request)
  111. do {
  112. for try await chunk in stream {
  113. // Capture any content that's streaming. This should be populated if there's no error.
  114. if let chunkContent = chunk.candidates.first?.content {
  115. aggregatedContent.append(chunkContent)
  116. }
  117. // Pass along the chunk.
  118. continuation.yield(chunk)
  119. }
  120. } catch {
  121. // Rethrow the error that the underlying stream threw. Don't add anything to history.
  122. continuation.finish(throwing: error)
  123. return
  124. }
  125. // Save the request.
  126. history.append(contentsOf: newContent)
  127. // Aggregate the content to add it to the history before we finish.
  128. let aggregated = aggregatedChunks(aggregatedContent)
  129. history.append(aggregated)
  130. continuation.finish()
  131. }
  132. }
  133. }
  134. private func aggregatedChunks(_ chunks: [ModelContent]) -> ModelContent {
  135. var parts: [ModelContent.Part] = []
  136. var combinedText = ""
  137. for aggregate in chunks {
  138. // Loop through all the parts, aggregating the text and adding the images.
  139. for part in aggregate.parts {
  140. switch part {
  141. case let .text(str):
  142. combinedText += str
  143. case .data, .fileData, .functionCall, .functionResponse:
  144. // Don't combine it, just add to the content. If there's any text pending, add that as
  145. // a part.
  146. if !combinedText.isEmpty {
  147. parts.append(.text(combinedText))
  148. combinedText = ""
  149. }
  150. parts.append(part)
  151. }
  152. }
  153. }
  154. if !combinedText.isEmpty {
  155. parts.append(.text(combinedText))
  156. }
  157. return ModelContent(role: "model", parts: parts)
  158. }
  159. /// Populates the `role` field with `user` if it doesn't exist. Required in chat sessions.
  160. private func populateContentRole(_ content: ModelContent) -> ModelContent {
  161. if content.role != nil {
  162. return content
  163. } else {
  164. return ModelContent(role: "user", parts: content.parts)
  165. }
  166. }
  167. }