GenerateContentResponse.swift 19 KB

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