@adminforth/i18n 2.3.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +24 -2
- package/index.ts +24 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -586,10 +586,32 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
586
586
|
const maxInputTokens = (_a = this.options.inputTokensPerBatch) !== null && _a !== void 0 ? _a : 30000;
|
|
587
587
|
const limit = pLimit(30);
|
|
588
588
|
const enStringsTokenLengthCache = {};
|
|
589
|
+
let tokenCountingFailed = false;
|
|
590
|
+
// Counting tokens is only used to split requests into batches. Some completion
|
|
591
|
+
// adapters cannot provide a token count, so use a conservative estimate instead
|
|
592
|
+
// of preventing the translation job from being created.
|
|
593
|
+
const getTokenLength = (value) => __awaiter(this, void 0, void 0, function* () {
|
|
594
|
+
try {
|
|
595
|
+
const tokenLength = yield this.options.completeAdapter.measureTokensCount(value);
|
|
596
|
+
if (!Number.isFinite(tokenLength) || tokenLength < 0) {
|
|
597
|
+
throw new Error(`Invalid token count: ${tokenLength}`);
|
|
598
|
+
}
|
|
599
|
+
return tokenLength;
|
|
600
|
+
}
|
|
601
|
+
catch (error) {
|
|
602
|
+
if (!tokenCountingFailed) {
|
|
603
|
+
tokenCountingFailed = true;
|
|
604
|
+
afLogger.warn(`Could not count tokens for translation batches in plugin ${this.constructor.name}; using a character-based estimate instead: ${String(error)}`);
|
|
605
|
+
}
|
|
606
|
+
// English text normally uses about 4 characters per token. Three gives
|
|
607
|
+
// batches some extra headroom for JSON formatting and unusual characters.
|
|
608
|
+
return Math.max(1, Math.ceil(value.length / 3));
|
|
609
|
+
}
|
|
610
|
+
});
|
|
589
611
|
const tokenLengthPerString = (_a) => __awaiter(this, [_a], void 0, function* ({ str, id }) {
|
|
590
612
|
const objectToPush = {
|
|
591
613
|
en_string: str,
|
|
592
|
-
numOfTokens: yield
|
|
614
|
+
numOfTokens: yield getTokenLength(`"${str}":"", \n`)
|
|
593
615
|
};
|
|
594
616
|
enStringsTokenLengthCache[id] = objectToPush;
|
|
595
617
|
});
|
|
@@ -622,7 +644,7 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
622
644
|
\`\`\`
|
|
623
645
|
`;
|
|
624
646
|
const failedToTranslate = [];
|
|
625
|
-
const basePromptTokenLength = yield
|
|
647
|
+
const basePromptTokenLength = yield getTokenLength(basePrompt);
|
|
626
648
|
const allowedTokensAmountForFields = maxInputTokens - basePromptTokenLength;
|
|
627
649
|
const stringsToTranslate = Object.fromEntries(strings.map(s => [s.id, s]));
|
|
628
650
|
const generationTasksInitialData = [];
|
package/index.ts
CHANGED
|
@@ -698,12 +698,34 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
698
698
|
|
|
699
699
|
const limit = pLimit(30);
|
|
700
700
|
const enStringsTokenLengthCache: Record<string, any> = {};
|
|
701
|
+
let tokenCountingFailed = false;
|
|
702
|
+
|
|
703
|
+
// Counting tokens is only used to split requests into batches. Some completion
|
|
704
|
+
// adapters cannot provide a token count, so use a conservative estimate instead
|
|
705
|
+
// of preventing the translation job from being created.
|
|
706
|
+
const getTokenLength = async (value: string): Promise<number> => {
|
|
707
|
+
try {
|
|
708
|
+
const tokenLength = await this.options.completeAdapter.measureTokensCount(value);
|
|
709
|
+
if (!Number.isFinite(tokenLength) || tokenLength < 0) {
|
|
710
|
+
throw new Error(`Invalid token count: ${tokenLength}`);
|
|
711
|
+
}
|
|
712
|
+
return tokenLength;
|
|
713
|
+
} catch (error) {
|
|
714
|
+
if (!tokenCountingFailed) {
|
|
715
|
+
tokenCountingFailed = true;
|
|
716
|
+
afLogger.warn(`Could not count tokens for translation batches in plugin ${this.constructor.name}; using a character-based estimate instead: ${String(error)}`);
|
|
717
|
+
}
|
|
718
|
+
// English text normally uses about 4 characters per token. Three gives
|
|
719
|
+
// batches some extra headroom for JSON formatting and unusual characters.
|
|
720
|
+
return Math.max(1, Math.ceil(value.length / 3));
|
|
721
|
+
}
|
|
722
|
+
};
|
|
701
723
|
|
|
702
724
|
|
|
703
725
|
const tokenLengthPerString = async ({ str, id }: { str: string; id: string }): Promise<void> => {
|
|
704
726
|
const objectToPush = {
|
|
705
727
|
en_string: str,
|
|
706
|
-
numOfTokens: await
|
|
728
|
+
numOfTokens: await getTokenLength(`"${str}":"", \n`)
|
|
707
729
|
};
|
|
708
730
|
enStringsTokenLengthCache[id] = objectToPush;
|
|
709
731
|
}
|
|
@@ -741,7 +763,7 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
741
763
|
`;
|
|
742
764
|
|
|
743
765
|
const failedToTranslate: IFailedTranslation[] = [];
|
|
744
|
-
const basePromptTokenLength = await
|
|
766
|
+
const basePromptTokenLength = await getTokenLength(basePrompt);
|
|
745
767
|
const allowedTokensAmountForFields = maxInputTokens - basePromptTokenLength;
|
|
746
768
|
const stringsToTranslate: Record<string, { id: string; en_string: string; category: string }> = Object.fromEntries(strings.map(s => [s.id, s]));
|
|
747
769
|
const generationTasksInitialData = []
|