( content: MessageContent, // defaults to llama2 because the tokenizer tends to produce more tokens modelName = "llama2", )
| 92 | } |
| 93 | |
| 94 | async function countTokensAsync( |
| 95 | content: MessageContent, |
| 96 | // defaults to llama2 because the tokenizer tends to produce more tokens |
| 97 | modelName = "llama2", |
| 98 | ): Promise<number> { |
| 99 | const encoding = asyncEncoderForModel(modelName); |
| 100 | if (Array.isArray(content)) { |
| 101 | const promises = content.map(async (part) => { |
| 102 | if (part.type === "imageUrl") { |
| 103 | return countImageTokens(part); |
| 104 | } |
| 105 | return (await encoding.encode(part.text ?? "")).length; |
| 106 | }); |
| 107 | return (await Promise.all(promises)).reduce((sum, val) => sum + val, 0); |
| 108 | } |
| 109 | return (await encoding.encode(content ?? "")).length; |
| 110 | } |
| 111 | |
| 112 | function countTokens( |
| 113 | content: MessageContent, |
no test coverage detected