Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vertex AI] Add HarmBlockMethod enum and method property #13876

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions FirebaseVertexAI/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
(#13875)
- [added] Added a new `HarmBlockThreshold` `.off`, which turns off the safety
filter. (#13863)
- [added] Added an optional `HarmBlockMethod` parameter `method` in
`SafetySetting` that configures whether responses are blocked based on the
`probability` and/or `severity` of content being in a `HarmCategory`. (#13876)
- [added] Added new `FinishReason` values `.blocklist`, `.prohibitedContent`,
`.spii` and `.malformedFunctionCall` that may be reported. (#13860)
- [added] Added new `BlockReason` values `.blocklist` and `.prohibitedContent`
Expand Down
34 changes: 33 additions & 1 deletion FirebaseVertexAI/Sources/Safety.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,26 @@ public struct SafetySetting {
let rawValue: String
}

/// The method of computing whether the ``SafetySetting/HarmBlockThreshold`` has been exceeded.
public struct HarmBlockMethod: EncodableProtoEnum, Sendable {
enum Kind: String {
case severity = "SEVERITY"
case probability = "PROBABILITY"
}

/// Use both probability and severity scores.
public static let severity = HarmBlockMethod(kind: .severity)

/// Use only the probability score.
public static let probability = HarmBlockMethod(kind: .probability)

let rawValue: String
}

enum CodingKeys: String, CodingKey {
case harmCategory = "category"
case threshold
case method
}

/// The category this safety setting should be applied to.
Expand All @@ -184,10 +201,25 @@ public struct SafetySetting {
/// The threshold describing what content should be blocked.
public let threshold: HarmBlockThreshold

/// The method of computing whether the ``threshold`` has been exceeded.
public let method: HarmBlockMethod?

/// Initializes a new safety setting with the given category and threshold.
public init(harmCategory: HarmCategory, threshold: HarmBlockThreshold) {
///
/// - Parameters:
/// - harmCategory: The category this safety setting should be applied to.
/// - threshold: The threshold describing what content should be blocked.
/// - method: The method of computing whether the threshold has been exceeded; if not specified,
/// the default method is ``HarmBlockMethod/severity`` for most models. See [harm block
/// methods](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/configure-safety-filters#how_to_configure_safety_filters)
/// in the Google Cloud documentation for more details.
/// > Note: For models older than `gemini-1.5-flash` and `gemini-1.5-pro`, the default method
/// > is ``HarmBlockMethod/probability``.
public init(harmCategory: HarmCategory, threshold: HarmBlockThreshold,
method: HarmBlockMethod? = nil) {
self.harmCategory = harmCategory
self.threshold = threshold
self.method = method
}
}

Expand Down
8 changes: 4 additions & 4 deletions FirebaseVertexAI/Tests/Integration/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ final class IntegrationTests: XCTestCase {
parts: "You are a friendly and helpful assistant."
)
let safetySettings = [
SafetySetting(harmCategory: .harassment, threshold: .blockLowAndAbove),
SafetySetting(harmCategory: .hateSpeech, threshold: .blockLowAndAbove),
SafetySetting(harmCategory: .harassment, threshold: .blockLowAndAbove, method: .probability),
SafetySetting(harmCategory: .hateSpeech, threshold: .blockLowAndAbove, method: .severity),
SafetySetting(harmCategory: .sexuallyExplicit, threshold: .blockLowAndAbove),
SafetySetting(harmCategory: .dangerousContent, threshold: .blockLowAndAbove),
SafetySetting(harmCategory: .civicIntegrity, threshold: .blockLowAndAbove),
Expand Down Expand Up @@ -89,11 +89,11 @@ final class IntegrationTests: XCTestCase {
modelName: "gemini-1.5-pro",
generationConfig: generationConfig,
safetySettings: [
SafetySetting(harmCategory: .harassment, threshold: .blockLowAndAbove),
SafetySetting(harmCategory: .harassment, threshold: .blockLowAndAbove, method: .severity),
SafetySetting(harmCategory: .hateSpeech, threshold: .blockMediumAndAbove),
SafetySetting(harmCategory: .sexuallyExplicit, threshold: .blockOnlyHigh),
SafetySetting(harmCategory: .dangerousContent, threshold: .blockNone),
SafetySetting(harmCategory: .civicIntegrity, threshold: .off),
SafetySetting(harmCategory: .civicIntegrity, threshold: .off, method: .probability),
],
toolConfig: .init(functionCallingConfig: .auto()),
systemInstruction: systemInstruction
Expand Down
Loading