Add model hash to TC API tokenizer cache key

fc4fecf0fdf56b7ec13fb2ab0bffb85d715546a2

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

1 files changed, +12 -2Showing whitespace changes
public/scripts/tokenizers.js+12 -2
@@ -433,6 +433,7 @@ export async function getTokenCountAsync(str, padding = undefined) {
433 }433 }
434434
435 let tokenizerType = power_user.tokenizer;435 let tokenizerType = power_user.tokenizer;
436 let modelHash = '';
436437
437 if (main_api === 'openai') {438 if (main_api === 'openai') {
438 if (padding === power_user.token_padding) {439 if (padding === power_user.token_padding) {
@@ -448,13 +449,17 @@ export async function getTokenCountAsync(str, padding = undefined) {
448 tokenizerType = getTokenizerBestMatch(main_api);449 tokenizerType = getTokenizerBestMatch(main_api);
449 }450 }
450451
452 if (tokenizerType === tokenizers.API_TEXTGENERATIONWEBUI) {
453 modelHash = getStringHash(getTextGenModel() || online_status).toString();
454 }
455
451 if (padding === undefined) {456 if (padding === undefined) {
452 padding = 0;457 padding = 0;
453 }458 }
454459
455 const cacheObject = getTokenCacheObject();460 const cacheObject = getTokenCacheObject();
456 const hash = getStringHash(str);461 const hash = getStringHash(str);
457 const cacheKey = `${tokenizerType}-${hash}+${padding}`;462 const cacheKey = `${tokenizerType}-${hash}${modelHash}+${padding}`;
458463
459 if (typeof cacheObject[cacheKey] === 'number') {464 if (typeof cacheObject[cacheKey] === 'number') {
460 return cacheObject[cacheKey];465 return cacheObject[cacheKey];
@@ -484,6 +489,7 @@ export function getTokenCount(str, padding = undefined) {
484 }489 }
485490
486 let tokenizerType = power_user.tokenizer;491 let tokenizerType = power_user.tokenizer;
492 let modelHash = '';
487493
488 if (main_api === 'openai') {494 if (main_api === 'openai') {
489 if (padding === power_user.token_padding) {495 if (padding === power_user.token_padding) {
@@ -499,13 +505,17 @@ export function getTokenCount(str, padding = undefined) {
499 tokenizerType = getTokenizerBestMatch(main_api);505 tokenizerType = getTokenizerBestMatch(main_api);
500 }506 }
501507
508 if (tokenizerType === tokenizers.API_TEXTGENERATIONWEBUI) {
509 modelHash = getStringHash(getTextGenModel() || online_status).toString();
510 }
511
502 if (padding === undefined) {512 if (padding === undefined) {
503 padding = 0;513 padding = 0;
504 }514 }
505515
506 const cacheObject = getTokenCacheObject();516 const cacheObject = getTokenCacheObject();
507 const hash = getStringHash(str);517 const hash = getStringHash(str);
508 const cacheKey = `${tokenizerType}-${hash}+${padding}`;518 const cacheKey = `${tokenizerType}-${hash}${modelHash}+${padding}`;
509519
510 if (typeof cacheObject[cacheKey] === 'number') {520 if (typeof cacheObject[cacheKey] === 'number') {
511 return cacheObject[cacheKey];521 return cacheObject[cacheKey];