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 blockReasonMessage to PromptFeedback #13891

Merged
merged 2 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
2 changes: 2 additions & 0 deletions FirebaseVertexAI/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
`.spii` and `.malformedFunctionCall` that may be reported. (#13860)
- [added] Added new `BlockReason` values `.blocklist` and `.prohibitedContent`
that may be reported when a prompt is blocked. (#13861)
- [added] Added the `PromptFeedback` property `blockReasonMessage` that *may* be
provided alongside the `blockReason`. (#13891)

# 11.3.0
- [added] Added `Decodable` conformance for `FunctionResponse`. (#13606)
Expand Down
9 changes: 8 additions & 1 deletion FirebaseVertexAI/Sources/GenerateContentResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,17 @@ public struct PromptFeedback: Sendable {
/// The reason a prompt was blocked, if it was blocked.
public let blockReason: BlockReason?

/// A human-readable description of the ``blockReason``.
public let blockReasonMessage: String?

/// The safety ratings of the prompt.
public let safetyRatings: [SafetyRating]

/// Initializer for SwiftUI previews or tests.
public init(blockReason: BlockReason?, safetyRatings: [SafetyRating]) {
public init(blockReason: BlockReason?, blockReasonMessage: String? = nil,
safetyRatings: [SafetyRating]) {
self.blockReason = blockReason
self.blockReasonMessage = blockReasonMessage
self.safetyRatings = safetyRatings
}
}
Expand Down Expand Up @@ -387,6 +392,7 @@ extension Citation: Decodable {
extension PromptFeedback: Decodable {
enum CodingKeys: CodingKey {
case blockReason
case blockReasonMessage
case safetyRatings
}

Expand All @@ -396,6 +402,7 @@ extension PromptFeedback: Decodable {
PromptFeedback.BlockReason.self,
forKey: .blockReason
)
blockReasonMessage = try container.decodeIfPresent(String.self, forKey: .blockReasonMessage)
if let safetyRatings = try container.decodeIfPresent(
[SafetyRating].self,
forKey: .safetyRatings
Expand Down
49 changes: 48 additions & 1 deletion FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,29 @@ final class GenerativeModelTests: XCTestCase {
XCTFail("Should throw")
} catch let GenerateContentError.promptBlocked(response) {
XCTAssertNil(response.text)
let promptFeedback = try XCTUnwrap(response.promptFeedback)
XCTAssertEqual(promptFeedback.blockReason, PromptFeedback.BlockReason.safety)
XCTAssertNil(promptFeedback.blockReasonMessage)
} catch {
XCTFail("Should throw a promptBlocked")
}
}

func testGenerateContent_failure_promptBlockedSafetyWithMessage() async throws {
MockURLProtocol
.requestHandler = try httpRequestHandler(
forResource: "unary-failure-prompt-blocked-safety-with-message",
withExtension: "json"
)

do {
_ = try await model.generateContent(testPrompt)
XCTFail("Should throw")
} catch let GenerateContentError.promptBlocked(response) {
XCTAssertNil(response.text)
let promptFeedback = try XCTUnwrap(response.promptFeedback)
XCTAssertEqual(promptFeedback.blockReason, PromptFeedback.BlockReason.safety)
XCTAssertEqual(promptFeedback.blockReasonMessage, "Reasons")
} catch {
XCTFail("Should throw a promptBlocked")
}
Expand Down Expand Up @@ -909,7 +932,31 @@ final class GenerativeModelTests: XCTestCase {
XCTFail("Content shouldn't be shown, this shouldn't happen.")
}
} catch let GenerateContentError.promptBlocked(response) {
XCTAssertEqual(response.promptFeedback?.blockReason, .safety)
let promptFeedback = try XCTUnwrap(response.promptFeedback)
XCTAssertEqual(promptFeedback.blockReason, .safety)
XCTAssertNil(promptFeedback.blockReasonMessage)
return
}

XCTFail("Should have caught an error.")
}

func testGenerateContentStream_failurePromptBlockedSafetyWithMessage() async throws {
MockURLProtocol
.requestHandler = try httpRequestHandler(
forResource: "streaming-failure-prompt-blocked-safety-with-message",
withExtension: "txt"
)

do {
let stream = try model.generateContentStream("Hi")
for try await _ in stream {
XCTFail("Content shouldn't be shown, this shouldn't happen.")
}
} catch let GenerateContentError.promptBlocked(response) {
let promptFeedback = try XCTUnwrap(response.promptFeedback)
XCTAssertEqual(promptFeedback.blockReason, .safety)
XCTAssertEqual(promptFeedback.blockReasonMessage, "Reasons")
return
}

Expand Down
Loading