(text: string, modelName = 'text-embedding-3-small')
| 43 | * This is the exact count OpenAI's API will use |
| 44 | */ |
| 45 | export function getAccurateTokenCount(text: string, modelName = 'text-embedding-3-small'): number { |
| 46 | if (!text || text.length === 0) { |
| 47 | return 0 |
| 48 | } |
| 49 | |
| 50 | try { |
| 51 | const encoding = getEncoding(modelName) |
| 52 | const tokens = encoding.encode(text) |
| 53 | return tokens.length |
| 54 | } catch (error) { |
| 55 | logger.error('Error counting tokens with tiktoken:', error) |
| 56 | return Math.ceil(text.length / 4) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get individual tokens as strings for visualization |
no test coverage detected