This commit is contained in:
duanfuxiang
2025-01-05 11:51:39 +08:00
commit 0c7ee142cb
215 changed files with 20611 additions and 0 deletions

11
src/utils/token.ts Normal file
View File

@@ -0,0 +1,11 @@
import { getEncoding } from 'js-tiktoken'
// TODO: Replace js-tiktoken with tiktoken library for better performance
// Note: tiktoken uses WebAssembly, requiring esbuild configuration
// Caution: tokenCount is computationally expensive for large inputs.
// Frequent use, especially on large files, may significantly impact performance.
export async function tokenCount(text: string): Promise<number> {
const encoder = getEncoding('cl100k_base')
return encoder.encode(text).length
}