Auto-calculate summary words

This commit is contained in:
Cohee
2024-04-01 02:11:52 +03:00
parent 159404c3e2
commit a5d00d356b
2 changed files with 44 additions and 3 deletions

View File

@ -122,6 +122,41 @@ function loadSettings() {
switchSourceControls(extension_settings.memory.source);
}
async function onPromptForceWordsAutoClick() {
const context = getContext();
const maxPromptLength = getMaxContextSize(extension_settings.memory.overrideResponseLength);
const chat = context.chat;
const allMessages = chat.filter(m => !m.is_system && m.mes).map(m => m.mes);
const messagesWordCount = allMessages.map(m => extractAllWords(m)).flat().length;
const averageMessageWordCount = messagesWordCount / allMessages.length;
const tokensPerWord = getTokenCount(allMessages.join('\n')) / messagesWordCount;
const wordsPerToken = 1 / tokensPerWord;
const maxPromptLengthWords = Math.round(maxPromptLength * wordsPerToken);
// How many words should pass so that messages will start be dropped out of context;
const wordsPerPrompt = Math.floor(maxPromptLength / tokensPerWord);
// How many words will be needed to fit the allowance buffer
const summaryPromptWords = extractAllWords(extension_settings.memory.prompt).length;
const promptAllowanceWords = maxPromptLengthWords - extension_settings.memory.promptWords - summaryPromptWords;
const maxMessagesPerSummary = extension_settings.memory.maxMessagesPerRequest || 0;
const additionalWords = maxMessagesPerSummary > 0 ? Math.floor(averageMessageWordCount * maxMessagesPerSummary) : Math.max(0, promptAllowanceWords);
const targetSummaryWords = Math.round((wordsPerPrompt / 2) + additionalWords);
console.table({
maxPromptLength,
maxPromptLengthWords,
promptAllowanceWords,
targetSummaryWords,
wordsPerPrompt,
wordsPerToken,
tokensPerWord,
messagesWordCount,
});
const ROUNDING = 100;
extension_settings.memory.promptForceWords = Math.max(1, Math.floor(targetSummaryWords / ROUNDING) * ROUNDING);
$('#memory_prompt_words_force').val(extension_settings.memory.promptForceWords).trigger('input');
}
async function onPromptIntervalAutoClick() {
const context = getContext();
const maxPromptLength = getMaxContextSize(extension_settings.memory.overrideResponseLength);
@ -800,6 +835,7 @@ function setupListeners() {
$('#memory_prompt_builder_raw_non_blocking').off('click').on('input', onMemoryPromptBuilderInput);
$('#memory_prompt_restore').off('click').on('click', onMemoryPromptRestoreClick);
$('#memory_prompt_interval_auto').off('click').on('click', onPromptIntervalAutoClick);
$('#memory_prompt_words_auto').off('click').on('click', onPromptForceWordsAutoClick);
$('#memory_override_response_length').off('click').on('input', onOverrideResponseLengthInput);
$('#memory_max_messages_per_request').off('click').on('input', onMaxMessagesPerRequestInput);
$('#summarySettingsBlockToggle').off('click').on('click', function () {

View File

@ -91,9 +91,14 @@
</div>
</label>
<input id="memory_prompt_interval" type="range" value="{{defaultSettings.promptInterval}}" min="{{defaultSettings.promptMinInterval}}" max="{{defaultSettings.promptMaxInterval}}" step="{{defaultSettings.promptIntervalStep}}" />
<label for="memory_prompt_words_force">
<label for="memory_prompt_words_force" class="title_restorable">
<span>
Update every <span id="memory_prompt_words_force_value"></span> words
<small class="memory_disabled_hint">0 = disable</small>
</span>
<div id="memory_prompt_words_auto" title="Try to automatically adjust the interval based on the chat metrics." class="right_menu_button">
<div class="fa-solid fa-wand-magic-sparkles"></div>
</div>
</label>
<input id="memory_prompt_words_force" type="range" value="{{defaultSettings.promptForceWords}}" min="{{defaultSettings.promptMinForceWords}}" max="{{defaultSettings.promptMaxForceWords}}" step="{{defaultSettings.promptForceWordsStep}}" />
<small>If both sliders are non-zero, then both will trigger summary updates at their respective intervals.</small>