GenerateContentResponse.swift 15 KB

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