GenerateContentResponse.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 12.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. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  20. public struct UsageMetadata: Sendable {
  21. /// The number of tokens in the request prompt.
  22. public let promptTokenCount: Int
  23. /// The total number of tokens across the generated response candidates.
  24. public let candidatesTokenCount: Int
  25. /// The number of tokens used by the model's internal "thinking" process.
  26. ///
  27. /// For models that support thinking (like Gemini 2.5 Pro and Flash), this represents the actual
  28. /// number of tokens consumed for reasoning before the model generated a response. For models
  29. /// that do not support thinking, this value will be `0`.
  30. ///
  31. /// When thinking is used, this count will be less than or equal to the `thinkingBudget` set in
  32. /// the ``ThinkingConfig``.
  33. public let thoughtsTokenCount: Int
  34. /// The total number of tokens in both the request and response.
  35. public let totalTokenCount: Int
  36. /// The breakdown, by modality, of how many tokens are consumed by the prompt
  37. public let promptTokensDetails: [ModalityTokenCount]
  38. /// The breakdown, by modality, of how many tokens are consumed by the candidates
  39. public let candidatesTokensDetails: [ModalityTokenCount]
  40. }
  41. /// A list of candidate response content, ordered from best to worst.
  42. public let candidates: [Candidate]
  43. /// A value containing the safety ratings for the response, or, if the request was blocked, a
  44. /// reason for blocking the request.
  45. public let promptFeedback: PromptFeedback?
  46. /// Token usage metadata for processing the generate content request.
  47. public let usageMetadata: UsageMetadata?
  48. /// The response's content as text, if it exists.
  49. public var text: String? {
  50. guard let candidate = candidates.first else {
  51. AILog.error(
  52. code: .generateContentResponseNoCandidates,
  53. "Could not get text from a response that had no candidates."
  54. )
  55. return nil
  56. }
  57. let textValues: [String] = candidate.content.parts.compactMap { part in
  58. switch part {
  59. case let textPart as TextPart:
  60. return textPart.text
  61. default:
  62. return nil
  63. }
  64. }
  65. guard textValues.count > 0 else {
  66. AILog.error(
  67. code: .generateContentResponseNoText,
  68. "Could not get a text part from the first candidate."
  69. )
  70. return nil
  71. }
  72. return textValues.joined(separator: " ")
  73. }
  74. /// Returns function calls found in any `Part`s of the first candidate of the response, if any.
  75. public var functionCalls: [FunctionCallPart] {
  76. guard let candidate = candidates.first else {
  77. return []
  78. }
  79. return candidate.content.parts.compactMap { part in
  80. switch part {
  81. case let functionCallPart as FunctionCallPart:
  82. return functionCallPart
  83. default:
  84. return nil
  85. }
  86. }
  87. }
  88. /// Returns inline data parts found in any `Part`s of the first candidate of the response, if any.
  89. public var inlineDataParts: [InlineDataPart] {
  90. guard let candidate = candidates.first else {
  91. AILog.error(code: .generateContentResponseNoCandidates, """
  92. Could not get inline data parts because the response has no candidates. The accessor only \
  93. checks the first candidate.
  94. """)
  95. return []
  96. }
  97. return candidate.content.parts.compactMap { $0 as? InlineDataPart }
  98. }
  99. /// Initializer for SwiftUI previews or tests.
  100. public init(candidates: [Candidate], promptFeedback: PromptFeedback? = nil,
  101. usageMetadata: UsageMetadata? = nil) {
  102. self.candidates = candidates
  103. self.promptFeedback = promptFeedback
  104. self.usageMetadata = usageMetadata
  105. }
  106. }
  107. /// A struct representing a possible reply to a content generation prompt. Each content generation
  108. /// prompt may produce multiple candidate responses.
  109. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  110. public struct Candidate: Sendable {
  111. /// The response's content.
  112. public let content: ModelContent
  113. /// The safety rating of the response content.
  114. public let safetyRatings: [SafetyRating]
  115. /// The reason the model stopped generating content, if it exists; for example, if the model
  116. /// generated a predefined stop sequence.
  117. public let finishReason: FinishReason?
  118. /// Cited works in the model's response content, if it exists.
  119. public let citationMetadata: CitationMetadata?
  120. /// Initializer for SwiftUI previews or tests.
  121. public init(content: ModelContent, safetyRatings: [SafetyRating], finishReason: FinishReason?,
  122. citationMetadata: CitationMetadata?) {
  123. self.content = content
  124. self.safetyRatings = safetyRatings
  125. self.finishReason = finishReason
  126. self.citationMetadata = citationMetadata
  127. }
  128. }
  129. /// A collection of source attributions for a piece of content.
  130. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  131. public struct CitationMetadata: Sendable {
  132. /// A list of individual cited sources and the parts of the content to which they apply.
  133. public let citations: [Citation]
  134. }
  135. /// A struct describing a source attribution.
  136. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  137. public struct Citation: Sendable, Equatable {
  138. /// The inclusive beginning of a sequence in a model response that derives from a cited source.
  139. public let startIndex: Int
  140. /// The exclusive end of a sequence in a model response that derives from a cited source.
  141. public let endIndex: Int
  142. /// A link to the cited source, if available.
  143. public let uri: String?
  144. /// The title of the cited source, if available.
  145. public let title: String?
  146. /// The license the cited source work is distributed under, if specified.
  147. public let license: String?
  148. /// The publication date of the cited source, if available.
  149. ///
  150. /// > Tip: `DateComponents` can be converted to a `Date` using the `date` computed property.
  151. public let publicationDate: DateComponents?
  152. init(startIndex: Int,
  153. endIndex: Int,
  154. uri: String? = nil,
  155. title: String? = nil,
  156. license: String? = nil,
  157. publicationDate: DateComponents? = nil) {
  158. self.startIndex = startIndex
  159. self.endIndex = endIndex
  160. self.uri = uri
  161. self.title = title
  162. self.license = license
  163. self.publicationDate = publicationDate
  164. }
  165. }
  166. /// A value enumerating possible reasons for a model to terminate a content generation request.
  167. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  168. public struct FinishReason: DecodableProtoEnum, Hashable, Sendable {
  169. enum Kind: String {
  170. case stop = "STOP"
  171. case maxTokens = "MAX_TOKENS"
  172. case safety = "SAFETY"
  173. case recitation = "RECITATION"
  174. case other = "OTHER"
  175. case blocklist = "BLOCKLIST"
  176. case prohibitedContent = "PROHIBITED_CONTENT"
  177. case spii = "SPII"
  178. case malformedFunctionCall = "MALFORMED_FUNCTION_CALL"
  179. }
  180. /// Natural stop point of the model or provided stop sequence.
  181. public static let stop = FinishReason(kind: .stop)
  182. /// The maximum number of tokens as specified in the request was reached.
  183. public static let maxTokens = FinishReason(kind: .maxTokens)
  184. /// The token generation was stopped because the response was flagged for safety reasons.
  185. ///
  186. /// > NOTE: When streaming, the ``Candidate/content`` will be empty if content filters blocked the
  187. /// > output.
  188. public static let safety = FinishReason(kind: .safety)
  189. /// The token generation was stopped because the response was flagged for unauthorized citations.
  190. public static let recitation = FinishReason(kind: .recitation)
  191. /// All other reasons that stopped token generation.
  192. public static let other = FinishReason(kind: .other)
  193. /// Token generation was stopped because the response contained forbidden terms.
  194. public static let blocklist = FinishReason(kind: .blocklist)
  195. /// Token generation was stopped because the response contained potentially prohibited content.
  196. public static let prohibitedContent = FinishReason(kind: .prohibitedContent)
  197. /// Token generation was stopped because of Sensitive Personally Identifiable Information (SPII).
  198. public static let spii = FinishReason(kind: .spii)
  199. /// Token generation was stopped because the function call generated by the model was invalid.
  200. public static let malformedFunctionCall = FinishReason(kind: .malformedFunctionCall)
  201. /// Returns the raw string representation of the `FinishReason` value.
  202. ///
  203. /// > Note: This value directly corresponds to the values in the [REST
  204. /// > API](https://cloud.google.com/vertex-ai/docs/reference/rest/v1beta1/GenerateContentResponse#FinishReason).
  205. public let rawValue: String
  206. static let unrecognizedValueMessageCode =
  207. AILog.MessageCode.generateContentResponseUnrecognizedFinishReason
  208. }
  209. /// A metadata struct containing any feedback the model had on the prompt it was provided.
  210. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  211. public struct PromptFeedback: Sendable {
  212. /// A type describing possible reasons to block a prompt.
  213. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  214. public struct BlockReason: DecodableProtoEnum, Hashable, Sendable {
  215. enum Kind: String {
  216. case safety = "SAFETY"
  217. case other = "OTHER"
  218. case blocklist = "BLOCKLIST"
  219. case prohibitedContent = "PROHIBITED_CONTENT"
  220. }
  221. /// The prompt was blocked because it was deemed unsafe.
  222. public static let safety = BlockReason(kind: .safety)
  223. /// All other block reasons.
  224. public static let other = BlockReason(kind: .other)
  225. /// The prompt was blocked because it contained terms from the terminology blocklist.
  226. public static let blocklist = BlockReason(kind: .blocklist)
  227. /// The prompt was blocked due to prohibited content.
  228. public static let prohibitedContent = BlockReason(kind: .prohibitedContent)
  229. /// Returns the raw string representation of the `BlockReason` value.
  230. ///
  231. /// > Note: This value directly corresponds to the values in the [REST
  232. /// > API](https://cloud.google.com/vertex-ai/docs/reference/rest/v1beta1/GenerateContentResponse#BlockedReason).
  233. public let rawValue: String
  234. static let unrecognizedValueMessageCode =
  235. AILog.MessageCode.generateContentResponseUnrecognizedBlockReason
  236. }
  237. /// The reason a prompt was blocked, if it was blocked.
  238. public let blockReason: BlockReason?
  239. /// A human-readable description of the ``blockReason``.
  240. public let blockReasonMessage: String?
  241. /// The safety ratings of the prompt.
  242. public let safetyRatings: [SafetyRating]
  243. /// Initializer for SwiftUI previews or tests.
  244. public init(blockReason: BlockReason?, blockReasonMessage: String? = nil,
  245. safetyRatings: [SafetyRating]) {
  246. self.blockReason = blockReason
  247. self.blockReasonMessage = blockReasonMessage
  248. self.safetyRatings = safetyRatings
  249. }
  250. }
  251. // MARK: - Codable Conformances
  252. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  253. extension GenerateContentResponse: Decodable {
  254. enum CodingKeys: CodingKey {
  255. case candidates
  256. case promptFeedback
  257. case usageMetadata
  258. }
  259. public init(from decoder: Decoder) throws {
  260. let container = try decoder.container(keyedBy: CodingKeys.self)
  261. guard container.contains(CodingKeys.candidates) || container
  262. .contains(CodingKeys.promptFeedback) else {
  263. let context = DecodingError.Context(
  264. codingPath: [],
  265. debugDescription: "Failed to decode GenerateContentResponse;" +
  266. " missing keys 'candidates' and 'promptFeedback'."
  267. )
  268. throw DecodingError.dataCorrupted(context)
  269. }
  270. if let candidates = try container.decodeIfPresent(
  271. [Candidate].self,
  272. forKey: .candidates
  273. ) {
  274. self.candidates = candidates
  275. } else {
  276. candidates = []
  277. }
  278. promptFeedback = try container.decodeIfPresent(PromptFeedback.self, forKey: .promptFeedback)
  279. usageMetadata = try container.decodeIfPresent(UsageMetadata.self, forKey: .usageMetadata)
  280. }
  281. }
  282. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  283. extension GenerateContentResponse.UsageMetadata: Decodable {
  284. enum CodingKeys: CodingKey {
  285. case promptTokenCount
  286. case candidatesTokenCount
  287. case thoughtsTokenCount
  288. case totalTokenCount
  289. case promptTokensDetails
  290. case candidatesTokensDetails
  291. }
  292. public init(from decoder: any Decoder) throws {
  293. let container = try decoder.container(keyedBy: CodingKeys.self)
  294. promptTokenCount = try container.decodeIfPresent(Int.self, forKey: .promptTokenCount) ?? 0
  295. candidatesTokenCount =
  296. try container.decodeIfPresent(Int.self, forKey: .candidatesTokenCount) ?? 0
  297. thoughtsTokenCount = try container.decodeIfPresent(Int.self, forKey: .thoughtsTokenCount) ?? 0
  298. totalTokenCount = try container.decodeIfPresent(Int.self, forKey: .totalTokenCount) ?? 0
  299. promptTokensDetails =
  300. try container.decodeIfPresent([ModalityTokenCount].self, forKey: .promptTokensDetails) ?? []
  301. candidatesTokensDetails = try container.decodeIfPresent(
  302. [ModalityTokenCount].self,
  303. forKey: .candidatesTokensDetails
  304. ) ?? []
  305. }
  306. }
  307. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  308. extension Candidate: Decodable {
  309. enum CodingKeys: CodingKey {
  310. case content
  311. case safetyRatings
  312. case finishReason
  313. case citationMetadata
  314. }
  315. /// Initializes a response from a decoder. Used for decoding server responses; not for public
  316. /// use.
  317. public init(from decoder: Decoder) throws {
  318. let container = try decoder.container(keyedBy: CodingKeys.self)
  319. do {
  320. if let content = try container.decodeIfPresent(ModelContent.self, forKey: .content) {
  321. self.content = content
  322. } else {
  323. content = ModelContent(parts: [])
  324. }
  325. } catch {
  326. throw InvalidCandidateError.malformedContent(underlyingError: error)
  327. }
  328. if let safetyRatings = try container.decodeIfPresent(
  329. [SafetyRating].self, forKey: .safetyRatings
  330. ) {
  331. self.safetyRatings = safetyRatings.filter {
  332. // Due to a bug in the backend, the SDK may receive invalid `SafetyRating` values that do
  333. // not include a category or probability; these are filtered out of the safety ratings.
  334. $0.category != HarmCategory.unspecified
  335. && $0.probability != SafetyRating.HarmProbability.unspecified
  336. }
  337. } else {
  338. safetyRatings = []
  339. }
  340. finishReason = try container.decodeIfPresent(FinishReason.self, forKey: .finishReason)
  341. // The `content` may only be empty if a `finishReason` is included; if neither are included in
  342. // the response then this is likely the `"content": {}` bug.
  343. guard !content.parts.isEmpty || finishReason != nil else {
  344. throw InvalidCandidateError.emptyContent(underlyingError: DecodingError.dataCorrupted(.init(
  345. codingPath: [CodingKeys.content, CodingKeys.finishReason],
  346. debugDescription: "Invalid Candidate: empty content and no finish reason"
  347. )))
  348. }
  349. citationMetadata = try container.decodeIfPresent(
  350. CitationMetadata.self,
  351. forKey: .citationMetadata
  352. )
  353. }
  354. }
  355. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  356. extension CitationMetadata: Decodable {
  357. enum CodingKeys: CodingKey {
  358. case citations // Vertex AI
  359. case citationSources // Google AI
  360. }
  361. public init(from decoder: any Decoder) throws {
  362. let container = try decoder.container(keyedBy: CodingKeys.self)
  363. // Decode for Google API if `citationSources` key is present.
  364. if container.contains(.citationSources) {
  365. citations = try container.decode([Citation].self, forKey: .citationSources)
  366. } else { // Fallback to default Vertex AI decoding.
  367. citations = try container.decode([Citation].self, forKey: .citations)
  368. }
  369. }
  370. }
  371. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  372. extension Citation: Decodable {
  373. enum CodingKeys: CodingKey {
  374. case startIndex
  375. case endIndex
  376. case uri
  377. case title
  378. case license
  379. case publicationDate
  380. }
  381. public init(from decoder: any Decoder) throws {
  382. let container = try decoder.container(keyedBy: CodingKeys.self)
  383. startIndex = try container.decodeIfPresent(Int.self, forKey: .startIndex) ?? 0
  384. endIndex = try container.decode(Int.self, forKey: .endIndex)
  385. if let uri = try container.decodeIfPresent(String.self, forKey: .uri), !uri.isEmpty {
  386. self.uri = uri
  387. } else {
  388. uri = nil
  389. }
  390. if let title = try container.decodeIfPresent(String.self, forKey: .title), !title.isEmpty {
  391. self.title = title
  392. } else {
  393. title = nil
  394. }
  395. if let license = try container.decodeIfPresent(String.self, forKey: .license),
  396. !license.isEmpty {
  397. self.license = license
  398. } else {
  399. license = nil
  400. }
  401. if let publicationProtoDate = try container.decodeIfPresent(
  402. ProtoDate.self,
  403. forKey: .publicationDate
  404. ) {
  405. publicationDate = publicationProtoDate.dateComponents
  406. if let publicationDate, !publicationDate.isValidDate {
  407. AILog.warning(
  408. code: .decodedInvalidCitationPublicationDate,
  409. "Decoded an invalid citation publication date: \(publicationDate)"
  410. )
  411. }
  412. } else {
  413. publicationDate = nil
  414. }
  415. }
  416. }
  417. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  418. extension PromptFeedback: Decodable {
  419. enum CodingKeys: CodingKey {
  420. case blockReason
  421. case blockReasonMessage
  422. case safetyRatings
  423. }
  424. public init(from decoder: Decoder) throws {
  425. let container = try decoder.container(keyedBy: CodingKeys.self)
  426. blockReason = try container.decodeIfPresent(
  427. PromptFeedback.BlockReason.self,
  428. forKey: .blockReason
  429. )
  430. blockReasonMessage = try container.decodeIfPresent(String.self, forKey: .blockReasonMessage)
  431. if let safetyRatings = try container.decodeIfPresent(
  432. [SafetyRating].self,
  433. forKey: .safetyRatings
  434. ) {
  435. self.safetyRatings = safetyRatings
  436. } else {
  437. safetyRatings = []
  438. }
  439. }
  440. }