Merge pull request #2428 from sasha0552/vllm-tc Add a token counting endpoint for vLLM

545b77140f06d79f49800dd25b43265eac536721

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +8 -4Ignore whitespace
public/scripts/tokenizers.js+3 -2
@@ -7,7 +7,7 @@ import { kai_flags } from './kai-settings.js';
77import { textgen_types, textgenerationwebui_settings as textgen_settings, getTextGenServer, getTextGenModel } from './textgen-settings.js';
88import { getCurrentDreamGenModelTokenizer, getCurrentOpenRouterModelTokenizer, openRouterModels } from './textgen-models.js';
99
1010const { OOBA, TABBY, KOBOLDCPP, VLLM, APHRODITE, LLAMACPP, OPENROUTER, DREAMGEN } = textgen_types;
1111
1212export const CHARACTERS_PER_TOKEN_RATIO = 3.35;
1313const TOKENIZER_WARNING_KEY = 'tokenizationWarningShown';
@@ -39,7 +39,7 @@ export const SENTENCEPIECE_TOKENIZERS = [
3939 //tokenizers.NERD2,
4040];
4141
4242export const TEXTGEN_TOKENIZERS = [OOBA, TABBY, KOBOLDCPP, LLAMACPP, VLLM, APHRODITE];
4343
4444const TOKENIZER_URLS = {
4545 [tokenizers.GPT2]: {
@@ -769,6 +769,7 @@ function getTextgenAPITokenizationParams(str) {
769769 api_type: textgen_settings.type,
770770 url: getTextGenServer(),
771771 legacy_api: textgen_settings.legacy_api && (textgen_settings.type === OOBA || textgen_settings.type === APHRODITE),
772+ vllm_model: textgen_settings.vllm_model,
772773 };
773774}
774775
src/endpoints/tokenizers.js+5 -2
@@ -784,6 +784,7 @@ router.post('/remote/textgenerationwebui/encode', jsonParser, async function (re
784784 const text = String(request.body.text) || '';
785785 const baseUrl = String(request.body.url);
786786 const legacyApi = Boolean(request.body.legacy_api);
787+ const vllmModel = String(request.body.vllm_model) || '';
787788
788789 try {
789790 const args = {
@@ -814,7 +815,9 @@ router.post('/remote/textgenerationwebui/encode', jsonParser, async function (re
814815 args.body = JSON.stringify({ 'content': text });
815816 break;
816817 case TEXTGEN_TYPES.VLLM:
817- return response.send({ error: true });
818+ url += '/tokenize';
819+ args.body = JSON.stringify({ 'model': vllmModel, 'prompt': text });
820+ break;
818821 case TEXTGEN_TYPES.APHRODITE:
819822 url += '/v1/tokenize';
820823 args.body = JSON.stringify({ 'prompt': text });
@@ -834,7 +837,7 @@ router.post('/remote/textgenerationwebui/encode', jsonParser, async function (re
834837 }
835838
836839 const data = await result.json();
837840 const count = legacyApi ? data?.results[0]?.tokens : (data?.length ?? data?.count ?? data?.value ?? data?.tokens?.length);
838841 const ids = legacyApi ? [] : (data?.tokens ?? data?.ids ?? []);
839842
840843 return response.send({ count, ids });