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