Skip to content

Commit 6049f5c

Browse files
committed
[Vertex AI] Use struct instead of enum for HarmCategory
1 parent 5ed86cd commit 6049f5c

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

FirebaseVertexAI/Sample/ChatSample/Views/ErrorDetailsView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ extension HarmCategory: CustomStringConvertible {
2323
case .harassment: "Harassment"
2424
case .hateSpeech: "Hate speech"
2525
case .sexuallyExplicit: "Sexually explicit"
26-
case .unknown: "Unknown"
26+
default:
27+
"Unknown"
2728
}
2829
}
2930
}

FirebaseVertexAI/Sources/Safety.swift

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,44 @@ public struct SafetySetting {
9797
}
9898

9999
/// Categories describing the potential harm a piece of content may pose.
100-
public enum HarmCategory: String, Sendable {
100+
public struct HarmCategory: Sendable, Equatable, Hashable {
101101
/// Unknown. A new server value that isn't recognized by the SDK.
102-
case unknown = "HARM_CATEGORY_UNKNOWN"
102+
public static var unknown: HarmCategory {
103+
return self.init(category: "HARM_CATEGORY_UNKNOWN")
104+
}
103105

104106
/// Harassment content.
105-
case harassment = "HARM_CATEGORY_HARASSMENT"
107+
public static var harassment: HarmCategory {
108+
return self.init(category: "HARM_CATEGORY_HARASSMENT")
109+
}
106110

107111
/// Negative or harmful comments targeting identity and/or protected attributes.
108-
case hateSpeech = "HARM_CATEGORY_HATE_SPEECH"
112+
public static var hateSpeech: HarmCategory {
113+
return self.init(category: "HARM_CATEGORY_HATE_SPEECH")
114+
}
109115

110116
/// Contains references to sexual acts or other lewd content.
111-
case sexuallyExplicit = "HARM_CATEGORY_SEXUALLY_EXPLICIT"
117+
public static var sexuallyExplicit: HarmCategory {
118+
return self.init(category: "HARM_CATEGORY_SEXUALLY_EXPLICIT")
119+
}
112120

113121
/// Promotes or enables access to harmful goods, services, or activities.
114-
case dangerousContent = "HARM_CATEGORY_DANGEROUS_CONTENT"
122+
public static var dangerousContent: HarmCategory {
123+
return self.init(category: "HARM_CATEGORY_DANGEROUS_CONTENT")
124+
}
125+
126+
static let allCategories = [
127+
HarmCategory.harassment.category,
128+
HarmCategory.hateSpeech.category,
129+
HarmCategory.sexuallyExplicit.category,
130+
HarmCategory.dangerousContent.category,
131+
]
132+
133+
let category: String
134+
135+
init(category: String) {
136+
self.category = category
137+
}
115138
}
116139

117140
// MARK: - Codable Conformances
@@ -139,17 +162,17 @@ extension SafetyRating: Decodable {}
139162
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
140163
extension HarmCategory: Codable {
141164
public init(from decoder: Decoder) throws {
142-
let value = try decoder.singleValueContainer().decode(String.self)
143-
guard let decodedCategory = HarmCategory(rawValue: value) else {
165+
let category = try decoder.singleValueContainer().decode(String.self)
166+
guard HarmCategory.allCategories.contains(category) else {
144167
VertexLog.error(
145168
code: .generateContentResponseUnrecognizedHarmCategory,
146-
"Unrecognized HarmCategory with value \"\(value)\"."
169+
"Unrecognized HarmCategory with value \"\(category)\"."
147170
)
148171
self = .unknown
149172
return
150173
}
151174

152-
self = decodedCategory
175+
self.init(category: category)
153176
}
154177
}
155178

FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,6 @@ struct AppCheckErrorFake: Error {}
14221422
extension SafetyRating: Swift.Comparable {
14231423
public static func < (lhs: FirebaseVertexAI.SafetyRating,
14241424
rhs: FirebaseVertexAI.SafetyRating) -> Bool {
1425-
return lhs.category.rawValue < rhs.category.rawValue
1425+
return lhs.category.category < rhs.category.category
14261426
}
14271427
}

0 commit comments

Comments
 (0)