|
|
@ -9,6 +9,8 @@ import android.provider.Settings |
|
|
import android.util.Log |
|
|
import android.util.Log |
|
|
import androidx.core.content.ContextCompat |
|
|
import androidx.core.content.ContextCompat |
|
|
import com.aallam.openai.api.chat.ChatMessage |
|
|
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.* |
|
|
import io.ktor.client.engine.okhttp.* |
|
|
import io.ktor.client.engine.okhttp.* |
|
|
import io.ktor.client.plugins.sse.* |
|
|
import io.ktor.client.plugins.sse.* |
|
|
@ -169,16 +171,42 @@ fun roughTokenCount(text: String): Int { |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 估算消息列表的总 token 数量(近似) |
|
|
* 估算消息列表的总 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 数 |
|
|
* @return 估算 token 数 |
|
|
*/ |
|
|
*/ |
|
|
fun estimatePromptTokens(messages: List<ChatMessage>): Int { |
|
|
fun estimatePromptTokens(messages: List<ChatMessage>): Int { |
|
|
var tokens = 0 |
|
|
var totalText = "" |
|
|
for (msg in messages) { |
|
|
for (message in messages) { |
|
|
tokens += 4 // 每条消息结构开销 |
|
|
when (val content = message.messageContent) { |
|
|
tokens += roughTokenCount(msg.role.role.lowercase()) // ChatRole 枚举转小写字符串 |
|
|
is TextContent? -> { |
|
|
tokens += roughTokenCount(msg.content ?: "") |
|
|
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 roughTokenCount(totalText) |
|
|
return tokens |
|
|
} |
|
|
} |
|
|
//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 |
|
|
|
|
|
//} |