GenerateContentResponse.swift 14 KB

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