MCPcopy Create free account
hub / github.com/Rfym21/Qwen2API / countTokens

Function countTokens

src/utils/precise-tokenizer.js:14–21  ·  view source on GitHub ↗

* 使用tiktoken进行精准token计数 * @param {string} text - 要计数的文本 * @param {string} model - 模型名称,默认为gpt-3.5-turbo * @returns {number} 精确的token数量

(text, model = 'gpt-3.5-turbo')

Source from the content-addressed store, hash-verified

12 * @returns {number} 精确的token数量
13 */
14function countTokens(text, model = 'gpt-3.5-turbo') {
15 if (!text || typeof text !== 'string') return 0
16
17 const encoding = tiktoken.encoding_for_model(model)
18 const tokens = encoding.encode(text)
19 encoding.free() // 释放内存
20 return tokens.length
21}
22
23
24

Callers 2

countMessagesTokensFunction · 0.85
createUsageObjectFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected