From fc4fecf0fdf56b7ec13fb2ab0bffb85d715546a2 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 2 Jan 2025 23:49:22 +0200 Subject: [PATCH] Add model hash to TC API tokenizer cache key --- public/scripts/tokenizers.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/public/scripts/tokenizers.js b/public/scripts/tokenizers.js index a39be642d..59d55ba7f 100644 --- a/public/scripts/tokenizers.js +++ b/public/scripts/tokenizers.js @@ -433,6 +433,7 @@ export async function getTokenCountAsync(str, padding = undefined) { } let tokenizerType = power_user.tokenizer; + let modelHash = ''; if (main_api === 'openai') { if (padding === power_user.token_padding) { @@ -448,13 +449,17 @@ export async function getTokenCountAsync(str, padding = undefined) { tokenizerType = getTokenizerBestMatch(main_api); } + if (tokenizerType === tokenizers.API_TEXTGENERATIONWEBUI) { + modelHash = getStringHash(getTextGenModel() || online_status).toString(); + } + if (padding === undefined) { padding = 0; } const cacheObject = getTokenCacheObject(); const hash = getStringHash(str); - const cacheKey = `${tokenizerType}-${hash}+${padding}`; + const cacheKey = `${tokenizerType}-${hash}${modelHash}+${padding}`; if (typeof cacheObject[cacheKey] === 'number') { return cacheObject[cacheKey]; @@ -484,6 +489,7 @@ export function getTokenCount(str, padding = undefined) { } let tokenizerType = power_user.tokenizer; + let modelHash = ''; if (main_api === 'openai') { if (padding === power_user.token_padding) { @@ -499,13 +505,17 @@ export function getTokenCount(str, padding = undefined) { tokenizerType = getTokenizerBestMatch(main_api); } + if (tokenizerType === tokenizers.API_TEXTGENERATIONWEBUI) { + modelHash = getStringHash(getTextGenModel() || online_status).toString(); + } + if (padding === undefined) { padding = 0; } const cacheObject = getTokenCacheObject(); const hash = getStringHash(str); - const cacheKey = `${tokenizerType}-${hash}+${padding}`; + const cacheKey = `${tokenizerType}-${hash}${modelHash}+${padding}`; if (typeof cacheObject[cacheKey] === 'number') { return cacheObject[cacheKey];