Skip to content

Commit b5e4f0b

Browse files
committed
[FirebaseAI] Implicit caching support
1 parent 5d84076 commit b5e4f0b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

FirebaseAI/Sources/GenerateContentResponse.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public struct GenerateContentResponse: Sendable {
2323
/// The number of tokens in the request prompt.
2424
public let promptTokenCount: Int
2525

26+
/// Number of tokens in the cached part of the prompt (the cached content)
27+
public let cachedContentTokenCount: Int
28+
2629
/// The total number of tokens across the generated response candidates.
2730
public let candidatesTokenCount: Int
2831

@@ -32,6 +35,9 @@ public struct GenerateContentResponse: Sendable {
3235
/// The breakdown, by modality, of how many tokens are consumed by the prompt
3336
public let promptTokensDetails: [ModalityTokenCount]
3437

38+
/// The breakdown, by modality, of how many tokens are consumed by the cachedContent
39+
public let cacheTokensDetails: [ModalityTokenCount]
40+
3541
/// The breakdown, by modality, of how many tokens are consumed by the candidates
3642
public let candidatesTokensDetails: [ModalityTokenCount]
3743
}
@@ -329,20 +335,25 @@ extension GenerateContentResponse: Decodable {
329335
extension GenerateContentResponse.UsageMetadata: Decodable {
330336
enum CodingKeys: CodingKey {
331337
case promptTokenCount
338+
case cacheContentTokenCount
332339
case candidatesTokenCount
333340
case totalTokenCount
334341
case promptTokensDetails
342+
case cacheTokensDetails
335343
case candidatesTokensDetails
336344
}
337345

338346
public init(from decoder: any Decoder) throws {
339347
let container = try decoder.container(keyedBy: CodingKeys.self)
340348
promptTokenCount = try container.decodeIfPresent(Int.self, forKey: .promptTokenCount) ?? 0
349+
cachedContentTokenCount = try container.decodeIfPresent(Int.self, forKey: .cacheContentTokenCount) ?? 0
341350
candidatesTokenCount =
342351
try container.decodeIfPresent(Int.self, forKey: .candidatesTokenCount) ?? 0
343352
totalTokenCount = try container.decodeIfPresent(Int.self, forKey: .totalTokenCount) ?? 0
344353
promptTokensDetails =
345354
try container.decodeIfPresent([ModalityTokenCount].self, forKey: .promptTokensDetails) ?? []
355+
cacheTokensDetails =
356+
try container.decodeIfPresent([ModalityTokenCount].self, forKey: .cacheTokensDetails) ?? []
346357
candidatesTokensDetails = try container.decodeIfPresent(
347358
[ModalityTokenCount].self,
348359
forKey: .candidatesTokensDetails

FirebaseAI/Tests/Unit/APITests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ final class APITests: XCTestCase {
176176
// Usage Metadata
177177
guard let usageMetadata = response.usageMetadata else { fatalError() }
178178
let _: Int = usageMetadata.promptTokenCount
179+
let _: Int = usageMetadata.cachedContentTokenCount
179180
let _: Int = usageMetadata.candidatesTokenCount
180181
let _: Int = usageMetadata.totalTokenCount
181182

0 commit comments

Comments
 (0)