Skip to content

Handle totalBillableCharacters optional decoding in Vertex AI #12715

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

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion FirebaseVertexAI/Sources/CountTokensRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ public struct CountTokensResponse: Decodable {
public let totalTokens: Int

/// The total number of billable characters in the input given to the model as a prompt.
public let totalBillableCharacters: Int
public let totalBillableCharacters: Int?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"totalTokens": 258
}
15 changes: 15 additions & 0 deletions FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,21 @@ final class GenerativeModelTests: XCTestCase {
XCTAssertEqual(response.totalBillableCharacters, 16)
}

func testCountTokens_succeeds_noBillableCharacters() async throws {
MockURLProtocol.requestHandler = try httpRequestHandler(
forResource: "success-no-billable-characters",
withExtension: "json"
)

let response = try await model.countTokens(ModelContent.Part.data(
mimetype: "image/jpeg",
Data()
))

XCTAssertEqual(response.totalTokens, 258)
XCTAssertNil(response.totalBillableCharacters)
}

func testCountTokens_modelNotFound() async throws {
MockURLProtocol.requestHandler = try httpRequestHandler(
forResource: "failure-model-not-found", withExtension: "json",
Expand Down