GenerateContentResponse.swift 18 KB

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