From cde830ce5bf5bf58d91dd540e80af4ed00d07480 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Fri, 8 Aug 2025 11:10:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E5=AA=92=E4=BD=93?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E7=BB=9F=E8=AE=A1token=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yunqiinnovation/chat_api/Utils.kt | 46 +++++++++++++++---- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/Utils.kt b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/Utils.kt index 77a3dffde..d303ae3b4 100644 --- a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/Utils.kt +++ b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/Utils.kt @@ -9,6 +9,8 @@ import android.provider.Settings import android.util.Log import androidx.core.content.ContextCompat import com.aallam.openai.api.chat.ChatMessage +import com.aallam.openai.api.chat.TextContent +import com.aallam.openai.api.chat.ListContent import io.ktor.client.* import io.ktor.client.engine.okhttp.* import io.ktor.client.plugins.sse.* @@ -169,16 +171,42 @@ fun roughTokenCount(text: String): Int { /** * 估算消息列表的总 token 数量(近似) - * @param messages List of message map: role: user/assistant, content: string + * @param messages List of message map: role: user/assistant, content: string or array * @return 估算 token 数 */ fun estimatePromptTokens(messages: List): Int { - var tokens = 0 - for (msg in messages) { - tokens += 4 // 每条消息结构开销 - tokens += roughTokenCount(msg.role.role.lowercase()) // ChatRole 枚举转小写字符串 - tokens += roughTokenCount(msg.content ?: "") + var totalText = "" + for (message in messages) { + when (val content = message.messageContent) { + is TextContent? -> { + totalText += content?.content + " " + } + is ListContent -> { + // 处理多模态内容数组 + content.content.forEach { item -> + if (item is Map<*, *>) { + val type = item["type"] as? String + if (type == "text") { + val text = item["text"] as? String ?: "" + totalText += text + " " + }else if(type == "image_url"){ + val text = item["image_url"] as? String ?: "" + totalText += text + " " + } + } + } + } + } } - tokens += 2 // 结尾 assistant 回复结构开销 - return tokens -} \ No newline at end of file + return roughTokenCount(totalText) +} +//fun estimatePromptTokens(messages: List): Int { +// var tokens = 0 +// for (msg in messages) { +// tokens += 4 // 每条消息结构开销 +// tokens += roughTokenCount(msg.role.role.lowercase()) // ChatRole 枚举转小写字符串 +// tokens += roughTokenCount(msg.content ?: "") +// } +// tokens += 2 // 结尾 assistant 回复结构开销 +// return tokens +//} \ No newline at end of file