mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
* chore: bump anthropic SDK * chore: update anthropic config settings (fileSupport, default models) * feat: anthropic multi modal formatting * refactor: update vision models and use endpoint specific max long side resizing * feat(anthropic): multimodal messages, retry logic, and messages payload * chore: add more safety to trimming content due to whitespace error for assistant messages * feat(anthropic): token accounting and resending multiple images in progress * chore: bump data-provider * feat(anthropic): resendImages feature * chore: optimize Edit/Ask controllers, switch model back to req model * fix: false positive of invalid model * refactor(validateVisionModel): use object as arg, pass in additional/available models * refactor(validateModel): use helper function, `getModelsConfig` * feat: add modelsConfig to endpointOption so it gets passed to all clients, use for properly validating vision models * refactor: initialize default vision model and make sure it's available before assigning it * refactor(useSSE): avoid resetting model if user selected a new model between request and response * feat: show rate in transaction logging * fix: return tokenCountMap regardless of payload shape
64 lines
2.1 KiB
JavaScript
64 lines
2.1 KiB
JavaScript
const Transaction = require('./Transaction');
|
|
const { logger } = require('~/config');
|
|
|
|
/**
|
|
* Creates up to two transactions to record the spending of tokens.
|
|
*
|
|
* @function
|
|
* @async
|
|
* @param {Object} txData - Transaction data.
|
|
* @param {mongoose.Schema.Types.ObjectId} txData.user - The user ID.
|
|
* @param {String} txData.conversationId - The ID of the conversation.
|
|
* @param {String} txData.model - The model name.
|
|
* @param {String} txData.context - The context in which the transaction is made.
|
|
* @param {String} [txData.endpointTokenConfig] - The current endpoint token config.
|
|
* @param {String} [txData.valueKey] - The value key (optional).
|
|
* @param {Object} tokenUsage - The number of tokens used.
|
|
* @param {Number} tokenUsage.promptTokens - The number of prompt tokens used.
|
|
* @param {Number} tokenUsage.completionTokens - The number of completion tokens used.
|
|
* @returns {Promise<void>} - Returns nothing.
|
|
* @throws {Error} - Throws an error if there's an issue creating the transactions.
|
|
*/
|
|
const spendTokens = async (txData, tokenUsage) => {
|
|
const { promptTokens, completionTokens } = tokenUsage;
|
|
logger.debug(`[spendTokens] conversationId: ${txData.conversationId} | Token usage: `, {
|
|
promptTokens,
|
|
completionTokens,
|
|
});
|
|
let prompt, completion;
|
|
try {
|
|
if (promptTokens >= 0) {
|
|
prompt = await Transaction.create({
|
|
...txData,
|
|
tokenType: 'prompt',
|
|
rawAmount: -promptTokens,
|
|
});
|
|
}
|
|
|
|
if (!completionTokens) {
|
|
logger.debug('[spendTokens] !completionTokens', { prompt, completion });
|
|
return;
|
|
}
|
|
|
|
completion = await Transaction.create({
|
|
...txData,
|
|
tokenType: 'completion',
|
|
rawAmount: -completionTokens,
|
|
});
|
|
|
|
prompt &&
|
|
completion &&
|
|
logger.debug('[spendTokens] Transaction data record against balance:', {
|
|
user: prompt.user,
|
|
prompt: prompt.prompt,
|
|
promptRate: prompt.rate,
|
|
completion: completion.completion,
|
|
completionRate: completion.rate,
|
|
balance: completion.balance,
|
|
});
|
|
} catch (err) {
|
|
logger.error('[spendTokens]', err);
|
|
}
|
|
};
|
|
|
|
module.exports = spendTokens;
|