Skip to content

Commit

Permalink
[Vertex AI] Remove CountTokensError enum (#13736)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard authored Sep 27, 2024
1 parent 4c9cc64 commit 78c341f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
3 changes: 3 additions & 0 deletions FirebaseVertexAI/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
- [changed] **Breaking Change**: The `ImageConversionError` enum is no longer
public; image conversion errors are still reported as
`GenerateContentError.promptImageContentError`. (#13735)
- [changed] **Breaking Change**: The `CountTokensError` enum has been removed;
errors occurring in `GenerativeModel.countTokens(...)` are now thrown directly
instead of being wrapped in a `CountTokensError.internalError`. (#13736)
- [changed] The default request timeout is now 180 seconds instead of the
platform-default value of 60 seconds for a `URLRequest`; this timeout may
still be customized in `RequestOptions`. (#13722)
Expand Down
22 changes: 6 additions & 16 deletions FirebaseVertexAI/Sources/GenerativeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,12 @@ public final class GenerativeModel {
/// invalid.
public func countTokens(_ content: @autoclosure () throws -> [ModelContent]) async throws
-> CountTokensResponse {
do {
let countTokensRequest = try CountTokensRequest(
model: modelResourceName,
contents: content(),
options: requestOptions
)
return try await generativeAIService.loadRequest(request: countTokensRequest)
} catch {
throw CountTokensError.internalError(underlying: error)
}
let countTokensRequest = try CountTokensRequest(
model: modelResourceName,
contents: content(),
options: requestOptions
)
return try await generativeAIService.loadRequest(request: countTokensRequest)
}

/// Returns a `GenerateContentError` (for public consumption) from an internal error.
Expand All @@ -291,9 +287,3 @@ public final class GenerativeModel {
return GenerateContentError.internalError(underlying: error)
}
}

/// An error thrown in `GenerativeModel.countTokens(_:)`.
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public enum CountTokensError: Error {
case internalError(underlying: Error)
}
2 changes: 1 addition & 1 deletion FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ final class GenerativeModelTests: XCTestCase {
do {
_ = try await model.countTokens("Why is the sky blue?")
XCTFail("Request should not have succeeded.")
} catch let CountTokensError.internalError(rpcError as RPCError) {
} catch let rpcError as RPCError {
XCTAssertEqual(rpcError.httpResponseCode, 404)
XCTAssertEqual(rpcError.status, .notFound)
XCTAssert(rpcError.message.hasPrefix("models/test-model-name is not found"))
Expand Down

0 comments on commit 78c341f

Please sign in to comment.