GenerateContentResponse.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. VertexLog.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. VertexLog.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. /// Initializer for SwiftUI previews or tests.
  80. public init(candidates: [Candidate], promptFeedback: PromptFeedback? = nil,
  81. usageMetadata: UsageMetadata? = nil) {
  82. self.candidates = candidates
  83. self.promptFeedback = promptFeedback
  84. self.usageMetadata = usageMetadata
  85. }
  86. }
  87. /// A struct representing a possible reply to a content generation prompt. Each content generation
  88. /// prompt may produce multiple candidate responses.
  89. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  90. public struct Candidate: Sendable {
  91. /// The response's content.
  92. public let content: ModelContent
  93. /// The safety rating of the response content.
  94. public let safetyRatings: [SafetyRating]
  95. /// The reason the model stopped generating content, if it exists; for example, if the model
  96. /// generated a predefined stop sequence.
  97. public let finishReason: FinishReason?
  98. /// Cited works in the model's response content, if it exists.
  99. public let citationMetadata: CitationMetadata?
  100. /// Initializer for SwiftUI previews or tests.
  101. public init(content: ModelContent, safetyRatings: [SafetyRating], finishReason: FinishReason?,
  102. citationMetadata: CitationMetadata?) {
  103. self.content = content
  104. self.safetyRatings = safetyRatings
  105. self.finishReason = finishReason
  106. self.citationMetadata = citationMetadata
  107. }
  108. }
  109. /// A collection of source attributions for a piece of content.
  110. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  111. public struct CitationMetadata: Sendable {
  112. /// A list of individual cited sources and the parts of the content to which they apply.
  113. public let citations: [Citation]
  114. }
  115. /// A struct describing a source attribution.
  116. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  117. public struct Citation: Sendable {
  118. /// The inclusive beginning of a sequence in a model response that derives from a cited source.
  119. public let startIndex: Int
  120. /// The exclusive end of a sequence in a model response that derives from a cited source.
  121. public let endIndex: Int
  122. /// A link to the cited source, if available.
  123. public let uri: String?
  124. /// The title of the cited source, if available.
  125. public let title: String?
  126. /// The license the cited source work is distributed under, if specified.
  127. public let license: String?
  128. /// The publication date of the cited source, if available.
  129. ///
  130. /// > Tip: `DateComponents` can be converted to a `Date` using the `date` computed property.
  131. public let publicationDate: DateComponents?
  132. }
  133. /// A value enumerating possible reasons for a model to terminate a content generation request.
  134. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  135. public struct FinishReason: DecodableProtoEnum, Hashable, Sendable {
  136. enum Kind: String {
  137. case stop = "STOP"
  138. case maxTokens = "MAX_TOKENS"
  139. case safety = "SAFETY"
  140. case recitation = "RECITATION"
  141. case other = "OTHER"
  142. case blocklist = "BLOCKLIST"
  143. case prohibitedContent = "PROHIBITED_CONTENT"
  144. case spii = "SPII"
  145. case malformedFunctionCall = "MALFORMED_FUNCTION_CALL"
  146. }
  147. /// Natural stop point of the model or provided stop sequence.
  148. public static let stop = FinishReason(kind: .stop)
  149. /// The maximum number of tokens as specified in the request was reached.
  150. public static let maxTokens = FinishReason(kind: .maxTokens)
  151. /// The token generation was stopped because the response was flagged for safety reasons.
  152. ///
  153. /// > NOTE: When streaming, the ``Candidate/content`` will be empty if content filters blocked the
  154. /// > output.
  155. public static let safety = FinishReason(kind: .safety)
  156. /// The token generation was stopped because the response was flagged for unauthorized citations.
  157. public static let recitation = FinishReason(kind: .recitation)
  158. /// All other reasons that stopped token generation.
  159. public static let other = FinishReason(kind: .other)
  160. /// Token generation was stopped because the response contained forbidden terms.
  161. public static let blocklist = FinishReason(kind: .blocklist)
  162. /// Token generation was stopped because the response contained potentially prohibited content.
  163. public static let prohibitedContent = FinishReason(kind: .prohibitedContent)
  164. /// Token generation was stopped because of Sensitive Personally Identifiable Information (SPII).
  165. public static let spii = FinishReason(kind: .spii)
  166. /// Token generation was stopped because the function call generated by the model was invalid.
  167. public static let malformedFunctionCall = FinishReason(kind: .malformedFunctionCall)
  168. /// Returns the raw string representation of the `FinishReason` value.
  169. ///
  170. /// > Note: This value directly corresponds to the values in the [REST
  171. /// > API](https://cloud.google.com/vertex-ai/docs/reference/rest/v1beta1/GenerateContentResponse#FinishReason).
  172. public let rawValue: String
  173. static let unrecognizedValueMessageCode =
  174. VertexLog.MessageCode.generateContentResponseUnrecognizedFinishReason
  175. }
  176. /// A metadata struct containing any feedback the model had on the prompt it was provided.
  177. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  178. public struct PromptFeedback: Sendable {
  179. /// A type describing possible reasons to block a prompt.
  180. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  181. public struct BlockReason: DecodableProtoEnum, Hashable, Sendable {
  182. enum Kind: String {
  183. case safety = "SAFETY"
  184. case other = "OTHER"
  185. case blocklist = "BLOCKLIST"
  186. case prohibitedContent = "PROHIBITED_CONTENT"
  187. }
  188. /// The prompt was blocked because it was deemed unsafe.
  189. public static let safety = BlockReason(kind: .safety)
  190. /// All other block reasons.
  191. public static let other = BlockReason(kind: .other)
  192. /// The prompt was blocked because it contained terms from the terminology blocklist.
  193. public static let blocklist = BlockReason(kind: .blocklist)
  194. /// The prompt was blocked due to prohibited content.
  195. public static let prohibitedContent = BlockReason(kind: .prohibitedContent)
  196. /// Returns the raw string representation of the `BlockReason` value.
  197. ///
  198. /// > Note: This value directly corresponds to the values in the [REST
  199. /// > API](https://cloud.google.com/vertex-ai/docs/reference/rest/v1beta1/GenerateContentResponse#BlockedReason).
  200. public let rawValue: String
  201. static let unrecognizedValueMessageCode =
  202. VertexLog.MessageCode.generateContentResponseUnrecognizedBlockReason
  203. }
  204. /// The reason a prompt was blocked, if it was blocked.
  205. public let blockReason: BlockReason?
  206. /// A human-readable description of the ``blockReason``.
  207. public let blockReasonMessage: String?
  208. /// The safety ratings of the prompt.
  209. public let safetyRatings: [SafetyRating]
  210. /// Initializer for SwiftUI previews or tests.
  211. public init(blockReason: BlockReason?, blockReasonMessage: String? = nil,
  212. safetyRatings: [SafetyRating]) {
  213. self.blockReason = blockReason
  214. self.blockReasonMessage = blockReasonMessage
  215. self.safetyRatings = safetyRatings
  216. }
  217. }
  218. // MARK: - Codable Conformances
  219. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  220. extension GenerateContentResponse: Decodable {
  221. enum CodingKeys: CodingKey {
  222. case candidates
  223. case promptFeedback
  224. case usageMetadata
  225. }
  226. public init(from decoder: Decoder) throws {
  227. let container = try decoder.container(keyedBy: CodingKeys.self)
  228. guard container.contains(CodingKeys.candidates) || container
  229. .contains(CodingKeys.promptFeedback) else {
  230. let context = DecodingError.Context(
  231. codingPath: [],
  232. debugDescription: "Failed to decode GenerateContentResponse;" +
  233. " missing keys 'candidates' and 'promptFeedback'."
  234. )
  235. throw DecodingError.dataCorrupted(context)
  236. }
  237. if let candidates = try container.decodeIfPresent(
  238. [Candidate].self,
  239. forKey: .candidates
  240. ) {
  241. self.candidates = candidates
  242. } else {
  243. candidates = []
  244. }
  245. promptFeedback = try container.decodeIfPresent(PromptFeedback.self, forKey: .promptFeedback)
  246. usageMetadata = try container.decodeIfPresent(UsageMetadata.self, forKey: .usageMetadata)
  247. }
  248. }
  249. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  250. extension GenerateContentResponse.UsageMetadata: Decodable {
  251. enum CodingKeys: CodingKey {
  252. case promptTokenCount
  253. case candidatesTokenCount
  254. case totalTokenCount
  255. case promptTokensDetails
  256. case candidatesTokensDetails
  257. }
  258. public init(from decoder: any Decoder) throws {
  259. let container = try decoder.container(keyedBy: CodingKeys.self)
  260. promptTokenCount = try container.decodeIfPresent(Int.self, forKey: .promptTokenCount) ?? 0
  261. candidatesTokenCount =
  262. try container.decodeIfPresent(Int.self, forKey: .candidatesTokenCount) ?? 0
  263. totalTokenCount = try container.decodeIfPresent(Int.self, forKey: .totalTokenCount) ?? 0
  264. promptTokensDetails =
  265. try container.decodeIfPresent([ModalityTokenCount].self, forKey: .promptTokensDetails) ?? []
  266. candidatesTokensDetails = try container.decodeIfPresent(
  267. [ModalityTokenCount].self,
  268. forKey: .candidatesTokensDetails
  269. ) ?? []
  270. }
  271. }
  272. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  273. extension Candidate: Decodable {
  274. enum CodingKeys: CodingKey {
  275. case content
  276. case safetyRatings
  277. case finishReason
  278. case citationMetadata
  279. }
  280. /// Initializes a response from a decoder. Used for decoding server responses; not for public
  281. /// use.
  282. public init(from decoder: Decoder) throws {
  283. let container = try decoder.container(keyedBy: CodingKeys.self)
  284. do {
  285. if let content = try container.decodeIfPresent(ModelContent.self, forKey: .content) {
  286. self.content = content
  287. } else {
  288. content = ModelContent(parts: [])
  289. }
  290. } catch {
  291. // Check if `content` can be decoded as an empty dictionary to detect the `"content": {}` bug.
  292. if let content = try? container.decode([String: String].self, forKey: .content),
  293. content.isEmpty {
  294. throw InvalidCandidateError.emptyContent(underlyingError: error)
  295. } else {
  296. throw InvalidCandidateError.malformedContent(underlyingError: error)
  297. }
  298. }
  299. if let safetyRatings = try container.decodeIfPresent(
  300. [SafetyRating].self,
  301. forKey: .safetyRatings
  302. ) {
  303. self.safetyRatings = safetyRatings
  304. } else {
  305. safetyRatings = []
  306. }
  307. finishReason = try container.decodeIfPresent(FinishReason.self, forKey: .finishReason)
  308. citationMetadata = try container.decodeIfPresent(
  309. CitationMetadata.self,
  310. forKey: .citationMetadata
  311. )
  312. }
  313. }
  314. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  315. extension CitationMetadata: Decodable {}
  316. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  317. extension Citation: Decodable {
  318. enum CodingKeys: CodingKey {
  319. case startIndex
  320. case endIndex
  321. case uri
  322. case title
  323. case license
  324. case publicationDate
  325. }
  326. public init(from decoder: any Decoder) throws {
  327. let container = try decoder.container(keyedBy: CodingKeys.self)
  328. startIndex = try container.decodeIfPresent(Int.self, forKey: .startIndex) ?? 0
  329. endIndex = try container.decode(Int.self, forKey: .endIndex)
  330. if let uri = try container.decodeIfPresent(String.self, forKey: .uri), !uri.isEmpty {
  331. self.uri = uri
  332. } else {
  333. uri = nil
  334. }
  335. if let title = try container.decodeIfPresent(String.self, forKey: .title), !title.isEmpty {
  336. self.title = title
  337. } else {
  338. title = nil
  339. }
  340. if let license = try container.decodeIfPresent(String.self, forKey: .license),
  341. !license.isEmpty {
  342. self.license = license
  343. } else {
  344. license = nil
  345. }
  346. if let publicationProtoDate = try container.decodeIfPresent(
  347. ProtoDate.self,
  348. forKey: .publicationDate
  349. ) {
  350. publicationDate = publicationProtoDate.dateComponents
  351. if let publicationDate, !publicationDate.isValidDate {
  352. VertexLog.warning(
  353. code: .decodedInvalidCitationPublicationDate,
  354. "Decoded an invalid citation publication date: \(publicationDate)"
  355. )
  356. }
  357. } else {
  358. publicationDate = nil
  359. }
  360. }
  361. }
  362. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  363. extension PromptFeedback: Decodable {
  364. enum CodingKeys: CodingKey {
  365. case blockReason
  366. case blockReasonMessage
  367. case safetyRatings
  368. }
  369. public init(from decoder: Decoder) throws {
  370. let container = try decoder.container(keyedBy: CodingKeys.self)
  371. blockReason = try container.decodeIfPresent(
  372. PromptFeedback.BlockReason.self,
  373. forKey: .blockReason
  374. )
  375. blockReasonMessage = try container.decodeIfPresent(String.self, forKey: .blockReasonMessage)
  376. if let safetyRatings = try container.decodeIfPresent(
  377. [SafetyRating].self,
  378. forKey: .safetyRatings
  379. ) {
  380. self.safetyRatings = safetyRatings
  381. } else {
  382. safetyRatings = []
  383. }
  384. }
  385. }