Add model hash to TC API tokenizer cache key

This commit is contained in:
Cohee
2025-01-02 23:49:22 +02:00
parent 40b05bb6b1
commit fc4fecf0fd

View File

@ -433,6 +433,7 @@ export async function getTokenCountAsync(str, padding = undefined) {
} }
let tokenizerType = power_user.tokenizer; let tokenizerType = power_user.tokenizer;
let modelHash = '';
if (main_api === 'openai') { if (main_api === 'openai') {
if (padding === power_user.token_padding) { if (padding === power_user.token_padding) {
@ -448,13 +449,17 @@ export async function getTokenCountAsync(str, padding = undefined) {
tokenizerType = getTokenizerBestMatch(main_api); tokenizerType = getTokenizerBestMatch(main_api);
} }
if (tokenizerType === tokenizers.API_TEXTGENERATIONWEBUI) {
modelHash = getStringHash(getTextGenModel() || online_status).toString();
}
if (padding === undefined) { if (padding === undefined) {
padding = 0; padding = 0;
} }
const cacheObject = getTokenCacheObject(); const cacheObject = getTokenCacheObject();
const hash = getStringHash(str); const hash = getStringHash(str);
const cacheKey = `${tokenizerType}-${hash}+${padding}`; const cacheKey = `${tokenizerType}-${hash}${modelHash}+${padding}`;
if (typeof cacheObject[cacheKey] === 'number') { if (typeof cacheObject[cacheKey] === 'number') {
return cacheObject[cacheKey]; return cacheObject[cacheKey];
@ -484,6 +489,7 @@ export function getTokenCount(str, padding = undefined) {
} }
let tokenizerType = power_user.tokenizer; let tokenizerType = power_user.tokenizer;
let modelHash = '';
if (main_api === 'openai') { if (main_api === 'openai') {
if (padding === power_user.token_padding) { if (padding === power_user.token_padding) {
@ -499,13 +505,17 @@ export function getTokenCount(str, padding = undefined) {
tokenizerType = getTokenizerBestMatch(main_api); tokenizerType = getTokenizerBestMatch(main_api);
} }
if (tokenizerType === tokenizers.API_TEXTGENERATIONWEBUI) {
modelHash = getStringHash(getTextGenModel() || online_status).toString();
}
if (padding === undefined) { if (padding === undefined) {
padding = 0; padding = 0;
} }
const cacheObject = getTokenCacheObject(); const cacheObject = getTokenCacheObject();
const hash = getStringHash(str); const hash = getStringHash(str);
const cacheKey = `${tokenizerType}-${hash}+${padding}`; const cacheKey = `${tokenizerType}-${hash}${modelHash}+${padding}`;
if (typeof cacheObject[cacheKey] === 'number') { if (typeof cacheObject[cacheKey] === 'number') {
return cacheObject[cacheKey]; return cacheObject[cacheKey];