refactor: message content (#1424)

This commit is contained in:
yetone
2025-02-28 14:43:39 +08:00
committed by GitHub
parent ebadba7420
commit ae8497faf1
10 changed files with 187 additions and 125 deletions

View File

@@ -9,6 +9,21 @@ local cost_per_token = {
davinci = 0.000002,
}
---@param content AvanteLLMMessageContent
---@return integer
function Tokens.calculate_message_content_tokens(content)
if type(content) == "string" then return Tokens.calculate_tokens(content) end
local tokens = 0
for _, item in ipairs(content) do
if type(item) == "string" then
tokens = tokens + Tokens.calculate_tokens(item)
elseif item.type == "text" then
tokens = tokens + Tokens.calculate_tokens(item.text)
end
end
return tokens
end
--- Calculate the number of tokens in a given text.
---@param text string The text to calculate the number of tokens for.
---@return integer The number of tokens in the given text.