gpt-tokenizer 2.7.0 → 2.8.0
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/README.md +24 -1
- package/cjs/BytePairEncodingCore.d.ts +13 -1
- package/cjs/BytePairEncodingCore.js +81 -6
- package/cjs/BytePairEncodingCore.js.map +1 -1
- package/cjs/GptEncoding.d.ts +3 -4
- package/cjs/GptEncoding.js +21 -14
- package/cjs/GptEncoding.js.map +1 -1
- package/cjs/constants.d.ts +1 -0
- package/cjs/constants.js +2 -1
- package/cjs/constants.js.map +1 -1
- package/cjs/encoding/cl100k_base.d.ts +2 -2
- package/cjs/encoding/cl100k_base.js +3 -2
- package/cjs/encoding/cl100k_base.js.map +1 -1
- package/cjs/encoding/o200k_base.d.ts +2 -2
- package/cjs/encoding/o200k_base.js +3 -2
- package/cjs/encoding/o200k_base.js.map +1 -1
- package/cjs/encoding/p50k_base.d.ts +2 -2
- package/cjs/encoding/p50k_base.js +3 -2
- package/cjs/encoding/p50k_base.js.map +1 -1
- package/cjs/encoding/p50k_edit.d.ts +2 -2
- package/cjs/encoding/p50k_edit.js +3 -2
- package/cjs/encoding/p50k_edit.js.map +1 -1
- package/cjs/encoding/r50k_base.d.ts +2 -2
- package/cjs/encoding/r50k_base.js +3 -2
- package/cjs/encoding/r50k_base.js.map +1 -1
- package/cjs/encodingParams/{Cl100KBase.js → cl100k_base.js} +3 -2
- package/cjs/encodingParams/cl100k_base.js.map +1 -0
- package/cjs/encodingParams/constants.d.ts +2 -0
- package/cjs/encodingParams/constants.js +6 -0
- package/cjs/encodingParams/constants.js.map +1 -0
- package/cjs/encodingParams/{O200KBase.js → o200k_base.js} +3 -2
- package/cjs/encodingParams/o200k_base.js.map +1 -0
- package/cjs/encodingParams/{P50KBase.js → p50k_base.js} +3 -3
- package/cjs/encodingParams/p50k_base.js.map +1 -0
- package/cjs/encodingParams/{P50KEdit.js → p50k_edit.js} +3 -3
- package/cjs/encodingParams/p50k_edit.js.map +1 -0
- package/cjs/encodingParams/{R50KBase.js → r50k_base.js} +3 -3
- package/cjs/encodingParams/r50k_base.js.map +1 -0
- package/cjs/extraApis.test.d.ts +1 -0
- package/cjs/extraApis.test.js +57 -0
- package/cjs/extraApis.test.js.map +1 -0
- package/cjs/model/gpt-3.5-turbo-0125.d.ts +2 -2
- package/cjs/model/gpt-3.5-turbo-0125.js +4 -2
- package/cjs/model/gpt-3.5-turbo-0125.js.map +1 -1
- package/cjs/model/gpt-3.5-turbo-0301.d.ts +2 -2
- package/cjs/model/gpt-3.5-turbo-0301.js +4 -2
- package/cjs/model/gpt-3.5-turbo-0301.js.map +1 -1
- package/cjs/model/gpt-3.5-turbo-0613.d.ts +2 -2
- package/cjs/model/gpt-3.5-turbo-0613.js +4 -2
- package/cjs/model/gpt-3.5-turbo-0613.js.map +1 -1
- package/cjs/model/gpt-3.5-turbo-1106.d.ts +2 -2
- package/cjs/model/gpt-3.5-turbo-1106.js +4 -2
- package/cjs/model/gpt-3.5-turbo-1106.js.map +1 -1
- package/cjs/model/gpt-3.5-turbo-16k-0613.d.ts +2 -2
- package/cjs/model/gpt-3.5-turbo-16k-0613.js +4 -2
- package/cjs/model/gpt-3.5-turbo-16k-0613.js.map +1 -1
- package/cjs/model/gpt-3.5-turbo-16k.d.ts +2 -2
- package/cjs/model/gpt-3.5-turbo-16k.js +4 -2
- package/cjs/model/gpt-3.5-turbo-16k.js.map +1 -1
- package/cjs/model/gpt-3.5-turbo-finetune.d.ts +2 -2
- package/cjs/model/gpt-3.5-turbo-finetune.js +4 -2
- package/cjs/model/gpt-3.5-turbo-finetune.js.map +1 -1
- package/cjs/model/gpt-3.5-turbo.d.ts +2 -2
- package/cjs/model/gpt-3.5-turbo.js +4 -2
- package/cjs/model/gpt-3.5-turbo.js.map +1 -1
- package/cjs/model/gpt-4-0125-preview.d.ts +2 -2
- package/cjs/model/gpt-4-0125-preview.js +4 -2
- package/cjs/model/gpt-4-0125-preview.js.map +1 -1
- package/cjs/model/gpt-4-0314.d.ts +2 -2
- package/cjs/model/gpt-4-0314.js +4 -2
- package/cjs/model/gpt-4-0314.js.map +1 -1
- package/cjs/model/gpt-4-0613.d.ts +2 -2
- package/cjs/model/gpt-4-0613.js +4 -2
- package/cjs/model/gpt-4-0613.js.map +1 -1
- package/cjs/model/gpt-4-1106-preview.d.ts +2 -2
- package/cjs/model/gpt-4-1106-preview.js +4 -2
- package/cjs/model/gpt-4-1106-preview.js.map +1 -1
- package/cjs/model/gpt-4-1106-vision-preview.d.ts +2 -2
- package/cjs/model/gpt-4-1106-vision-preview.js +4 -2
- package/cjs/model/gpt-4-1106-vision-preview.js.map +1 -1
- package/cjs/model/gpt-4-32k-0314.d.ts +2 -2
- package/cjs/model/gpt-4-32k-0314.js +4 -2
- package/cjs/model/gpt-4-32k-0314.js.map +1 -1
- package/cjs/model/gpt-4-32k-0613.d.ts +2 -2
- package/cjs/model/gpt-4-32k-0613.js +4 -2
- package/cjs/model/gpt-4-32k-0613.js.map +1 -1
- package/cjs/model/gpt-4-32k.d.ts +2 -2
- package/cjs/model/gpt-4-32k.js +4 -2
- package/cjs/model/gpt-4-32k.js.map +1 -1
- package/cjs/model/gpt-4-turbo-2024-04-09.d.ts +2 -2
- package/cjs/model/gpt-4-turbo-2024-04-09.js +4 -2
- package/cjs/model/gpt-4-turbo-2024-04-09.js.map +1 -1
- package/cjs/model/gpt-4-turbo-preview.d.ts +2 -2
- package/cjs/model/gpt-4-turbo-preview.js +4 -2
- package/cjs/model/gpt-4-turbo-preview.js.map +1 -1
- package/cjs/model/gpt-4-turbo.d.ts +2 -2
- package/cjs/model/gpt-4-turbo.js +4 -2
- package/cjs/model/gpt-4-turbo.js.map +1 -1
- package/cjs/model/gpt-4-vision-preview.d.ts +2 -2
- package/cjs/model/gpt-4-vision-preview.js +4 -2
- package/cjs/model/gpt-4-vision-preview.js.map +1 -1
- package/cjs/model/gpt-4.d.ts +2 -2
- package/cjs/model/gpt-4.js +4 -2
- package/cjs/model/gpt-4.js.map +1 -1
- package/cjs/model/gpt-4o-2024-05-13.d.ts +2 -2
- package/cjs/model/gpt-4o-2024-05-13.js +4 -2
- package/cjs/model/gpt-4o-2024-05-13.js.map +1 -1
- package/cjs/model/gpt-4o-2024-08-06-finetune.d.ts +2 -2
- package/cjs/model/gpt-4o-2024-08-06-finetune.js +4 -2
- package/cjs/model/gpt-4o-2024-08-06-finetune.js.map +1 -1
- package/cjs/model/gpt-4o-2024-08-06.d.ts +2 -2
- package/cjs/model/gpt-4o-2024-08-06.js +4 -2
- package/cjs/model/gpt-4o-2024-08-06.js.map +1 -1
- package/cjs/model/gpt-4o-audio-preview-2024-10-01.d.ts +2 -2
- package/cjs/model/gpt-4o-audio-preview-2024-10-01.js +4 -2
- package/cjs/model/gpt-4o-audio-preview-2024-10-01.js.map +1 -1
- package/cjs/model/gpt-4o-audio-preview.d.ts +2 -2
- package/cjs/model/gpt-4o-audio-preview.js +4 -2
- package/cjs/model/gpt-4o-audio-preview.js.map +1 -1
- package/cjs/model/gpt-4o-mini-2024-07-18-finetune.d.ts +2 -2
- package/cjs/model/gpt-4o-mini-2024-07-18-finetune.js +4 -2
- package/cjs/model/gpt-4o-mini-2024-07-18-finetune.js.map +1 -1
- package/cjs/model/gpt-4o-mini-2024-07-18.d.ts +2 -2
- package/cjs/model/gpt-4o-mini-2024-07-18.js +4 -2
- package/cjs/model/gpt-4o-mini-2024-07-18.js.map +1 -1
- package/cjs/model/gpt-4o-mini-training-2024-07-18.d.ts +2 -2
- package/cjs/model/gpt-4o-mini-training-2024-07-18.js +4 -2
- package/cjs/model/gpt-4o-mini-training-2024-07-18.js.map +1 -1
- package/cjs/model/gpt-4o-mini-training.d.ts +2 -2
- package/cjs/model/gpt-4o-mini-training.js +4 -2
- package/cjs/model/gpt-4o-mini-training.js.map +1 -1
- package/cjs/model/gpt-4o-mini.d.ts +2 -2
- package/cjs/model/gpt-4o-mini.js +4 -2
- package/cjs/model/gpt-4o-mini.js.map +1 -1
- package/cjs/model/gpt-4o-realtime-preview-2024-10-01.d.ts +2 -2
- package/cjs/model/gpt-4o-realtime-preview-2024-10-01.js +4 -2
- package/cjs/model/gpt-4o-realtime-preview-2024-10-01.js.map +1 -1
- package/cjs/model/gpt-4o-realtime-preview.d.ts +2 -2
- package/cjs/model/gpt-4o-realtime-preview.js +4 -2
- package/cjs/model/gpt-4o-realtime-preview.js.map +1 -1
- package/cjs/model/gpt-4o.d.ts +2 -2
- package/cjs/model/gpt-4o.js +4 -2
- package/cjs/model/gpt-4o.js.map +1 -1
- package/cjs/modelParams.d.ts +0 -3
- package/cjs/modelParams.js +10 -17
- package/cjs/modelParams.js.map +1 -1
- package/dist/cl100k_base.js +1 -1
- package/dist/cl100k_base.js.map +1 -1
- package/dist/o200k_base.js +1 -1
- package/dist/o200k_base.js.map +1 -1
- package/dist/p50k_base.js +1 -1
- package/dist/p50k_base.js.map +1 -1
- package/dist/p50k_edit.js +1 -1
- package/dist/p50k_edit.js.map +1 -1
- package/dist/r50k_base.js +1 -1
- package/dist/r50k_base.js.map +1 -1
- package/esm/BytePairEncodingCore.d.ts +13 -1
- package/esm/BytePairEncodingCore.js +81 -6
- package/esm/BytePairEncodingCore.js.map +1 -1
- package/esm/GptEncoding.d.ts +3 -4
- package/esm/GptEncoding.js +22 -15
- package/esm/GptEncoding.js.map +1 -1
- package/esm/constants.d.ts +1 -0
- package/esm/constants.js +1 -0
- package/esm/constants.js.map +1 -1
- package/esm/encoding/cl100k_base.d.ts +2 -2
- package/esm/encoding/cl100k_base.js +2 -2
- package/esm/encoding/cl100k_base.js.map +1 -1
- package/esm/encoding/o200k_base.d.ts +2 -2
- package/esm/encoding/o200k_base.js +2 -2
- package/esm/encoding/o200k_base.js.map +1 -1
- package/esm/encoding/p50k_base.d.ts +2 -2
- package/esm/encoding/p50k_base.js +2 -2
- package/esm/encoding/p50k_base.js.map +1 -1
- package/esm/encoding/p50k_edit.d.ts +2 -2
- package/esm/encoding/p50k_edit.js +2 -2
- package/esm/encoding/p50k_edit.js.map +1 -1
- package/esm/encoding/r50k_base.d.ts +2 -2
- package/esm/encoding/r50k_base.js +2 -2
- package/esm/encoding/r50k_base.js.map +1 -1
- package/esm/encodingParams/{Cl100KBase.js → cl100k_base.js} +3 -2
- package/esm/encodingParams/cl100k_base.js.map +1 -0
- package/esm/encodingParams/constants.d.ts +2 -0
- package/esm/encodingParams/constants.js +3 -0
- package/esm/encodingParams/constants.js.map +1 -0
- package/esm/encodingParams/{O200KBase.js → o200k_base.js} +3 -2
- package/esm/encodingParams/o200k_base.js.map +1 -0
- package/esm/encodingParams/{P50KBase.js → p50k_base.js} +4 -3
- package/esm/encodingParams/p50k_base.js.map +1 -0
- package/esm/encodingParams/{P50KEdit.js → p50k_edit.js} +4 -3
- package/esm/encodingParams/p50k_edit.js.map +1 -0
- package/esm/encodingParams/{R50KBase.js → r50k_base.js} +4 -3
- package/esm/encodingParams/r50k_base.js.map +1 -0
- package/esm/extraApis.test.d.ts +1 -0
- package/esm/extraApis.test.js +55 -0
- package/esm/extraApis.test.js.map +1 -0
- package/esm/model/gpt-3.5-turbo-0125.d.ts +2 -2
- package/esm/model/gpt-3.5-turbo-0125.js +2 -2
- package/esm/model/gpt-3.5-turbo-0125.js.map +1 -1
- package/esm/model/gpt-3.5-turbo-0301.d.ts +2 -2
- package/esm/model/gpt-3.5-turbo-0301.js +2 -2
- package/esm/model/gpt-3.5-turbo-0301.js.map +1 -1
- package/esm/model/gpt-3.5-turbo-0613.d.ts +2 -2
- package/esm/model/gpt-3.5-turbo-0613.js +2 -2
- package/esm/model/gpt-3.5-turbo-0613.js.map +1 -1
- package/esm/model/gpt-3.5-turbo-1106.d.ts +2 -2
- package/esm/model/gpt-3.5-turbo-1106.js +2 -2
- package/esm/model/gpt-3.5-turbo-1106.js.map +1 -1
- package/esm/model/gpt-3.5-turbo-16k-0613.d.ts +2 -2
- package/esm/model/gpt-3.5-turbo-16k-0613.js +2 -2
- package/esm/model/gpt-3.5-turbo-16k-0613.js.map +1 -1
- package/esm/model/gpt-3.5-turbo-16k.d.ts +2 -2
- package/esm/model/gpt-3.5-turbo-16k.js +2 -2
- package/esm/model/gpt-3.5-turbo-16k.js.map +1 -1
- package/esm/model/gpt-3.5-turbo-finetune.d.ts +2 -2
- package/esm/model/gpt-3.5-turbo-finetune.js +2 -2
- package/esm/model/gpt-3.5-turbo-finetune.js.map +1 -1
- package/esm/model/gpt-3.5-turbo.d.ts +2 -2
- package/esm/model/gpt-3.5-turbo.js +2 -2
- package/esm/model/gpt-3.5-turbo.js.map +1 -1
- package/esm/model/gpt-4-0125-preview.d.ts +2 -2
- package/esm/model/gpt-4-0125-preview.js +2 -2
- package/esm/model/gpt-4-0125-preview.js.map +1 -1
- package/esm/model/gpt-4-0314.d.ts +2 -2
- package/esm/model/gpt-4-0314.js +2 -2
- package/esm/model/gpt-4-0314.js.map +1 -1
- package/esm/model/gpt-4-0613.d.ts +2 -2
- package/esm/model/gpt-4-0613.js +2 -2
- package/esm/model/gpt-4-0613.js.map +1 -1
- package/esm/model/gpt-4-1106-preview.d.ts +2 -2
- package/esm/model/gpt-4-1106-preview.js +2 -2
- package/esm/model/gpt-4-1106-preview.js.map +1 -1
- package/esm/model/gpt-4-1106-vision-preview.d.ts +2 -2
- package/esm/model/gpt-4-1106-vision-preview.js +2 -2
- package/esm/model/gpt-4-1106-vision-preview.js.map +1 -1
- package/esm/model/gpt-4-32k-0314.d.ts +2 -2
- package/esm/model/gpt-4-32k-0314.js +2 -2
- package/esm/model/gpt-4-32k-0314.js.map +1 -1
- package/esm/model/gpt-4-32k-0613.d.ts +2 -2
- package/esm/model/gpt-4-32k-0613.js +2 -2
- package/esm/model/gpt-4-32k-0613.js.map +1 -1
- package/esm/model/gpt-4-32k.d.ts +2 -2
- package/esm/model/gpt-4-32k.js +2 -2
- package/esm/model/gpt-4-32k.js.map +1 -1
- package/esm/model/gpt-4-turbo-2024-04-09.d.ts +2 -2
- package/esm/model/gpt-4-turbo-2024-04-09.js +2 -2
- package/esm/model/gpt-4-turbo-2024-04-09.js.map +1 -1
- package/esm/model/gpt-4-turbo-preview.d.ts +2 -2
- package/esm/model/gpt-4-turbo-preview.js +2 -2
- package/esm/model/gpt-4-turbo-preview.js.map +1 -1
- package/esm/model/gpt-4-turbo.d.ts +2 -2
- package/esm/model/gpt-4-turbo.js +2 -2
- package/esm/model/gpt-4-turbo.js.map +1 -1
- package/esm/model/gpt-4-vision-preview.d.ts +2 -2
- package/esm/model/gpt-4-vision-preview.js +2 -2
- package/esm/model/gpt-4-vision-preview.js.map +1 -1
- package/esm/model/gpt-4.d.ts +2 -2
- package/esm/model/gpt-4.js +2 -2
- package/esm/model/gpt-4.js.map +1 -1
- package/esm/model/gpt-4o-2024-05-13.d.ts +2 -2
- package/esm/model/gpt-4o-2024-05-13.js +2 -2
- package/esm/model/gpt-4o-2024-05-13.js.map +1 -1
- package/esm/model/gpt-4o-2024-08-06-finetune.d.ts +2 -2
- package/esm/model/gpt-4o-2024-08-06-finetune.js +2 -2
- package/esm/model/gpt-4o-2024-08-06-finetune.js.map +1 -1
- package/esm/model/gpt-4o-2024-08-06.d.ts +2 -2
- package/esm/model/gpt-4o-2024-08-06.js +2 -2
- package/esm/model/gpt-4o-2024-08-06.js.map +1 -1
- package/esm/model/gpt-4o-audio-preview-2024-10-01.d.ts +2 -2
- package/esm/model/gpt-4o-audio-preview-2024-10-01.js +2 -2
- package/esm/model/gpt-4o-audio-preview-2024-10-01.js.map +1 -1
- package/esm/model/gpt-4o-audio-preview.d.ts +2 -2
- package/esm/model/gpt-4o-audio-preview.js +2 -2
- package/esm/model/gpt-4o-audio-preview.js.map +1 -1
- package/esm/model/gpt-4o-mini-2024-07-18-finetune.d.ts +2 -2
- package/esm/model/gpt-4o-mini-2024-07-18-finetune.js +2 -2
- package/esm/model/gpt-4o-mini-2024-07-18-finetune.js.map +1 -1
- package/esm/model/gpt-4o-mini-2024-07-18.d.ts +2 -2
- package/esm/model/gpt-4o-mini-2024-07-18.js +2 -2
- package/esm/model/gpt-4o-mini-2024-07-18.js.map +1 -1
- package/esm/model/gpt-4o-mini-training-2024-07-18.d.ts +2 -2
- package/esm/model/gpt-4o-mini-training-2024-07-18.js +2 -2
- package/esm/model/gpt-4o-mini-training-2024-07-18.js.map +1 -1
- package/esm/model/gpt-4o-mini-training.d.ts +2 -2
- package/esm/model/gpt-4o-mini-training.js +2 -2
- package/esm/model/gpt-4o-mini-training.js.map +1 -1
- package/esm/model/gpt-4o-mini.d.ts +2 -2
- package/esm/model/gpt-4o-mini.js +2 -2
- package/esm/model/gpt-4o-mini.js.map +1 -1
- package/esm/model/gpt-4o-realtime-preview-2024-10-01.d.ts +2 -2
- package/esm/model/gpt-4o-realtime-preview-2024-10-01.js +2 -2
- package/esm/model/gpt-4o-realtime-preview-2024-10-01.js.map +1 -1
- package/esm/model/gpt-4o-realtime-preview.d.ts +2 -2
- package/esm/model/gpt-4o-realtime-preview.js +2 -2
- package/esm/model/gpt-4o-realtime-preview.js.map +1 -1
- package/esm/model/gpt-4o.d.ts +2 -2
- package/esm/model/gpt-4o.js +2 -2
- package/esm/model/gpt-4o.js.map +1 -1
- package/esm/modelParams.d.ts +0 -3
- package/esm/modelParams.js +5 -10
- package/esm/modelParams.js.map +1 -1
- package/package.json +1 -1
- package/src/BytePairEncodingCore.ts +108 -8
- package/src/GptEncoding.ts +31 -31
- package/src/constants.ts +1 -0
- package/src/encoding/cl100k_base.ts +2 -0
- package/src/encoding/o200k_base.ts +2 -0
- package/src/encoding/p50k_base.ts +2 -0
- package/src/encoding/p50k_edit.ts +2 -0
- package/src/encoding/r50k_base.ts +2 -0
- package/src/encodingParams/{Cl100KBase.ts → cl100k_base.ts} +2 -2
- package/src/encodingParams/constants.ts +5 -0
- package/src/encodingParams/{O200KBase.ts → o200k_base.ts} +2 -2
- package/src/encodingParams/{P50KBase.ts → p50k_base.ts} +3 -2
- package/src/encodingParams/{P50KEdit.ts → p50k_edit.ts} +3 -2
- package/src/encodingParams/{R50KBase.ts → r50k_base.ts} +3 -2
- package/src/extraApis.test.ts +74 -0
- package/src/model/gpt-3.5-turbo-0125.ts +4 -0
- package/src/model/gpt-3.5-turbo-0301.ts +4 -0
- package/src/model/gpt-3.5-turbo-0613.ts +4 -0
- package/src/model/gpt-3.5-turbo-1106.ts +4 -0
- package/src/model/gpt-3.5-turbo-16k-0613.ts +4 -0
- package/src/model/gpt-3.5-turbo-16k.ts +4 -0
- package/src/model/gpt-3.5-turbo-finetune.ts +4 -0
- package/src/model/gpt-3.5-turbo.ts +4 -0
- package/src/model/gpt-4-0125-preview.ts +4 -0
- package/src/model/gpt-4-0314.ts +4 -0
- package/src/model/gpt-4-0613.ts +4 -0
- package/src/model/gpt-4-1106-preview.ts +4 -0
- package/src/model/gpt-4-1106-vision-preview.ts +4 -0
- package/src/model/gpt-4-32k-0314.ts +4 -0
- package/src/model/gpt-4-32k-0613.ts +4 -0
- package/src/model/gpt-4-32k.ts +4 -0
- package/src/model/gpt-4-turbo-2024-04-09.ts +4 -0
- package/src/model/gpt-4-turbo-preview.ts +4 -0
- package/src/model/gpt-4-turbo.ts +4 -0
- package/src/model/gpt-4-vision-preview.ts +4 -0
- package/src/model/gpt-4.ts +4 -0
- package/src/model/gpt-4o-2024-05-13.ts +4 -0
- package/src/model/gpt-4o-2024-08-06-finetune.ts +4 -0
- package/src/model/gpt-4o-2024-08-06.ts +4 -0
- package/src/model/gpt-4o-audio-preview-2024-10-01.ts +4 -0
- package/src/model/gpt-4o-audio-preview.ts +4 -0
- package/src/model/gpt-4o-mini-2024-07-18-finetune.ts +4 -0
- package/src/model/gpt-4o-mini-2024-07-18.ts +4 -0
- package/src/model/gpt-4o-mini-training-2024-07-18.ts +4 -0
- package/src/model/gpt-4o-mini-training.ts +4 -0
- package/src/model/gpt-4o-mini.ts +4 -0
- package/src/model/gpt-4o-realtime-preview-2024-10-01.ts +4 -0
- package/src/model/gpt-4o-realtime-preview.ts +4 -0
- package/src/model/gpt-4o.ts +4 -0
- package/src/modelParams.ts +5 -19
- package/cjs/encodingParams/Cl100KBase.js.map +0 -1
- package/cjs/encodingParams/O200KBase.js.map +0 -1
- package/cjs/encodingParams/P50KBase.js.map +0 -1
- package/cjs/encodingParams/P50KEdit.js.map +0 -1
- package/cjs/encodingParams/R50KBase.js.map +0 -1
- package/esm/encodingParams/Cl100KBase.js.map +0 -1
- package/esm/encodingParams/O200KBase.js.map +0 -1
- package/esm/encodingParams/P50KBase.js.map +0 -1
- package/esm/encodingParams/P50KEdit.js.map +0 -1
- package/esm/encodingParams/R50KBase.js.map +0 -1
- /package/cjs/encodingParams/{Cl100KBase.d.ts → cl100k_base.d.ts} +0 -0
- /package/cjs/encodingParams/{O200KBase.d.ts → o200k_base.d.ts} +0 -0
- /package/cjs/encodingParams/{P50KBase.d.ts → p50k_base.d.ts} +0 -0
- /package/cjs/encodingParams/{P50KEdit.d.ts → p50k_edit.d.ts} +0 -0
- /package/cjs/encodingParams/{R50KBase.d.ts → r50k_base.d.ts} +0 -0
- /package/esm/encodingParams/{Cl100KBase.d.ts → cl100k_base.d.ts} +0 -0
- /package/esm/encodingParams/{O200KBase.d.ts → o200k_base.d.ts} +0 -0
- /package/esm/encodingParams/{P50KBase.d.ts → p50k_base.d.ts} +0 -0
- /package/esm/encodingParams/{P50KEdit.d.ts → p50k_edit.d.ts} +0 -0
- /package/esm/encodingParams/{R50KBase.d.ts → r50k_base.d.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-continue */
|
|
2
2
|
|
|
3
|
+
import { DEFAULT_MERGE_CACHE_SIZE } from './constants.js'
|
|
3
4
|
import { compareUint8Arrays, isAscii, tryConvertToString } from './utfUtil.js'
|
|
4
5
|
import { escapeRegExp } from './util.js'
|
|
5
6
|
|
|
@@ -9,6 +10,13 @@ export interface BytePairEncodingConfig {
|
|
|
9
10
|
bytePairRankDecoder: RawBytePairRanks
|
|
10
11
|
specialTokensEncoder?: Map<string, number>
|
|
11
12
|
tokenSplitRegex: RegExp
|
|
13
|
+
/**
|
|
14
|
+
* LRU cache for merged tokens pairs.
|
|
15
|
+
* Increasing this value should make encoding similar strings faster,
|
|
16
|
+
* but will consume more memory.
|
|
17
|
+
* @default 100000
|
|
18
|
+
*/
|
|
19
|
+
mergeCacheSize?: number
|
|
12
20
|
}
|
|
13
21
|
|
|
14
22
|
const emptyBuffer = new Uint8Array(0)
|
|
@@ -36,14 +44,21 @@ export class BytePairEncodingCore {
|
|
|
36
44
|
private specialTokensDecoder: Map<number, string>
|
|
37
45
|
private specialTokenPatternRegex: RegExp
|
|
38
46
|
private textEncoder = new TextEncoder()
|
|
47
|
+
private mergeCache?: Map<string, number[]>
|
|
48
|
+
private mergeCacheSize: number
|
|
39
49
|
|
|
40
50
|
constructor({
|
|
41
51
|
bytePairRankDecoder,
|
|
42
52
|
specialTokensEncoder,
|
|
43
53
|
tokenSplitRegex,
|
|
54
|
+
mergeCacheSize = DEFAULT_MERGE_CACHE_SIZE,
|
|
44
55
|
}: BytePairEncodingConfig) {
|
|
45
56
|
this.bytePairRankDecoder = bytePairRankDecoder
|
|
46
57
|
this.bytePairStringRankEncoder = new Map<string, number>()
|
|
58
|
+
this.mergeCacheSize = mergeCacheSize
|
|
59
|
+
if (mergeCacheSize > 0) {
|
|
60
|
+
this.mergeCache = new Map()
|
|
61
|
+
}
|
|
47
62
|
|
|
48
63
|
// size without array holes (which may be present in the encoder)
|
|
49
64
|
this.mergeableBytePairRankCount = Object.keys(bytePairRankDecoder).length
|
|
@@ -73,12 +88,22 @@ export class BytePairEncodingCore {
|
|
|
73
88
|
)
|
|
74
89
|
const allSpecialTokensRegex = escapedSpecialTokens.join('|')
|
|
75
90
|
try {
|
|
76
|
-
this.specialTokenPatternRegex = new RegExp(allSpecialTokensRegex)
|
|
91
|
+
this.specialTokenPatternRegex = new RegExp(allSpecialTokensRegex, 'y')
|
|
77
92
|
} catch {
|
|
78
93
|
throw new Error('Invalid regular expression pattern.')
|
|
79
94
|
}
|
|
80
95
|
}
|
|
81
96
|
|
|
97
|
+
setMergeCacheSize(newSize: number): void {
|
|
98
|
+
if (this.mergeCacheSize === 0 && newSize > 0) {
|
|
99
|
+
this.mergeCache = new Map()
|
|
100
|
+
}
|
|
101
|
+
this.mergeCacheSize = newSize
|
|
102
|
+
if (newSize === 0) {
|
|
103
|
+
this.mergeCache = undefined
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
82
107
|
*encodeNativeGenerator(
|
|
83
108
|
text: string,
|
|
84
109
|
allowedSpecial?: Set<string>,
|
|
@@ -96,7 +121,10 @@ export class BytePairEncodingCore {
|
|
|
96
121
|
|
|
97
122
|
const endIndex = nextSpecialStartIndex ?? text.length
|
|
98
123
|
|
|
99
|
-
const textBeforeSpecial =
|
|
124
|
+
const textBeforeSpecial =
|
|
125
|
+
startIndex === 0 && endIndex === text.length
|
|
126
|
+
? text
|
|
127
|
+
: text.slice(startIndex, endIndex)
|
|
100
128
|
|
|
101
129
|
for (const [match] of textBeforeSpecial.matchAll(this.tokenSplitRegex)) {
|
|
102
130
|
const token = this.getBpeRankFromString(match)
|
|
@@ -146,7 +174,10 @@ export class BytePairEncodingCore {
|
|
|
146
174
|
|
|
147
175
|
const endIndex = nextSpecialStartIndex ?? text.length
|
|
148
176
|
|
|
149
|
-
const textBeforeSpecial =
|
|
177
|
+
const textBeforeSpecial =
|
|
178
|
+
startIndex === 0 && endIndex === text.length
|
|
179
|
+
? text
|
|
180
|
+
: text.slice(startIndex, endIndex)
|
|
150
181
|
|
|
151
182
|
for (const [match] of textBeforeSpecial.matchAll(this.tokenSplitRegex)) {
|
|
152
183
|
const token = this.getBpeRankFromString(match)
|
|
@@ -178,6 +209,56 @@ export class BytePairEncodingCore {
|
|
|
178
209
|
return tokensArray
|
|
179
210
|
}
|
|
180
211
|
|
|
212
|
+
countNative(text: string, allowedSpecial?: Set<string>): number {
|
|
213
|
+
let startIndex = 0
|
|
214
|
+
let tokensCount = 0
|
|
215
|
+
|
|
216
|
+
// eslint-disable-next-line no-constant-condition
|
|
217
|
+
while (true) {
|
|
218
|
+
const nextSpecialMatch = this.findNextSpecialToken(
|
|
219
|
+
text,
|
|
220
|
+
allowedSpecial,
|
|
221
|
+
startIndex,
|
|
222
|
+
)
|
|
223
|
+
const nextSpecialStartIndex = nextSpecialMatch?.[0]
|
|
224
|
+
|
|
225
|
+
const endIndex = nextSpecialStartIndex ?? text.length
|
|
226
|
+
|
|
227
|
+
const textBeforeSpecial =
|
|
228
|
+
startIndex === 0 && endIndex === text.length
|
|
229
|
+
? text
|
|
230
|
+
: text.slice(startIndex, endIndex)
|
|
231
|
+
|
|
232
|
+
for (const [match] of textBeforeSpecial.matchAll(this.tokenSplitRegex)) {
|
|
233
|
+
const token = this.getBpeRankFromString(match)
|
|
234
|
+
if (token !== undefined) {
|
|
235
|
+
tokensCount++
|
|
236
|
+
|
|
237
|
+
continue
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const tokens = this.bytePairEncode(match)
|
|
241
|
+
tokensCount += tokens.length
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (nextSpecialStartIndex !== undefined) {
|
|
245
|
+
const specialToken = nextSpecialMatch![1]
|
|
246
|
+
const specialTokenValue = this.specialTokensEncoder.get(specialToken)
|
|
247
|
+
if (specialTokenValue === undefined) {
|
|
248
|
+
throw new Error(
|
|
249
|
+
`Special token "${specialToken}" is not in the special token encoder.`,
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
tokensCount++
|
|
253
|
+
startIndex = nextSpecialStartIndex + specialToken.length
|
|
254
|
+
} else {
|
|
255
|
+
break
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return tokensCount
|
|
260
|
+
}
|
|
261
|
+
|
|
181
262
|
*decodeNativeGenerator(
|
|
182
263
|
tokens: Iterable<number>,
|
|
183
264
|
): Generator<Uint8Array | string, void, void> {
|
|
@@ -308,9 +389,8 @@ export class BytePairEncodingCore {
|
|
|
308
389
|
|
|
309
390
|
// eslint-disable-next-line no-constant-condition
|
|
310
391
|
while (true) {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
)
|
|
392
|
+
this.specialTokenPatternRegex.lastIndex = searchIndex
|
|
393
|
+
const nextSpecialMatch = this.specialTokenPatternRegex.exec(text)
|
|
314
394
|
|
|
315
395
|
if (!nextSpecialMatch) {
|
|
316
396
|
return undefined
|
|
@@ -341,14 +421,34 @@ export class BytePairEncodingCore {
|
|
|
341
421
|
return this.specialTokensDecoder.get(tokenRank)
|
|
342
422
|
}
|
|
343
423
|
|
|
424
|
+
private addToMergeCache(key: string, value: number[]): void {
|
|
425
|
+
if (!this.mergeCache) return
|
|
426
|
+
|
|
427
|
+
if (this.mergeCache.size >= this.mergeCacheSize) {
|
|
428
|
+
// Remove least recently used item (first item)
|
|
429
|
+
const firstKey = this.mergeCache.keys().next().value!
|
|
430
|
+
this.mergeCache.delete(firstKey)
|
|
431
|
+
}
|
|
432
|
+
this.mergeCache.set(key, value)
|
|
433
|
+
}
|
|
434
|
+
|
|
344
435
|
private bytePairEncode(input: string): number[] {
|
|
345
436
|
if (input.length === 1 && isAscii(input.codePointAt(0)!)) {
|
|
346
437
|
return [this.getBpeRankFromStringOrThrow(input)]
|
|
347
438
|
}
|
|
348
439
|
|
|
349
|
-
|
|
440
|
+
if (this.mergeCache?.has(input)) {
|
|
441
|
+
const result = this.mergeCache.get(input)!
|
|
442
|
+
// Move to end to mark as recently used
|
|
443
|
+
this.mergeCache.delete(input)
|
|
444
|
+
this.mergeCache.set(input, result)
|
|
445
|
+
return result
|
|
446
|
+
}
|
|
350
447
|
|
|
351
|
-
|
|
448
|
+
const inputBytes = this.textEncoder.encode(input)
|
|
449
|
+
const result = this.bytePairMerge(inputBytes)
|
|
450
|
+
this.addToMergeCache(input, result)
|
|
451
|
+
return result
|
|
352
452
|
}
|
|
353
453
|
|
|
354
454
|
private bytePairMerge(
|
package/src/GptEncoding.ts
CHANGED
|
@@ -12,10 +12,8 @@ import {
|
|
|
12
12
|
} from './mapping.js'
|
|
13
13
|
import {
|
|
14
14
|
type EncodingParams,
|
|
15
|
-
type GetMergeableRanksAsyncFn,
|
|
16
15
|
type GetMergeableRanksFn,
|
|
17
16
|
getEncodingParams,
|
|
18
|
-
getModelParamsAsync,
|
|
19
17
|
} from './modelParams.js'
|
|
20
18
|
import {
|
|
21
19
|
EndOfPrompt,
|
|
@@ -129,6 +127,7 @@ export class GptEncoding {
|
|
|
129
127
|
this.encodeChat = this.encodeChat.bind(this)
|
|
130
128
|
this.encodeChatGenerator = this.encodeChatGenerator.bind(this)
|
|
131
129
|
this.countTokens = this.countTokens.bind(this)
|
|
130
|
+
this.setMergeCacheSize = this.setMergeCacheSize.bind(this)
|
|
132
131
|
this.modelName = modelName
|
|
133
132
|
}
|
|
134
133
|
|
|
@@ -149,29 +148,6 @@ export class GptEncoding {
|
|
|
149
148
|
return new GptEncoding({ ...modelParams, modelName })
|
|
150
149
|
}
|
|
151
150
|
|
|
152
|
-
static async getEncodingApiAsync(
|
|
153
|
-
encodingName: EncodingName,
|
|
154
|
-
getMergeableRanks: GetMergeableRanksAsyncFn,
|
|
155
|
-
): Promise<GptEncoding> {
|
|
156
|
-
const modelParams = await getModelParamsAsync(
|
|
157
|
-
encodingName,
|
|
158
|
-
getMergeableRanks,
|
|
159
|
-
)
|
|
160
|
-
return new GptEncoding(modelParams)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
static async getEncodingApiForModelAsync(
|
|
164
|
-
modelName: ModelName,
|
|
165
|
-
getMergeableRanks: GetMergeableRanksAsyncFn,
|
|
166
|
-
): Promise<GptEncoding> {
|
|
167
|
-
const encodingName = modelToEncodingMap[modelName]
|
|
168
|
-
const modelParams = await getModelParamsAsync(
|
|
169
|
-
encodingName,
|
|
170
|
-
getMergeableRanks,
|
|
171
|
-
)
|
|
172
|
-
return new GptEncoding({ ...modelParams, modelName })
|
|
173
|
-
}
|
|
174
|
-
|
|
175
151
|
private processSpecialTokens({
|
|
176
152
|
allowedSpecial,
|
|
177
153
|
disallowedSpecial,
|
|
@@ -210,7 +186,9 @@ export class GptEncoding {
|
|
|
210
186
|
allowedSpecial.forEach((val) => disallowedSpecialSet.delete(val))
|
|
211
187
|
// disallowed takes precedence over allowed
|
|
212
188
|
disallowedSpecial.forEach((val) => allowedSpecial.delete(val))
|
|
213
|
-
|
|
189
|
+
if (disallowedSpecial.size > 0) {
|
|
190
|
+
regexPattern = getSpecialTokenRegex(disallowedSpecial)
|
|
191
|
+
}
|
|
214
192
|
} else {
|
|
215
193
|
regexPattern = this.allSpecialTokenRegex
|
|
216
194
|
}
|
|
@@ -354,11 +332,29 @@ export class GptEncoding {
|
|
|
354
332
|
* Counts the number of tokens in the input.
|
|
355
333
|
* @returns {number} The number of tokens.
|
|
356
334
|
*/
|
|
357
|
-
countTokens(
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
335
|
+
countTokens(
|
|
336
|
+
input: string | Iterable<ChatMessage>,
|
|
337
|
+
encodeOptions?: EncodeOptions,
|
|
338
|
+
): number {
|
|
339
|
+
if (typeof input === 'string') {
|
|
340
|
+
const specialTokenConfig = encodeOptions
|
|
341
|
+
? this.processSpecialTokens(encodeOptions)
|
|
342
|
+
: this.defaultSpecialTokenConfig
|
|
343
|
+
|
|
344
|
+
if (specialTokenConfig.regexPattern) {
|
|
345
|
+
const match = input.match(specialTokenConfig.regexPattern)
|
|
346
|
+
if (match !== null) {
|
|
347
|
+
throw new Error(`Disallowed special token found: ${match[0]}`)
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return this.bytePairEncodingCoreProcessor.countNative(
|
|
352
|
+
input,
|
|
353
|
+
specialTokenConfig.allowedSpecial,
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const tokenGenerator = this.encodeChatGenerator(input)
|
|
362
358
|
let count = 0
|
|
363
359
|
for (const tokens of tokenGenerator) {
|
|
364
360
|
count += tokens.length
|
|
@@ -366,6 +362,10 @@ export class GptEncoding {
|
|
|
366
362
|
return count
|
|
367
363
|
}
|
|
368
364
|
|
|
365
|
+
setMergeCacheSize(size: number): void {
|
|
366
|
+
this.bytePairEncodingCoreProcessor.setMergeCacheSize(size)
|
|
367
|
+
}
|
|
368
|
+
|
|
369
369
|
decode(inputTokensToDecode: Iterable<number>): string {
|
|
370
370
|
return this.bytePairEncodingCoreProcessor.decodeNative(inputTokensToDecode)
|
|
371
371
|
}
|
package/src/constants.ts
CHANGED
|
@@ -17,6 +17,7 @@ const {
|
|
|
17
17
|
encodeChat,
|
|
18
18
|
encodeChatGenerator,
|
|
19
19
|
vocabularySize,
|
|
20
|
+
setMergeCacheSize,
|
|
20
21
|
} = api
|
|
21
22
|
export {
|
|
22
23
|
countTokens,
|
|
@@ -28,6 +29,7 @@ export {
|
|
|
28
29
|
encodeChatGenerator,
|
|
29
30
|
encodeGenerator,
|
|
30
31
|
isWithinTokenLimit,
|
|
32
|
+
setMergeCacheSize,
|
|
31
33
|
vocabularySize,
|
|
32
34
|
}
|
|
33
35
|
// eslint-disable-next-line import/no-default-export
|
|
@@ -17,6 +17,7 @@ const {
|
|
|
17
17
|
encodeChat,
|
|
18
18
|
encodeChatGenerator,
|
|
19
19
|
vocabularySize,
|
|
20
|
+
setMergeCacheSize,
|
|
20
21
|
} = api
|
|
21
22
|
export {
|
|
22
23
|
countTokens,
|
|
@@ -28,6 +29,7 @@ export {
|
|
|
28
29
|
encodeChatGenerator,
|
|
29
30
|
encodeGenerator,
|
|
30
31
|
isWithinTokenLimit,
|
|
32
|
+
setMergeCacheSize,
|
|
31
33
|
vocabularySize,
|
|
32
34
|
}
|
|
33
35
|
// eslint-disable-next-line import/no-default-export
|
|
@@ -15,6 +15,7 @@ const {
|
|
|
15
15
|
isWithinTokenLimit,
|
|
16
16
|
countTokens,
|
|
17
17
|
vocabularySize,
|
|
18
|
+
setMergeCacheSize,
|
|
18
19
|
} = api
|
|
19
20
|
export {
|
|
20
21
|
countTokens,
|
|
@@ -24,6 +25,7 @@ export {
|
|
|
24
25
|
encode,
|
|
25
26
|
encodeGenerator,
|
|
26
27
|
isWithinTokenLimit,
|
|
28
|
+
setMergeCacheSize,
|
|
27
29
|
vocabularySize,
|
|
28
30
|
}
|
|
29
31
|
// eslint-disable-next-line import/no-default-export
|
|
@@ -15,6 +15,7 @@ const {
|
|
|
15
15
|
isWithinTokenLimit,
|
|
16
16
|
countTokens,
|
|
17
17
|
vocabularySize,
|
|
18
|
+
setMergeCacheSize,
|
|
18
19
|
} = api
|
|
19
20
|
export {
|
|
20
21
|
countTokens,
|
|
@@ -24,6 +25,7 @@ export {
|
|
|
24
25
|
encode,
|
|
25
26
|
encodeGenerator,
|
|
26
27
|
isWithinTokenLimit,
|
|
28
|
+
setMergeCacheSize,
|
|
27
29
|
vocabularySize,
|
|
28
30
|
}
|
|
29
31
|
// eslint-disable-next-line import/no-default-export
|
|
@@ -15,6 +15,7 @@ const {
|
|
|
15
15
|
isWithinTokenLimit,
|
|
16
16
|
countTokens,
|
|
17
17
|
vocabularySize,
|
|
18
|
+
setMergeCacheSize,
|
|
18
19
|
} = api
|
|
19
20
|
export {
|
|
20
21
|
countTokens,
|
|
@@ -24,6 +25,7 @@ export {
|
|
|
24
25
|
encode,
|
|
25
26
|
encodeGenerator,
|
|
26
27
|
isWithinTokenLimit,
|
|
28
|
+
setMergeCacheSize,
|
|
27
29
|
vocabularySize,
|
|
28
30
|
}
|
|
29
31
|
// eslint-disable-next-line import/no-default-export
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
ImSep,
|
|
12
12
|
ImStart,
|
|
13
13
|
} from '../specialTokens.js'
|
|
14
|
+
import { CL_AND_O_TOKEN_SPLIT_PATTERN } from './constants.js'
|
|
14
15
|
|
|
15
16
|
export function Cl100KBase(
|
|
16
17
|
bytePairRankDecoder: RawBytePairRanks,
|
|
@@ -27,8 +28,7 @@ export function Cl100KBase(
|
|
|
27
28
|
])
|
|
28
29
|
|
|
29
30
|
return {
|
|
30
|
-
tokenSplitRegex:
|
|
31
|
-
/(?:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+/giu,
|
|
31
|
+
tokenSplitRegex: CL_AND_O_TOKEN_SPLIT_PATTERN,
|
|
32
32
|
bytePairRankDecoder,
|
|
33
33
|
specialTokensEncoder: specialTokenMapping,
|
|
34
34
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export const R50K_TOKEN_SPLIT_REGEX =
|
|
2
|
+
/'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu
|
|
3
|
+
|
|
4
|
+
export const CL_AND_O_TOKEN_SPLIT_PATTERN =
|
|
5
|
+
/(?:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+/giu
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
ImSep,
|
|
12
12
|
ImStart,
|
|
13
13
|
} from '../specialTokens.js'
|
|
14
|
+
import { CL_AND_O_TOKEN_SPLIT_PATTERN } from './constants.js'
|
|
14
15
|
|
|
15
16
|
export function O200KBase(
|
|
16
17
|
bytePairRankDecoder: RawBytePairRanks,
|
|
@@ -27,8 +28,7 @@ export function O200KBase(
|
|
|
27
28
|
])
|
|
28
29
|
|
|
29
30
|
return {
|
|
30
|
-
tokenSplitRegex:
|
|
31
|
-
/(?:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+/giu,
|
|
31
|
+
tokenSplitRegex: CL_AND_O_TOKEN_SPLIT_PATTERN,
|
|
32
32
|
bytePairRankDecoder,
|
|
33
33
|
specialTokensEncoder: specialTokenMapping,
|
|
34
34
|
}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/* eslint-disable no-magic-numbers */
|
|
2
2
|
import type { RawBytePairRanks } from '../BytePairEncodingCore.js'
|
|
3
|
-
import { type EncodingParams
|
|
3
|
+
import { type EncodingParams } from '../modelParams.js'
|
|
4
4
|
import { EndOfText } from '../specialTokens.js'
|
|
5
|
+
import { R50K_TOKEN_SPLIT_REGEX } from './constants.js'
|
|
5
6
|
|
|
6
7
|
export function P50KBase(
|
|
7
8
|
bytePairRankDecoder: RawBytePairRanks,
|
|
8
9
|
): EncodingParams {
|
|
9
10
|
return {
|
|
10
11
|
expectedVocabularySize: 50_281,
|
|
11
|
-
tokenSplitRegex,
|
|
12
|
+
tokenSplitRegex: R50K_TOKEN_SPLIT_REGEX,
|
|
12
13
|
bytePairRankDecoder,
|
|
13
14
|
specialTokensEncoder: new Map<string, number>([[EndOfText, 50_256]]),
|
|
14
15
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/* eslint-disable no-magic-numbers */
|
|
2
2
|
import type { RawBytePairRanks } from '../BytePairEncodingCore.js'
|
|
3
|
-
import { type EncodingParams
|
|
3
|
+
import { type EncodingParams } from '../modelParams.js'
|
|
4
4
|
import { EndOfText, FimMiddle, FimPrefix, FimSuffix } from '../specialTokens.js'
|
|
5
|
+
import { R50K_TOKEN_SPLIT_REGEX } from './constants.js'
|
|
5
6
|
|
|
6
7
|
export function P50KEdit(
|
|
7
8
|
bytePairRankDecoder: RawBytePairRanks,
|
|
@@ -14,7 +15,7 @@ export function P50KEdit(
|
|
|
14
15
|
])
|
|
15
16
|
|
|
16
17
|
return {
|
|
17
|
-
tokenSplitRegex,
|
|
18
|
+
tokenSplitRegex: R50K_TOKEN_SPLIT_REGEX,
|
|
18
19
|
bytePairRankDecoder,
|
|
19
20
|
specialTokensEncoder: specialTokenMapping,
|
|
20
21
|
}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/* eslint-disable no-magic-numbers */
|
|
2
2
|
import type { RawBytePairRanks } from '../BytePairEncodingCore.js'
|
|
3
|
-
import { type EncodingParams
|
|
3
|
+
import { type EncodingParams } from '../modelParams.js'
|
|
4
4
|
import { EndOfText } from '../specialTokens.js'
|
|
5
|
+
import { R50K_TOKEN_SPLIT_REGEX } from './constants.js'
|
|
5
6
|
|
|
6
7
|
export function R50KBase(
|
|
7
8
|
bytePairRankDecoder: RawBytePairRanks,
|
|
8
9
|
): EncodingParams {
|
|
9
10
|
return {
|
|
10
11
|
expectedVocabularySize: 50_257,
|
|
11
|
-
tokenSplitRegex,
|
|
12
|
+
tokenSplitRegex: R50K_TOKEN_SPLIT_REGEX,
|
|
12
13
|
bytePairRankDecoder,
|
|
13
14
|
specialTokensEncoder: new Map<string, number>([[EndOfText, 50_256]]),
|
|
14
15
|
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type ChatMessage, GptEncoding } from './GptEncoding.js'
|
|
2
|
+
import { resolveEncoding } from './resolveEncoding.js'
|
|
3
|
+
|
|
4
|
+
const sampleText = 'This is a test message.'
|
|
5
|
+
const sampleChat: ChatMessage[] = [
|
|
6
|
+
{
|
|
7
|
+
role: 'system',
|
|
8
|
+
content: 'You are a helpful assistant.',
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
role: 'user',
|
|
12
|
+
content: 'Hello, how are you?',
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
role: 'assistant',
|
|
16
|
+
content: 'I am doing well, thank you for asking.',
|
|
17
|
+
},
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
describe('countTokens', () => {
|
|
21
|
+
const encoding = GptEncoding.getEncodingApiForModel(
|
|
22
|
+
'gpt-3.5-turbo',
|
|
23
|
+
resolveEncoding,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
describe('text input', () => {
|
|
27
|
+
it('counts tokens in empty string', () => {
|
|
28
|
+
expect(encoding.countTokens('')).toBe(0)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('counts tokens in simple text', () => {
|
|
32
|
+
expect(encoding.countTokens(sampleText)).toBe(
|
|
33
|
+
encoding.encode(sampleText).length,
|
|
34
|
+
)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('counts tokens in text with special characters', () => {
|
|
38
|
+
const textWithSpecial = 'Hello 👋 world! 🌍'
|
|
39
|
+
expect(encoding.countTokens(textWithSpecial)).toBe(
|
|
40
|
+
encoding.encode(textWithSpecial).length,
|
|
41
|
+
)
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
describe('chat input', () => {
|
|
46
|
+
it('counts tokens in empty chat', () => {
|
|
47
|
+
expect(encoding.countTokens([])).toBe(3) // Due to assistant prompt tokens
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('counts tokens in sample chat', () => {
|
|
51
|
+
expect(encoding.countTokens(sampleChat)).toBe(
|
|
52
|
+
encoding.encodeChat(sampleChat).length,
|
|
53
|
+
)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('matches token counts from encode methods', () => {
|
|
57
|
+
const tokens = encoding.encodeChat(sampleChat)
|
|
58
|
+
const count = encoding.countTokens(sampleChat)
|
|
59
|
+
expect(count).toBe(tokens.length)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('counts tokens in single message chat', () => {
|
|
63
|
+
const singleMessage = [
|
|
64
|
+
{
|
|
65
|
+
role: 'user',
|
|
66
|
+
content: 'Hello world',
|
|
67
|
+
},
|
|
68
|
+
] as const
|
|
69
|
+
expect(encoding.countTokens(singleMessage)).toBe(
|
|
70
|
+
encoding.encodeChat(singleMessage).length,
|
|
71
|
+
)
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
})
|
|
@@ -13,11 +13,14 @@ const {
|
|
|
13
13
|
encode,
|
|
14
14
|
encodeGenerator,
|
|
15
15
|
isWithinTokenLimit,
|
|
16
|
+
countTokens,
|
|
16
17
|
encodeChat,
|
|
17
18
|
encodeChatGenerator,
|
|
18
19
|
vocabularySize,
|
|
20
|
+
setMergeCacheSize,
|
|
19
21
|
} = api
|
|
20
22
|
export {
|
|
23
|
+
countTokens,
|
|
21
24
|
decode,
|
|
22
25
|
decodeAsyncGenerator,
|
|
23
26
|
decodeGenerator,
|
|
@@ -26,6 +29,7 @@ export {
|
|
|
26
29
|
encodeChatGenerator,
|
|
27
30
|
encodeGenerator,
|
|
28
31
|
isWithinTokenLimit,
|
|
32
|
+
setMergeCacheSize,
|
|
29
33
|
vocabularySize,
|
|
30
34
|
}
|
|
31
35
|
// eslint-disable-next-line import/no-default-export
|
|
@@ -13,11 +13,14 @@ const {
|
|
|
13
13
|
encode,
|
|
14
14
|
encodeGenerator,
|
|
15
15
|
isWithinTokenLimit,
|
|
16
|
+
countTokens,
|
|
16
17
|
encodeChat,
|
|
17
18
|
encodeChatGenerator,
|
|
18
19
|
vocabularySize,
|
|
20
|
+
setMergeCacheSize,
|
|
19
21
|
} = api
|
|
20
22
|
export {
|
|
23
|
+
countTokens,
|
|
21
24
|
decode,
|
|
22
25
|
decodeAsyncGenerator,
|
|
23
26
|
decodeGenerator,
|
|
@@ -26,6 +29,7 @@ export {
|
|
|
26
29
|
encodeChatGenerator,
|
|
27
30
|
encodeGenerator,
|
|
28
31
|
isWithinTokenLimit,
|
|
32
|
+
setMergeCacheSize,
|
|
29
33
|
vocabularySize,
|
|
30
34
|
}
|
|
31
35
|
// eslint-disable-next-line import/no-default-export
|
|
@@ -13,11 +13,14 @@ const {
|
|
|
13
13
|
encode,
|
|
14
14
|
encodeGenerator,
|
|
15
15
|
isWithinTokenLimit,
|
|
16
|
+
countTokens,
|
|
16
17
|
encodeChat,
|
|
17
18
|
encodeChatGenerator,
|
|
18
19
|
vocabularySize,
|
|
20
|
+
setMergeCacheSize,
|
|
19
21
|
} = api
|
|
20
22
|
export {
|
|
23
|
+
countTokens,
|
|
21
24
|
decode,
|
|
22
25
|
decodeAsyncGenerator,
|
|
23
26
|
decodeGenerator,
|
|
@@ -26,6 +29,7 @@ export {
|
|
|
26
29
|
encodeChatGenerator,
|
|
27
30
|
encodeGenerator,
|
|
28
31
|
isWithinTokenLimit,
|
|
32
|
+
setMergeCacheSize,
|
|
29
33
|
vocabularySize,
|
|
30
34
|
}
|
|
31
35
|
// eslint-disable-next-line import/no-default-export
|
|
@@ -13,11 +13,14 @@ const {
|
|
|
13
13
|
encode,
|
|
14
14
|
encodeGenerator,
|
|
15
15
|
isWithinTokenLimit,
|
|
16
|
+
countTokens,
|
|
16
17
|
encodeChat,
|
|
17
18
|
encodeChatGenerator,
|
|
18
19
|
vocabularySize,
|
|
20
|
+
setMergeCacheSize,
|
|
19
21
|
} = api
|
|
20
22
|
export {
|
|
23
|
+
countTokens,
|
|
21
24
|
decode,
|
|
22
25
|
decodeAsyncGenerator,
|
|
23
26
|
decodeGenerator,
|
|
@@ -26,6 +29,7 @@ export {
|
|
|
26
29
|
encodeChatGenerator,
|
|
27
30
|
encodeGenerator,
|
|
28
31
|
isWithinTokenLimit,
|
|
32
|
+
setMergeCacheSize,
|
|
29
33
|
vocabularySize,
|
|
30
34
|
}
|
|
31
35
|
// eslint-disable-next-line import/no-default-export
|
|
@@ -13,11 +13,14 @@ const {
|
|
|
13
13
|
encode,
|
|
14
14
|
encodeGenerator,
|
|
15
15
|
isWithinTokenLimit,
|
|
16
|
+
countTokens,
|
|
16
17
|
encodeChat,
|
|
17
18
|
encodeChatGenerator,
|
|
18
19
|
vocabularySize,
|
|
20
|
+
setMergeCacheSize,
|
|
19
21
|
} = api
|
|
20
22
|
export {
|
|
23
|
+
countTokens,
|
|
21
24
|
decode,
|
|
22
25
|
decodeAsyncGenerator,
|
|
23
26
|
decodeGenerator,
|
|
@@ -26,6 +29,7 @@ export {
|
|
|
26
29
|
encodeChatGenerator,
|
|
27
30
|
encodeGenerator,
|
|
28
31
|
isWithinTokenLimit,
|
|
32
|
+
setMergeCacheSize,
|
|
29
33
|
vocabularySize,
|
|
30
34
|
}
|
|
31
35
|
// eslint-disable-next-line import/no-default-export
|