Safety.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /// A type defining potentially harmful media categories and their model-assigned ratings. A value
  16. /// of this type may be assigned to a category for every model-generated response, not just
  17. /// responses that exceed a certain threshold.
  18. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, *)
  19. public struct SafetyRating: Equatable, Hashable {
  20. /// The category describing the potential harm a piece of content may pose. See
  21. /// ``SafetySetting/HarmCategory`` for a list of possible values.
  22. public let category: SafetySetting.HarmCategory
  23. /// The model-generated probability that a given piece of content falls under the harm category
  24. /// described in ``SafetySetting/HarmCategory``. This does not indicate the severity of harm for a
  25. /// piece of content. See ``HarmProbability`` for a list of possible values.
  26. public let probability: HarmProbability
  27. /// Initializes a new `SafetyRating` instance with the given category and probability.
  28. /// Use this initializer for SwiftUI previews or tests.
  29. public init(category: SafetySetting.HarmCategory, probability: HarmProbability) {
  30. self.category = category
  31. self.probability = probability
  32. }
  33. /// The probability that a given model output falls under a harmful content category. This does
  34. /// not indicate the severity of harm for a piece of content.
  35. public enum HarmProbability: String {
  36. /// Unknown. A new server value that isn't recognized by the SDK.
  37. case unknown = "UNKNOWN"
  38. /// The probability was not specified in the server response.
  39. case unspecified = "HARM_PROBABILITY_UNSPECIFIED"
  40. /// The probability is zero or close to zero. For benign content, the probability across all
  41. /// categories will be this value.
  42. case negligible = "NEGLIGIBLE"
  43. /// The probability is small but non-zero.
  44. case low = "LOW"
  45. /// The probability is moderate.
  46. case medium = "MEDIUM"
  47. /// The probability is high. The content described is very likely harmful.
  48. case high = "HIGH"
  49. }
  50. }
  51. /// Safety feedback for an entire request.
  52. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, *)
  53. public struct SafetyFeedback {
  54. /// Safety rating evaluated from content.
  55. public let rating: SafetyRating
  56. /// Safety settings applied to the request.
  57. public let setting: SafetySetting
  58. /// Internal initializer.
  59. init(rating: SafetyRating, setting: SafetySetting) {
  60. self.rating = rating
  61. self.setting = setting
  62. }
  63. }
  64. /// A type used to specify a threshold for harmful content, beyond which the model will return a
  65. /// fallback response instead of generated content.
  66. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, *)
  67. public struct SafetySetting {
  68. /// A type describing safety attributes, which include harmful categories and topics that can
  69. /// be considered sensitive.
  70. public enum HarmCategory: String {
  71. /// Unknown. A new server value that isn't recognized by the SDK.
  72. case unknown = "HARM_CATEGORY_UNKNOWN"
  73. /// Unspecified by the server.
  74. case unspecified = "HARM_CATEGORY_UNSPECIFIED"
  75. /// Harassment content.
  76. case harassment = "HARM_CATEGORY_HARASSMENT"
  77. /// Negative or harmful comments targeting identity and/or protected attributes.
  78. case hateSpeech = "HARM_CATEGORY_HATE_SPEECH"
  79. /// Contains references to sexual acts or other lewd content.
  80. case sexuallyExplicit = "HARM_CATEGORY_SEXUALLY_EXPLICIT"
  81. /// Promotes or enables access to harmful goods, services, or activities.
  82. case dangerousContent = "HARM_CATEGORY_DANGEROUS_CONTENT"
  83. }
  84. /// Block at and beyond a specified ``SafetyRating/HarmProbability``.
  85. public enum BlockThreshold: String {
  86. /// Unknown. A new server value that isn't recognized by the SDK.
  87. case unknown = "UNKNOWN"
  88. /// Threshold is unspecified.
  89. case unspecified = "HARM_BLOCK_THRESHOLD_UNSPECIFIED"
  90. // Content with `.negligible` will be allowed.
  91. case blockLowAndAbove = "BLOCK_LOW_AND_ABOVE"
  92. /// Content with `.negligible` and `.low` will be allowed.
  93. case blockMediumAndAbove = "BLOCK_MEDIUM_AND_ABOVE"
  94. /// Content with `.negligible`, `.low`, and `.medium` will be allowed.
  95. case blockOnlyHigh = "BLOCK_ONLY_HIGH"
  96. /// All content will be allowed.
  97. case blockNone = "BLOCK_NONE"
  98. }
  99. enum CodingKeys: String, CodingKey {
  100. case harmCategory = "category"
  101. case threshold
  102. }
  103. /// The category this safety setting should be applied to.
  104. public let harmCategory: HarmCategory
  105. /// The threshold describing what content should be blocked.
  106. public let threshold: BlockThreshold
  107. /// Initializes a new safety setting with the given category and threshold.
  108. public init(harmCategory: HarmCategory, threshold: BlockThreshold) {
  109. self.harmCategory = harmCategory
  110. self.threshold = threshold
  111. }
  112. }
  113. // MARK: - Codable Conformances
  114. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, *)
  115. extension SafetyRating.HarmProbability: Codable {
  116. public init(from decoder: Decoder) throws {
  117. let value = try decoder.singleValueContainer().decode(String.self)
  118. guard let decodedProbability = SafetyRating.HarmProbability(rawValue: value) else {
  119. Logging.default
  120. .error("[FirebaseVertexAI] Unrecognized HarmProbability with value \"\(value)\".")
  121. self = .unknown
  122. return
  123. }
  124. self = decodedProbability
  125. }
  126. }
  127. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, *)
  128. extension SafetyRating: Decodable {}
  129. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, *)
  130. extension SafetyFeedback: Decodable {}
  131. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, *)
  132. extension SafetySetting.HarmCategory: Codable {
  133. public init(from decoder: Decoder) throws {
  134. let value = try decoder.singleValueContainer().decode(String.self)
  135. guard let decodedCategory = SafetySetting.HarmCategory(rawValue: value) else {
  136. Logging.default
  137. .error("[FirebaseVertexAI] Unrecognized HarmCategory with value \"\(value)\".")
  138. self = .unknown
  139. return
  140. }
  141. self = decodedCategory
  142. }
  143. }
  144. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, *)
  145. extension SafetySetting.BlockThreshold: Codable {
  146. public init(from decoder: Decoder) throws {
  147. let value = try decoder.singleValueContainer().decode(String.self)
  148. guard let decodedThreshold = SafetySetting.BlockThreshold(rawValue: value) else {
  149. Logging.default
  150. .error("[FirebaseVertexAI] Unrecognized BlockThreshold with value \"\(value)\".")
  151. self = .unknown
  152. return
  153. }
  154. self = decodedThreshold
  155. }
  156. }
  157. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, *)
  158. extension SafetySetting: Codable {}