Skip to content

[Firebase AI] Return default proto values with $outputDefaults #14784

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

Closed
wants to merge 4 commits into from
Closed
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
16 changes: 9 additions & 7 deletions FirebaseAI/Sources/GenerateContentResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public struct Citation: Sendable {
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public struct FinishReason: DecodableProtoEnum, Hashable, Sendable {
enum Kind: String {
case unspecified = "FINISH_REASON_UNSPECIFIED"
case stop = "STOP"
case maxTokens = "MAX_TOKENS"
case safety = "SAFETY"
Expand Down Expand Up @@ -366,16 +367,17 @@ extension Candidate: Decodable {
}
}

if let safetyRatings = try container.decodeIfPresent(
[SafetyRating].self,
forKey: .safetyRatings
) {
self.safetyRatings = safetyRatings
} else {
safetyRatings = []
let safetyRatings = try container
.decodeIfPresent([SafetyRating].self, forKey: .safetyRatings) ?? []
// Filter out safety ratings where the category and probability are both unspecified by the
// backend.
self.safetyRatings = safetyRatings.filter {
$0.category.rawValue != HarmCategory.Kind.unspecified.rawValue
&& $0.probability.rawValue != SafetyRating.HarmProbability.Kind.unspecified.rawValue
}

finishReason = try container.decodeIfPresent(FinishReason.self, forKey: .finishReason)
.flatMap { $0.rawValue == FinishReason.Kind.unspecified.rawValue ? nil : $0 }

citationMetadata = try container.decodeIfPresent(
CitationMetadata.self,
Expand Down
25 changes: 24 additions & 1 deletion FirebaseAI/Sources/GenerativeAIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ struct GenerativeAIService {
// MARK: - Private Helpers

private func urlRequest<T: GenerativeAIRequest>(request: T) async throws -> URLRequest {
var urlRequest = URLRequest(url: request.url)
var urlRequest = try URLRequest(url: requestURL(request: request))
urlRequest.httpMethod = "POST"
urlRequest.setValue(firebaseInfo.apiKey, forHTTPHeaderField: "x-goog-api-key")
urlRequest.setValue(
Expand Down Expand Up @@ -207,6 +207,29 @@ struct GenerativeAIService {
return urlRequest
}

private func requestURL<T: GenerativeAIRequest>(request: T) throws -> URL {
guard var urlComponents = URLComponents(url: request.url, resolvingAgainstBaseURL: false) else {
throw URLError(.badURL, userInfo: [
NSLocalizedDescriptionKey: "Invalid Request URL: \(request.url)",
])
}
var urlQueryItems = urlComponents.queryItems ?? []

// The query parameter `$outputDefaults` forces the backend to output proto default values for
// JSON responses instead of omitting them. See
// https://cloud.google.com/apis/docs/system-parameters#definitions for more details.
urlQueryItems.append(URLQueryItem(name: "$outputDefaults", value: "true"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this increase cost?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only in terms of bandwidth since the JSON payload will be larger in cases where defaults were previously omitted.

Note: Google AI actually behaves this way by default (default values have always been included in the JSON). Vertex AI always omits them unless this option is specified.

This has also caused some confusion in the past since, for example, blocked: false was omitted in Vertex since false is the default. Other cases tend to revolve around 0 getting omitted for numeric fields.


urlComponents.queryItems = urlQueryItems
guard let url = urlComponents.url else {
throw URLError(.badURL, userInfo: [
NSLocalizedDescriptionKey: "Invalid Request URL with components: \(urlComponents)",
])
}

return url
}

private func httpResponse(urlResponse: URLResponse) throws -> HTTPURLResponse {
// The following condition should always be true: "Whenever you make HTTP URL load requests, any
// response objects you get back from the URLSession, NSURLConnection, or NSURLDownload class
Expand Down
3 changes: 2 additions & 1 deletion FirebaseAI/Sources/GenerativeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ public final class GenerativeModel: Sendable {
}

// Check to see if an error should be thrown for stop reason.
if let reason = response.candidates.first?.finishReason, reason != .stop {
if let reason = response.candidates.first?.finishReason, reason != .stop,
reason.rawValue != FinishReason.Kind.unspecified.rawValue {
throw GenerateContentError.responseStoppedEarly(reason: reason, response: response)
}

Expand Down
3 changes: 3 additions & 0 deletions FirebaseAI/Sources/Safety.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public struct SafetyRating: Equatable, Hashable, Sendable {
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public struct HarmProbability: DecodableProtoEnum, Hashable, Sendable {
enum Kind: String {
case unspecified = "HARM_CATEGORY_UNSPECIFIED"
case negligible = "NEGLIGIBLE"
case low = "LOW"
case medium = "MEDIUM"
Expand Down Expand Up @@ -114,6 +115,7 @@ public struct SafetyRating: Equatable, Hashable, Sendable {
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public struct HarmSeverity: DecodableProtoEnum, Hashable, Sendable {
enum Kind: String {
case unspecified = "HARM_SEVERITY_UNSPECIFIED"
case negligible = "HARM_SEVERITY_NEGLIGIBLE"
case low = "HARM_SEVERITY_LOW"
case medium = "HARM_SEVERITY_MEDIUM"
Expand Down Expand Up @@ -234,6 +236,7 @@ public struct SafetySetting: Sendable {
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public struct HarmCategory: CodableProtoEnum, Hashable, Sendable {
enum Kind: String {
case unspecified = "HARM_CATEGORY_UNSPECIFIED"
case harassment = "HARM_CATEGORY_HARASSMENT"
case hateSpeech = "HARM_CATEGORY_HATE_SPEECH"
case sexuallyExplicit = "HARM_CATEGORY_SEXUALLY_EXPLICIT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ struct GenerateContentIntegrationTests {

@Test(arguments: [
InstanceConfig.vertexV1Beta,
// TODO(andrewheard): Configs temporarily disabled due to backend issue.
// InstanceConfig.developerV1Beta,
// InstanceConfig.developerV1BetaStaging
InstanceConfig.developerV1Beta,
InstanceConfig.developerV1BetaStaging,
InstanceConfig.developerV1BetaSpark,
])
func generateImage(_ config: InstanceConfig) async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ final class IntegrationTests: XCTestCase {
ModelContent(role: "function", parts: sumResponse),
])

XCTAssertEqual(response.totalTokens, 24)
XCTAssertEqual(response.totalTokens, 30)
XCTAssertEqual(response.totalBillableCharacters, 71)
XCTAssertEqual(response.promptTokensDetails.count, 1)
let promptTokensDetails = try XCTUnwrap(response.promptTokensDetails.first)
XCTAssertEqual(promptTokensDetails.modality, .text)
XCTAssertEqual(promptTokensDetails.tokenCount, 24)
XCTAssertEqual(promptTokensDetails.tokenCount, response.totalTokens)
}

func testCountTokens_appCheckNotConfigured_shouldFail() async throws {
Expand Down
10 changes: 4 additions & 6 deletions FirebaseAI/Tests/TestApp/Tests/Utilities/InstanceConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ struct InstanceConfig {
vertexV1Staging,
vertexV1Beta,
vertexV1BetaStaging,
// TODO(andrewheard): Configs temporarily disabled due to backend issue:
// developerV1Beta,
// developerV1BetaStaging,
developerV1Beta,
developerV1BetaStaging,
developerV1Spark,
developerV1BetaSpark,
]
Expand All @@ -63,9 +62,8 @@ struct InstanceConfig {
vertexV1Staging,
vertexV1Beta,
vertexV1BetaStaging,
// TODO(andrewheard): Configs temporarily disabled due to backend issue:
// developerV1Beta,
// developerV1BetaStaging,
developerV1Beta,
developerV1BetaStaging,
developerV1BetaSpark,
]

Expand Down
Loading