GenerateContentResponse.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. /// The model's response to a generate content request.
  16. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  17. public struct GenerateContentResponse: Sendable {
  18. /// Token usage metadata for processing the generate content request.
  19. public struct UsageMetadata: Sendable {
  20. /// The number of tokens in the request prompt.
  21. public let promptTokenCount: Int
  22. /// The total number of tokens across the generated response candidates.
  23. public let candidatesTokenCount: Int
  24. /// The total number of tokens in both the request and response.
  25. public let totalTokenCount: Int
  26. }
  27. /// A list of candidate response content, ordered from best to worst.
  28. public let candidates: [CandidateResponse]
  29. /// A value containing the safety ratings for the response, or, if the request was blocked, a
  30. /// reason for blocking the request.
  31. public let promptFeedback: PromptFeedback?
  32. /// Token usage metadata for processing the generate content request.
  33. public let usageMetadata: UsageMetadata?
  34. /// The response's content as text, if it exists.
  35. public var text: String? {
  36. guard let candidate = candidates.first else {
  37. Logging.default
  38. .error("[FirebaseVertexAI] Could not get text from a response that had no candidates.")
  39. return nil
  40. }
  41. let textValues: [String] = candidate.content.parts.compactMap { part in
  42. guard case let .text(text) = part else {
  43. return nil
  44. }
  45. return text
  46. }
  47. guard textValues.count > 0 else {
  48. Logging.default
  49. .error("[FirebaseVertexAI] Could not get a text part from the first candidate.")
  50. return nil
  51. }
  52. return textValues.joined(separator: " ")
  53. }
  54. /// Returns function calls found in any `Part`s of the first candidate of the response, if any.
  55. public var functionCalls: [FunctionCall] {
  56. guard let candidate = candidates.first else {
  57. return []
  58. }
  59. return candidate.content.parts.compactMap { part in
  60. guard case let .functionCall(functionCall) = part else {
  61. return nil
  62. }
  63. return functionCall
  64. }
  65. }
  66. /// Initializer for SwiftUI previews or tests.
  67. public init(candidates: [CandidateResponse], promptFeedback: PromptFeedback? = nil,
  68. usageMetadata: UsageMetadata? = nil) {
  69. self.candidates = candidates
  70. self.promptFeedback = promptFeedback
  71. self.usageMetadata = usageMetadata
  72. }
  73. }
  74. /// A struct representing a possible reply to a content generation prompt. Each content generation
  75. /// prompt may produce multiple candidate responses.
  76. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  77. public struct CandidateResponse: Sendable {
  78. /// The response's content.
  79. public let content: ModelContent
  80. /// The safety rating of the response content.
  81. public let safetyRatings: [SafetyRating]
  82. /// The reason the model stopped generating content, if it exists; for example, if the model
  83. /// generated a predefined stop sequence.
  84. public let finishReason: FinishReason?
  85. /// Cited works in the model's response content, if it exists.
  86. public let citationMetadata: CitationMetadata?
  87. /// Initializer for SwiftUI previews or tests.
  88. public init(content: ModelContent, safetyRatings: [SafetyRating], finishReason: FinishReason?,
  89. citationMetadata: CitationMetadata?) {
  90. self.content = content
  91. self.safetyRatings = safetyRatings
  92. self.finishReason = finishReason
  93. self.citationMetadata = citationMetadata
  94. }
  95. }
  96. /// A collection of source attributions for a piece of content.
  97. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  98. public struct CitationMetadata: Sendable {
  99. /// A list of individual cited sources and the parts of the content to which they apply.
  100. public let citationSources: [Citation]
  101. }
  102. /// A struct describing a source attribution.
  103. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  104. public struct Citation: Sendable {
  105. /// The inclusive beginning of a sequence in a model response that derives from a cited source.
  106. public let startIndex: Int
  107. /// The exclusive end of a sequence in a model response that derives from a cited source.
  108. public let endIndex: Int
  109. /// A link to the cited source, if available.
  110. public let uri: String?
  111. /// The title of the cited source, if available.
  112. public let title: String?
  113. /// The license the cited source work is distributed under, if specified.
  114. public let license: String?
  115. }
  116. /// A value enumerating possible reasons for a model to terminate a content generation request.
  117. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  118. public enum FinishReason: String, Sendable {
  119. case unknown = "FINISH_REASON_UNKNOWN"
  120. case unspecified = "FINISH_REASON_UNSPECIFIED"
  121. /// Natural stop point of the model or provided stop sequence.
  122. case stop = "STOP"
  123. /// The maximum number of tokens as specified in the request was reached.
  124. case maxTokens = "MAX_TOKENS"
  125. /// The token generation was stopped because the response was flagged for safety reasons.
  126. /// NOTE: When streaming, the Candidate.content will be empty if content filters blocked the
  127. /// output.
  128. case safety = "SAFETY"
  129. /// The token generation was stopped because the response was flagged for unauthorized citations.
  130. case recitation = "RECITATION"
  131. /// All other reasons that stopped token generation.
  132. case other = "OTHER"
  133. }
  134. /// A metadata struct containing any feedback the model had on the prompt it was provided.
  135. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  136. public struct PromptFeedback: Sendable {
  137. /// A type describing possible reasons to block a prompt.
  138. public enum BlockReason: String, Sendable {
  139. /// The block reason is unknown.
  140. case unknown = "UNKNOWN"
  141. /// The block reason was not specified in the server response.
  142. case unspecified = "BLOCK_REASON_UNSPECIFIED"
  143. /// The prompt was blocked because it was deemed unsafe.
  144. case safety = "SAFETY"
  145. /// All other block reasons.
  146. case other = "OTHER"
  147. }
  148. /// The reason a prompt was blocked, if it was blocked.
  149. public let blockReason: BlockReason?
  150. /// The safety ratings of the prompt.
  151. public let safetyRatings: [SafetyRating]
  152. /// Initializer for SwiftUI previews or tests.
  153. public init(blockReason: BlockReason?, safetyRatings: [SafetyRating]) {
  154. self.blockReason = blockReason
  155. self.safetyRatings = safetyRatings
  156. }
  157. }
  158. // MARK: - Codable Conformances
  159. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  160. extension GenerateContentResponse: Decodable {
  161. enum CodingKeys: CodingKey {
  162. case candidates
  163. case promptFeedback
  164. case usageMetadata
  165. }
  166. public init(from decoder: Decoder) throws {
  167. let container = try decoder.container(keyedBy: CodingKeys.self)
  168. guard container.contains(CodingKeys.candidates) || container
  169. .contains(CodingKeys.promptFeedback) else {
  170. let context = DecodingError.Context(
  171. codingPath: [],
  172. debugDescription: "Failed to decode GenerateContentResponse;" +
  173. " missing keys 'candidates' and 'promptFeedback'."
  174. )
  175. throw DecodingError.dataCorrupted(context)
  176. }
  177. if let candidates = try container.decodeIfPresent(
  178. [CandidateResponse].self,
  179. forKey: .candidates
  180. ) {
  181. self.candidates = candidates
  182. } else {
  183. candidates = []
  184. }
  185. promptFeedback = try container.decodeIfPresent(PromptFeedback.self, forKey: .promptFeedback)
  186. usageMetadata = try container.decodeIfPresent(UsageMetadata.self, forKey: .usageMetadata)
  187. }
  188. }
  189. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  190. extension GenerateContentResponse.UsageMetadata: Decodable {
  191. enum CodingKeys: CodingKey {
  192. case promptTokenCount
  193. case candidatesTokenCount
  194. case totalTokenCount
  195. }
  196. public init(from decoder: any Decoder) throws {
  197. let container = try decoder.container(keyedBy: CodingKeys.self)
  198. promptTokenCount = try container.decodeIfPresent(Int.self, forKey: .promptTokenCount) ?? 0
  199. candidatesTokenCount = try container
  200. .decodeIfPresent(Int.self, forKey: .candidatesTokenCount) ?? 0
  201. totalTokenCount = try container.decodeIfPresent(Int.self, forKey: .totalTokenCount) ?? 0
  202. }
  203. }
  204. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  205. extension CandidateResponse: Decodable {
  206. enum CodingKeys: CodingKey {
  207. case content
  208. case safetyRatings
  209. case finishReason
  210. case finishMessage
  211. case citationMetadata
  212. }
  213. /// Initializes a response from a decoder. Used for decoding server responses; not for public
  214. /// use.
  215. public init(from decoder: Decoder) throws {
  216. let container = try decoder.container(keyedBy: CodingKeys.self)
  217. do {
  218. if let content = try container.decodeIfPresent(ModelContent.self, forKey: .content) {
  219. self.content = content
  220. } else {
  221. content = ModelContent(parts: [])
  222. }
  223. } catch {
  224. // Check if `content` can be decoded as an empty dictionary to detect the `"content": {}` bug.
  225. if let content = try? container.decode([String: String].self, forKey: .content),
  226. content.isEmpty {
  227. throw InvalidCandidateError.emptyContent(underlyingError: error)
  228. } else {
  229. throw InvalidCandidateError.malformedContent(underlyingError: error)
  230. }
  231. }
  232. if let safetyRatings = try container.decodeIfPresent(
  233. [SafetyRating].self,
  234. forKey: .safetyRatings
  235. ) {
  236. self.safetyRatings = safetyRatings
  237. } else {
  238. safetyRatings = []
  239. }
  240. finishReason = try container.decodeIfPresent(FinishReason.self, forKey: .finishReason)
  241. citationMetadata = try container.decodeIfPresent(
  242. CitationMetadata.self,
  243. forKey: .citationMetadata
  244. )
  245. }
  246. }
  247. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  248. extension CitationMetadata: Decodable {
  249. enum CodingKeys: String, CodingKey {
  250. case citationSources = "citations"
  251. }
  252. }
  253. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  254. extension Citation: Decodable {
  255. enum CodingKeys: CodingKey {
  256. case startIndex
  257. case endIndex
  258. case uri
  259. case title
  260. case license
  261. }
  262. public init(from decoder: any Decoder) throws {
  263. let container = try decoder.container(keyedBy: CodingKeys.self)
  264. startIndex = try container.decodeIfPresent(Int.self, forKey: .startIndex) ?? 0
  265. endIndex = try container.decode(Int.self, forKey: .endIndex)
  266. if let uri = try container.decodeIfPresent(String.self, forKey: .uri), !uri.isEmpty {
  267. self.uri = uri
  268. } else {
  269. uri = nil
  270. }
  271. if let title = try container.decodeIfPresent(String.self, forKey: .title), !title.isEmpty {
  272. self.title = title
  273. } else {
  274. title = nil
  275. }
  276. if let license = try container.decodeIfPresent(String.self, forKey: .license),
  277. !license.isEmpty {
  278. self.license = license
  279. } else {
  280. license = nil
  281. }
  282. }
  283. }
  284. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  285. extension FinishReason: Decodable {
  286. public init(from decoder: Decoder) throws {
  287. let value = try decoder.singleValueContainer().decode(String.self)
  288. guard let decodedFinishReason = FinishReason(rawValue: value) else {
  289. Logging.default
  290. .error("[FirebaseVertexAI] Unrecognized FinishReason with value \"\(value)\".")
  291. self = .unknown
  292. return
  293. }
  294. self = decodedFinishReason
  295. }
  296. }
  297. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  298. extension PromptFeedback.BlockReason: Decodable {
  299. public init(from decoder: Decoder) throws {
  300. let value = try decoder.singleValueContainer().decode(String.self)
  301. guard let decodedBlockReason = PromptFeedback.BlockReason(rawValue: value) else {
  302. Logging.default
  303. .error("[FirebaseVertexAI] Unrecognized BlockReason with value \"\(value)\".")
  304. self = .unknown
  305. return
  306. }
  307. self = decodedBlockReason
  308. }
  309. }
  310. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  311. extension PromptFeedback: Decodable {
  312. enum CodingKeys: CodingKey {
  313. case blockReason
  314. case safetyRatings
  315. }
  316. public init(from decoder: Decoder) throws {
  317. let container = try decoder.container(keyedBy: CodingKeys.self)
  318. blockReason = try container.decodeIfPresent(
  319. PromptFeedback.BlockReason.self,
  320. forKey: .blockReason
  321. )
  322. if let safetyRatings = try container.decodeIfPresent(
  323. [SafetyRating].self,
  324. forKey: .safetyRatings
  325. ) {
  326. self.safetyRatings = safetyRatings
  327. } else {
  328. safetyRatings = []
  329. }
  330. }
  331. }