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