diff --git a/public/scripts/extensions/token-counter/index.js b/public/scripts/extensions/token-counter/index.js new file mode 100644 index 000000000..e07f5b69b --- /dev/null +++ b/public/scripts/extensions/token-counter/index.js @@ -0,0 +1,40 @@ +import { callPopup, main_api } from "../../../script.js"; +import { getContext } from "../../extensions.js"; +import { oai_settings } from "../../openai.js"; + +async function doTokenCounter() { + const selectedTokenizer = main_api == 'openai' + ? `tiktoken (${oai_settings.openai_model})` + : $("#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); +}); diff --git a/public/scripts/extensions/token-counter/manifest.json b/public/scripts/extensions/token-counter/manifest.json new file mode 100644 index 000000000..65da1d897 --- /dev/null +++ b/public/scripts/extensions/token-counter/manifest.json @@ -0,0 +1,11 @@ +{ + "display_name": "Token Counter", + "loading_order": 15, + "requires": [], + "optional": [], + "js": "index.js", + "css": "style.css", + "author": "Cohee#1207", + "version": "1.0.0", + "homePage": "https://github.com/Cohee1207/SillyTavern" +} diff --git a/public/scripts/extensions/token-counter/style.css b/public/scripts/extensions/token-counter/style.css new file mode 100644 index 000000000..e69de29bb diff --git a/public/style.css b/public/style.css index 100d2245c..599e7caa8 100644 --- a/public/style.css +++ b/public/style.css @@ -557,6 +557,7 @@ code { font-size: 20px; height: 20px; width: 20px; + text-align: center; } hr { @@ -4466,4 +4467,4 @@ body.waifuMode #avatar_zoom_popup { overflow-y: auto; overflow-x: hidden; } -} \ No newline at end of file +}