mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00

* refactor: move model definitions and database-related methods to packages/data-schemas * ci: update tests due to new DB structure fix: disable mocking `librechat-data-provider` feat: Add schema exports to data-schemas package - Introduced a new schema module that exports various schemas including action, agent, and user schemas. - Updated index.ts to include the new schema exports for better modularity and organization. ci: fix appleStrategy tests fix: Agent.spec.js ci: refactor handleTools tests to use MongoMemoryServer for in-memory database fix: getLogStores imports ci: update banViolation tests to use MongoMemoryServer and improve session mocking test: refactor samlStrategy tests to improve mock configurations and user handling ci: fix crypto mock in handleText tests for improved accuracy ci: refactor spendTokens tests to improve model imports and setup ci: refactor Message model tests to use MongoMemoryServer and improve database interactions * refactor: streamline IMessage interface and move feedback properties to types/message.ts * refactor: use exported initializeRoles from `data-schemas`, remove api workspace version (this serves as an example of future migrations that still need to happen) * refactor: update model imports to use destructuring from `~/db/models` for consistency and clarity * refactor: remove unused mongoose imports from model files for cleaner code * refactor: remove unused mongoose imports from Share, Prompt, and Transaction model files for cleaner code * refactor: remove unused import in Transaction model for cleaner code * ci: update deploy workflow to reference new Docker Dev Branch Images Build and add new workflow for building Docker images on dev branch * chore: cleanup imports
139 lines
5 KiB
JavaScript
139 lines
5 KiB
JavaScript
const { logger } = require('~/config');
|
|
const { createTransaction, createStructuredTransaction } = require('./Transaction');
|
|
/**
|
|
* 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 {EndpointTokenConfig} [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}${
|
|
txData?.context ? ` | Context: ${txData?.context}` : ''
|
|
} | Token usage: `,
|
|
{
|
|
promptTokens,
|
|
completionTokens,
|
|
},
|
|
);
|
|
let prompt, completion;
|
|
try {
|
|
if (promptTokens !== undefined) {
|
|
prompt = await createTransaction({
|
|
...txData,
|
|
tokenType: 'prompt',
|
|
rawAmount: promptTokens === 0 ? 0 : -Math.max(promptTokens, 0),
|
|
});
|
|
}
|
|
|
|
if (completionTokens !== undefined) {
|
|
completion = await createTransaction({
|
|
...txData,
|
|
tokenType: 'completion',
|
|
rawAmount: completionTokens === 0 ? 0 : -Math.max(completionTokens, 0),
|
|
});
|
|
}
|
|
|
|
if (prompt || completion) {
|
|
logger.debug('[spendTokens] Transaction data record against balance:', {
|
|
user: txData.user,
|
|
prompt: prompt?.prompt,
|
|
promptRate: prompt?.rate,
|
|
completion: completion?.completion,
|
|
completionRate: completion?.rate,
|
|
balance: completion?.balance ?? prompt?.balance,
|
|
});
|
|
} else {
|
|
logger.debug('[spendTokens] No transactions incurred against balance');
|
|
}
|
|
} catch (err) {
|
|
logger.error('[spendTokens]', err);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Creates transactions to record the spending of structured 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 {EndpointTokenConfig} [txData.endpointTokenConfig] - The current endpoint token config.
|
|
* @param {String} [txData.valueKey] - The value key (optional).
|
|
* @param {Object} tokenUsage - The number of tokens used.
|
|
* @param {Object} tokenUsage.promptTokens - The number of prompt tokens used.
|
|
* @param {Number} tokenUsage.promptTokens.input - The number of input tokens.
|
|
* @param {Number} tokenUsage.promptTokens.write - The number of write tokens.
|
|
* @param {Number} tokenUsage.promptTokens.read - The number of read tokens.
|
|
* @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 spendStructuredTokens = async (txData, tokenUsage) => {
|
|
const { promptTokens, completionTokens } = tokenUsage;
|
|
logger.debug(
|
|
`[spendStructuredTokens] conversationId: ${txData.conversationId}${
|
|
txData?.context ? ` | Context: ${txData?.context}` : ''
|
|
} | Token usage: `,
|
|
{
|
|
promptTokens,
|
|
completionTokens,
|
|
},
|
|
);
|
|
let prompt, completion;
|
|
try {
|
|
if (promptTokens) {
|
|
const { input = 0, write = 0, read = 0 } = promptTokens;
|
|
prompt = await createStructuredTransaction({
|
|
...txData,
|
|
tokenType: 'prompt',
|
|
inputTokens: -input,
|
|
writeTokens: -write,
|
|
readTokens: -read,
|
|
});
|
|
}
|
|
|
|
if (completionTokens) {
|
|
completion = await createTransaction({
|
|
...txData,
|
|
tokenType: 'completion',
|
|
rawAmount: -completionTokens,
|
|
});
|
|
}
|
|
|
|
if (prompt || completion) {
|
|
logger.debug('[spendStructuredTokens] Transaction data record against balance:', {
|
|
user: txData.user,
|
|
prompt: prompt?.prompt,
|
|
promptRate: prompt?.rate,
|
|
completion: completion?.completion,
|
|
completionRate: completion?.rate,
|
|
balance: completion?.balance ?? prompt?.balance,
|
|
});
|
|
} else {
|
|
logger.debug('[spendStructuredTokens] No transactions incurred against balance');
|
|
}
|
|
} catch (err) {
|
|
logger.error('[spendStructuredTokens]', err);
|
|
}
|
|
|
|
return { prompt, completion };
|
|
};
|
|
|
|
module.exports = { spendTokens, spendStructuredTokens };
|