Browse Source

修复多媒体图片统计token报错问题

weicu
liwei1dao 1 year ago
parent
commit
cde830ce5b
  1. 44
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/Utils.kt

44
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<ChatMessage>): 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 + " "
}
tokens += 2 // 结尾 assistant 回复结构开销
return tokens
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 + " "
}
}
}
}
}
}
return roughTokenCount(totalText)
}
//fun estimatePromptTokens(messages: List<ChatMessage>): 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
//}
Loading…
Cancel
Save