mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-21 21:50:49 +02:00

* 🚀 feat: Add automatic refill settings to balance schema * 🚀 feat: Refactor balance feature to use global interface configuration * 🚀 feat: Implement auto-refill functionality for balance management * 🚀 feat: Enhance auto-refill logic and configuration for balance management * 🚀 chore: Bump version to 0.7.74 in package.json and package-lock.json * 🚀 chore: Bump version to 0.0.5 in package.json and package-lock.json * 🚀 docs: Update comment for balance settings in librechat.example.yaml * chore: space in `.env.example` * 🚀 feat: Implement balance configuration loading and refactor related components * 🚀 test: Refactor tests to use custom config for balance feature * 🚀 fix: Update balance response handling in Transaction.js to use Balance model * 🚀 test: Update AppService tests to include balance configuration in mock setup * 🚀 test: Enhance AppService tests with complete balance configuration scenarios * 🚀 refactor: Rename balanceConfig to balance and update related tests for clarity * 🚀 refactor: Remove loadDefaultBalance and update balance handling in AppService * 🚀 test: Update AppService tests to reflect new balance structure and defaults * 🚀 test: Mock getCustomConfig in BaseClient tests to control balance configuration * 🚀 test: Add get method to mockCache in OpenAIClient tests for improved cache handling * 🚀 test: Mock getCustomConfig in OpenAIClient tests to control balance configuration * 🚀 test: Remove mock for getCustomConfig in OpenAIClient tests to streamline configuration handling * 🚀 fix: Update balance configuration reference in config.js for consistency * refactor: Add getBalanceConfig function to retrieve balance configuration * chore: Comment out example balance settings in librechat.example.yaml * refactor: Replace getCustomConfig with getBalanceConfig for balance handling * fix: tests * refactor: Replace getBalanceConfig call with balance from request locals * refactor: Update balance handling to use environment variables for configuration * refactor: Replace getBalanceConfig calls with balance from request locals * refactor: Simplify balance configuration logic in getBalanceConfig --------- Co-authored-by: Danny Avila <danny@librechat.ai>
95 lines
2.6 KiB
JavaScript
95 lines
2.6 KiB
JavaScript
const { promptTokensEstimate } = require('openai-chat-tokens');
|
|
const { EModelEndpoint, supportsBalanceCheck } = require('librechat-data-provider');
|
|
const { formatFromLangChain } = require('~/app/clients/prompts');
|
|
const { getBalanceConfig } = require('~/server/services/Config');
|
|
const checkBalance = require('~/models/checkBalance');
|
|
const { logger } = require('~/config');
|
|
|
|
const createStartHandler = ({
|
|
context,
|
|
conversationId,
|
|
tokenBuffer = 0,
|
|
initialMessageCount,
|
|
manager,
|
|
}) => {
|
|
return async (_llm, _messages, runId, parentRunId, extraParams) => {
|
|
const { invocation_params } = extraParams;
|
|
const { model, functions, function_call } = invocation_params;
|
|
const messages = _messages[0].map(formatFromLangChain);
|
|
|
|
logger.debug(`[createStartHandler] handleChatModelStart: ${context}`, {
|
|
model,
|
|
function_call,
|
|
});
|
|
|
|
if (context !== 'title') {
|
|
logger.debug(`[createStartHandler] handleChatModelStart: ${context}`, {
|
|
functions,
|
|
});
|
|
}
|
|
|
|
const payload = { messages };
|
|
let prelimPromptTokens = 1;
|
|
|
|
if (functions) {
|
|
payload.functions = functions;
|
|
prelimPromptTokens += 2;
|
|
}
|
|
|
|
if (function_call) {
|
|
payload.function_call = function_call;
|
|
prelimPromptTokens -= 5;
|
|
}
|
|
|
|
prelimPromptTokens += promptTokensEstimate(payload);
|
|
logger.debug('[createStartHandler]', {
|
|
prelimPromptTokens,
|
|
tokenBuffer,
|
|
});
|
|
prelimPromptTokens += tokenBuffer;
|
|
|
|
try {
|
|
const balance = await getBalanceConfig();
|
|
if (balance?.enabled && supportsBalanceCheck[EModelEndpoint.openAI]) {
|
|
const generations =
|
|
initialMessageCount && messages.length > initialMessageCount
|
|
? messages.slice(initialMessageCount)
|
|
: null;
|
|
await checkBalance({
|
|
req: manager.req,
|
|
res: manager.res,
|
|
txData: {
|
|
user: manager.user,
|
|
tokenType: 'prompt',
|
|
amount: prelimPromptTokens,
|
|
debug: manager.debug,
|
|
generations,
|
|
model,
|
|
endpoint: EModelEndpoint.openAI,
|
|
},
|
|
});
|
|
}
|
|
} catch (err) {
|
|
logger.error(`[createStartHandler][${context}] checkBalance error`, err);
|
|
manager.abortController.abort();
|
|
if (context === 'summary' || context === 'plugins') {
|
|
manager.addRun(runId, { conversationId, error: err.message });
|
|
throw new Error(err);
|
|
}
|
|
return;
|
|
}
|
|
|
|
manager.addRun(runId, {
|
|
model,
|
|
messages,
|
|
functions,
|
|
function_call,
|
|
runId,
|
|
parentRunId,
|
|
conversationId,
|
|
prelimPromptTokens,
|
|
});
|
|
};
|
|
};
|
|
|
|
module.exports = createStartHandler;
|