Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,13 @@ interface LlmModelHelper {
* @param model the ongoing model response to be stopped.
*/
fun stopResponse(model: Model)

/**
* Counts the number of tokens in the specified text for the model.
*
* @param model the model to use for token counting.
* @param text the text to count tokens for.
* @return the number of tokens, or null if not supported or failed.
*/
suspend fun countTokens(model: Model, text: String): Int? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,19 @@ object AICoreModelHelper : LlmModelHelper {
instance.inferenceJob?.cancel()
}

override suspend fun countTokens(model: Model, text: String): Int? {
val instance = model.instance as? AICoreModelInstance ?: return null
return try {
val response = instance.generativeModel.countTokens(generateContentRequest(TextPart(text)) {})
val totalTokens = response.totalTokens
Log.d(TAG, "Counted tokens: $totalTokens for text: $text")
totalTokens
} catch (e: Exception) {
Log.e(TAG, "Failed to count tokens", e)
null
}
}

override fun runInference(
model: Model,
input: String,
Expand Down Expand Up @@ -308,6 +321,7 @@ object AICoreModelHelper : LlmModelHelper {
resultListener: ResultListener,
onError: (message: String) -> Unit,
) {
Log.d(TAG, "AICore Inference Prompt: $prompt")
try {
val request =
if (images.isNotEmpty()) {
Expand Down
Loading