fix: update token counting to use content length estimation

Co-Authored-By: Han Xiao <han.xiao@jina.ai>
This commit is contained in:
Devin AI 2025-02-11 09:32:41 +00:00
parent fbb60198cb
commit fd7dd8dbe8
3 changed files with 12 additions and 4 deletions

8
package-lock.json generated
View File

@ -26,6 +26,7 @@
"devDependencies": {
"@types/commander": "^2.12.0",
"@types/cors": "^2.8.17",
"@types/events": "^3.0.3",
"@types/express": "^5.0.0",
"@types/jest": "^29.5.14",
"@types/node": "^22.10.10",
@ -1578,6 +1579,13 @@
"integrity": "sha512-xFdR6tkm0MWvBfO8xXCSsinYxHcqkQUlcHeSpMC2ukzOb6lwQAfDmW+Qt0AvlGd8HpsS28qKsB+oPeJn9I39jg==",
"license": "MIT"
},
"node_modules/@types/events": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/@types/events/-/events-3.0.3.tgz",
"integrity": "sha512-trOc4AAUThEz9hapPtSd7wf5tiQKvTtu5b371UxXdTuqzIh0ArcRspRP0i0Viu+LXstIQ1z96t1nsPxT9ol01g==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/express": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/@types/express/-/express-5.0.0.tgz",

View File

@ -43,6 +43,7 @@
"devDependencies": {
"@types/commander": "^2.12.0",
"@types/cors": "^2.8.17",
"@types/events": "^3.0.3",
"@types/express": "^5.0.0",
"@types/jest": "^29.5.14",
"@types/node": "^22.10.10",

View File

@ -404,11 +404,10 @@ app.post('/v1/chat/completions', (async (req: Request, res: Response) => {
requestId
});
// Track error as rejected tokens with Vercel token counting
// Track error tokens using content length estimation
const errorMessage = error?.message || 'An error occurred';
// Default to 1 token for errors as per Vercel AI SDK convention
const errorTokens = 1;
context.tokenTracker.trackUsage('evaluator', errorTokens, TOKEN_CATEGORIES.REJECTED);
const estimatedTokens = Math.ceil(Buffer.byteLength(errorMessage, 'utf-8') / 4);
context.tokenTracker.trackUsage('evaluator', estimatedTokens, TOKEN_CATEGORIES.REJECTED);
// Clean up event listeners
context.actionTracker.removeAllListeners('action');