import { callPopup, main_api } from "../../../script.js";
import { getContext } from "../../extensions.js";
import { getTokenizerModel } from "../../openai.js";
async function doTokenCounter() {
const selectedTokenizer = main_api == 'openai'
? `tiktoken (${getTokenizerModel()})`
: $("#tokenizer").find(':selected').text();
const html = `
Token Counter
Type / paste in the box below to see the number of tokens in the text.
Selected tokenizer: ${selectedTokenizer}
Tokens: 0
`;
const dialog = $(html);
dialog.find('#token_counter_textarea').on('input', () => {
const text = $('#token_counter_textarea').val();
const context = getContext();
const count = context.getTokenCount(text);
$('#token_counter_result').text(count);
});
$('#dialogue_popup').addClass('wide_dialogue_popup');
callPopup(dialog, 'text');
}
jQuery(() => {
const buttonHtml = `
Token Counter
`;
$('#extensionsMenu').prepend(buttonHtml);
$('#token_counter').on('click', doTokenCounter);
});