Chat.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. public final class Chat: Sendable {
  19. private let model: GenerativeModel
  20. private let _history: History
  21. init(model: GenerativeModel, history: [ModelContent]) {
  22. self.model = model
  23. _history = History(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. get {
  29. return _history.history
  30. }
  31. set {
  32. _history.history = newValue
  33. }
  34. }
  35. /// Sends a message using the existing history of this chat as context. If successful, the message
  36. /// and response will be added to the history. If unsuccessful, history will remain unchanged.
  37. /// - Parameter parts: The new content to send as a single chat message.
  38. /// - Returns: The model's response if no error occurred.
  39. /// - Throws: A ``GenerateContentError`` if an error occurred.
  40. public func sendMessage(_ parts: any PartsRepresentable...) async throws
  41. -> GenerateContentResponse {
  42. return try await sendMessage([ModelContent(parts: parts)])
  43. }
  44. /// Sends a message using the existing history of this chat as context. If successful, the message
  45. /// and response will be added to the history. If unsuccessful, history will remain unchanged.
  46. /// - Parameter content: The new content to send as a single chat message.
  47. /// - Returns: The model's response if no error occurred.
  48. /// - Throws: A ``GenerateContentError`` if an error occurred.
  49. public func sendMessage(_ content: [ModelContent]) async throws
  50. -> GenerateContentResponse {
  51. // Ensure that the new content has the role set.
  52. let newContent = content.map(populateContentRole(_:))
  53. // Send the history alongside the new message as context.
  54. let request = history + newContent
  55. let result = try await model.generateContent(request)
  56. guard let reply = result.candidates.first?.content else {
  57. let error = NSError(domain: "com.google.generative-ai",
  58. code: -1,
  59. userInfo: [
  60. NSLocalizedDescriptionKey: "No candidates with content available.",
  61. ])
  62. throw GenerateContentError.internalError(underlying: error)
  63. }
  64. // Make sure we inject the role into the content received.
  65. let toAdd = ModelContent(role: "model", parts: reply.parts)
  66. // Append the request and successful result to history, then return the value.
  67. _history.append(contentsOf: newContent)
  68. _history.append(toAdd)
  69. return result
  70. }
  71. /// Sends a message using the existing history of this chat as context. If successful, the message
  72. /// and response will be added to the history. If unsuccessful, history will remain unchanged.
  73. /// - Parameter parts: The new content to send as a single chat message.
  74. /// - Returns: A stream containing the model's response or an error if an error occurred.
  75. @available(macOS 12.0, *)
  76. public func sendMessageStream(_ parts: any PartsRepresentable...) throws
  77. -> AsyncThrowingStream<GenerateContentResponse, Error> {
  78. return try sendMessageStream([ModelContent(parts: parts)])
  79. }
  80. /// Sends a message using the existing history of this chat as context. If successful, the message
  81. /// and response will be added to the history. If unsuccessful, history will remain unchanged.
  82. /// - Parameter content: The new content to send as a single chat message.
  83. /// - Returns: A stream containing the model's response or an error if an error occurred.
  84. @available(macOS 12.0, *)
  85. public func sendMessageStream(_ content: [ModelContent]) throws
  86. -> AsyncThrowingStream<GenerateContentResponse, Error> {
  87. // Ensure that the new content has the role set.
  88. let newContent: [ModelContent] = content.map(populateContentRole(_:))
  89. // Send the history alongside the new message as context.
  90. let request = history + newContent
  91. let stream = try model.generateContentStream(request)
  92. return AsyncThrowingStream { continuation in
  93. Task {
  94. var aggregatedContent: [ModelContent] = []
  95. do {
  96. for try await chunk in stream {
  97. // Capture any content that's streaming. This should be populated if there's no error.
  98. if let chunkContent = chunk.candidates.first?.content {
  99. aggregatedContent.append(chunkContent)
  100. }
  101. // Pass along the chunk.
  102. continuation.yield(chunk)
  103. }
  104. } catch {
  105. // Rethrow the error that the underlying stream threw. Don't add anything to history.
  106. continuation.finish(throwing: error)
  107. return
  108. }
  109. // Save the request.
  110. _history.append(contentsOf: newContent)
  111. // Aggregate the content to add it to the history before we finish.
  112. let aggregated = self._history.aggregatedChunks(aggregatedContent)
  113. self._history.append(aggregated)
  114. continuation.finish()
  115. }
  116. }
  117. }
  118. /// Populates the `role` field with `user` if it doesn't exist. Required in chat sessions.
  119. private func populateContentRole(_ content: ModelContent) -> ModelContent {
  120. if content.role != nil {
  121. return content
  122. } else {
  123. return ModelContent(role: "user", parts: content.parts)
  124. }
  125. }
  126. }