2024-09-03 05:12:07 -04:00
|
|
|
local Utils = require("avante.utils")
|
2024-08-31 13:39:50 -04:00
|
|
|
|
|
|
|
---@class AvanteTokenizer
|
|
|
|
---@field from_pretrained fun(model: string): nil
|
|
|
|
---@field encode fun(string): integer[]
|
|
|
|
local tokenizers = nil
|
|
|
|
|
2024-12-13 20:00:43 +05:00
|
|
|
---@type "gpt-4o" | string
|
|
|
|
local current_model = "gpt-4o"
|
|
|
|
|
2024-08-31 13:39:50 -04:00
|
|
|
local M = {}
|
|
|
|
|
2024-12-13 20:00:43 +05:00
|
|
|
---@param model "gpt-4o" | string
|
|
|
|
---@return AvanteTokenizer|nil
|
|
|
|
M._init_tokenizers_lib = function(model)
|
|
|
|
if tokenizers ~= nil then
|
|
|
|
return tokenizers
|
|
|
|
end
|
|
|
|
|
|
|
|
local ok, core = pcall(require, "avante_tokenizers")
|
|
|
|
if not ok then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
---@cast core AvanteTokenizer
|
|
|
|
tokenizers = core
|
|
|
|
|
|
|
|
core.from_pretrained(model)
|
|
|
|
|
|
|
|
return tokenizers
|
|
|
|
end
|
|
|
|
|
2024-08-31 13:39:50 -04:00
|
|
|
---@param model "gpt-4o" | string
|
2024-10-27 02:17:35 -04:00
|
|
|
---@param warning? boolean
|
|
|
|
M.setup = function(model, warning)
|
2024-12-13 20:00:43 +05:00
|
|
|
current_model = model
|
2024-10-27 02:17:35 -04:00
|
|
|
warning = warning or true
|
2024-09-03 04:09:13 -04:00
|
|
|
vim.defer_fn(function()
|
2024-12-13 20:00:43 +05:00
|
|
|
M._init_tokenizers_lib(model)
|
2024-09-03 04:09:13 -04:00
|
|
|
end, 1000)
|
2024-08-31 13:39:50 -04:00
|
|
|
|
2024-10-27 02:17:35 -04:00
|
|
|
if warning then
|
|
|
|
local HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
if HF_TOKEN == nil and model ~= "gpt-4o" then
|
|
|
|
Utils.warn(
|
|
|
|
"Please set HF_TOKEN environment variable to use HuggingFace tokenizer if " .. model .. " is gated",
|
|
|
|
{ once = true }
|
|
|
|
)
|
|
|
|
end
|
2024-08-31 13:39:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-12-13 20:00:43 +05:00
|
|
|
M.available = function() return M._init_tokenizers_lib(current_model) ~= nil end
|
2024-08-31 13:39:50 -04:00
|
|
|
|
|
|
|
---@param prompt string
|
|
|
|
M.encode = function(prompt)
|
2024-12-13 20:00:43 +05:00
|
|
|
if not M.available() then return nil end
|
2024-09-03 04:19:54 -04:00
|
|
|
if not prompt or prompt == "" then return nil end
|
|
|
|
if type(prompt) ~= "string" then error("Prompt is not type string", 2) end
|
2024-08-31 13:39:50 -04:00
|
|
|
|
|
|
|
return tokenizers.encode(prompt)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param prompt string
|
|
|
|
M.count = function(prompt)
|
2024-12-13 20:00:43 +05:00
|
|
|
if not M.available() then return math.ceil(#prompt * 0.5) end
|
2024-08-31 13:39:50 -04:00
|
|
|
|
|
|
|
local tokens = M.encode(prompt)
|
2024-09-03 04:19:54 -04:00
|
|
|
if not tokens then return 0 end
|
2024-08-31 13:39:50 -04:00
|
|
|
return #tokens
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|