22 * Token estimation and accurate counting functions for different providers
33 */
44
5- import { encoding_for_model , type Tiktoken } from 'tiktoken'
5+ import { encodingForModel , type Tiktoken } from 'js- tiktoken'
66import { createLogger } from '@/lib/logs/console/logger'
77import { MIN_TEXT_LENGTH_FOR_ESTIMATION , TOKENIZATION_CONFIG } from '@/lib/tokenization/constants'
88import type { TokenEstimate } from '@/lib/tokenization/types'
@@ -21,12 +21,12 @@ function getEncoding(modelName: string): Tiktoken {
2121 }
2222
2323 try {
24- const encoding = encoding_for_model ( modelName as Parameters < typeof encoding_for_model > [ 0 ] )
24+ const encoding = encodingForModel ( modelName as Parameters < typeof encodingForModel > [ 0 ] )
2525 encodingCache . set ( modelName , encoding )
2626 return encoding
2727 } catch ( error ) {
2828 logger . warn ( `Failed to get encoding for model ${ modelName } , falling back to cl100k_base` )
29- const encoding = encoding_for_model ( 'gpt-4' )
29+ const encoding = encodingForModel ( 'gpt-4' )
3030 encodingCache . set ( modelName , encoding )
3131 return encoding
3232 }
@@ -79,7 +79,7 @@ export function truncateToTokenLimit(
7979 }
8080
8181 const truncatedTokens = tokens . slice ( 0 , maxTokens )
82- const truncatedText = new TextDecoder ( ) . decode ( encoding . decode ( truncatedTokens ) )
82+ const truncatedText = encoding . decode ( truncatedTokens )
8383
8484 logger . warn (
8585 `Truncated text from ${ tokens . length } to ${ maxTokens } tokens (${ text . length } to ${ truncatedText . length } chars)`
@@ -160,9 +160,6 @@ export function batchByTokenLimit(
160160 * Clean up cached encodings (call when shutting down)
161161 */
162162export function clearEncodingCache ( ) : void {
163- for ( const encoding of encodingCache . values ( ) ) {
164- encoding . free ( )
165- }
166163 encodingCache . clear ( )
167164 logger . info ( 'Cleared tiktoken encoding cache' )
168165}
0 commit comments