mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-10-10 08:04:10 +00:00
refactor(agents): token counting for media content in messages
Introduced a new method to estimate token costs for image and document blocks in messages, improving the accuracy of token counting. This enhancement ensures that media content is properly accounted for, particularly for the Claude model, by integrating additional token estimation logic for various content types. Updated the token counting function to utilize this new method, enhancing overall reliability and functionality.
This commit is contained in:
parent
d2a4d9fbee
commit
9ab29a9b29
2 changed files with 169 additions and 4 deletions
|
|
@ -25,6 +25,7 @@ const {
|
|||
loadAgent: loadAgentFn,
|
||||
createMultiAgentMapper,
|
||||
filterMalformedContentParts,
|
||||
estimateMediaTokensForMessage,
|
||||
hydrateMissingIndexTokenCounts,
|
||||
} = require('@librechat/api');
|
||||
const {
|
||||
|
|
@ -1237,8 +1238,12 @@ class AgentClient extends BaseClient {
|
|||
* @returns {number}
|
||||
*/
|
||||
getTokenCountForMessage(message) {
|
||||
const count = super.getTokenCountForMessage(message);
|
||||
if (this.getEncoding() === 'claude') {
|
||||
const isClaude = this.getEncoding() === 'claude';
|
||||
let count = super.getTokenCountForMessage(message);
|
||||
count += estimateMediaTokensForMessage(message.content ?? message.text, isClaude, (text) =>
|
||||
this.getTokenCount(text),
|
||||
);
|
||||
if (isClaude) {
|
||||
return Math.ceil(count * AgentClient.CLAUDE_TOKEN_CORRECTION);
|
||||
}
|
||||
return count;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue