Skip to content

Commit d7987be

Browse files
committed
Add GoogleAITokenCounter
1 parent bb330b7 commit d7987be

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import GoogleGenerativeAI
2+
3+
public final class GoogleAITokenCounter: TokenCounter {
4+
let model: GenerativeModel
5+
6+
public init(model: GenerativeModel) {
7+
self.model = model
8+
}
9+
10+
public func countToken(text: String) async -> Int {
11+
(try? await model.countTokens(text))?.totalTokens ?? text.count
12+
}
13+
}

0 commit comments

Comments
 (0)