From ba545e44e3f6b37bdc82a498ec7b98b175797234 Mon Sep 17 00:00:00 2001 From: hh_aa <1@2.3> Date: Sat, 17 Jun 2023 23:30:48 -0400 Subject: [PATCH 1/3] Use filename instead of id to delete chat logs. --- public/script.js | 2 +- server.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/public/script.js b/public/script.js index a94b00a43..d6797c767 100644 --- a/public/script.js +++ b/public/script.js @@ -938,7 +938,7 @@ async function delChat(chatfile) { headers: getRequestHeaders(), body: JSON.stringify({ chatfile: chatfile, - id: characters[this_chid].name + avatar_url: characters[this_chid].avatar, }), }); if (response.ok === true) { diff --git a/server.js b/server.js index 6d14375fe..d458cbfed 100644 --- a/server.js +++ b/server.js @@ -1198,9 +1198,10 @@ app.post("/delchat", jsonParser, function (request, response) { return response.sendStatus(403); } - const fileName = path.join(directories.chats, '/', sanitize(request.body.id), '/', sanitize(request.body.chatfile)); + var dirName = String(request.body.avatar_url).replace('.png', ''); + const fileName = path.join(directories.chats, '/', sanitize(dirName), '/', sanitize(request.body.chatfile)); if (!fs.existsSync(fileName)) { - console.log('Chat file not found'); + console.log(`Chat file not found '${fileName}'`); return response.sendStatus(400); } else { console.log('found the chat file: ' + fileName); From 6d649c716d2a8ab0b284d582d6cf49091d6e9a6b Mon Sep 17 00:00:00 2001 From: hh_aa <1@2.3> Date: Sat, 17 Jun 2023 23:30:48 -0400 Subject: [PATCH 2/3] Use filename instead of id to delete chat logs. --- public/script.js | 2 +- server.js | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/public/script.js b/public/script.js index a94b00a43..d6797c767 100644 --- a/public/script.js +++ b/public/script.js @@ -938,7 +938,7 @@ async function delChat(chatfile) { headers: getRequestHeaders(), body: JSON.stringify({ chatfile: chatfile, - id: characters[this_chid].name + avatar_url: characters[this_chid].avatar, }), }); if (response.ok === true) { diff --git a/server.js b/server.js index 6d14375fe..237de68c2 100644 --- a/server.js +++ b/server.js @@ -578,7 +578,7 @@ app.post("/savechat", jsonParser, function (request, response) { var dir_name = String(request.body.avatar_url).replace('.png', ''); let chat_data = request.body.chat; let jsonlData = chat_data.map(JSON.stringify).join('\n'); - fs.writeFileSync(`${chatsPath + dir_name}/${sanitize(String(request.body.file_name))}.jsonl`, jsonlData, 'utf8'); + fs.writeFileSync(`${chatsPath + sanitize(dir_name)}/${sanitize(String(request.body.file_name))}.jsonl`, jsonlData, 'utf8'); return response.send({ result: "ok" }); } catch (error) { response.send(error); @@ -1198,9 +1198,10 @@ app.post("/delchat", jsonParser, function (request, response) { return response.sendStatus(403); } - const fileName = path.join(directories.chats, '/', sanitize(request.body.id), '/', sanitize(request.body.chatfile)); + var dirName = String(request.body.avatar_url).replace('.png', ''); + const fileName = path.join(directories.chats, '/', sanitize(dirName), '/', sanitize(request.body.chatfile)); if (!fs.existsSync(fileName)) { - console.log('Chat file not found'); + console.log(`Chat file not found '${fileName}'`); return response.sendStatus(400); } else { console.log('found the chat file: ' + fileName); From 22a5def61839da27078649424fa7c3f408ccc4bc Mon Sep 17 00:00:00 2001 From: Cohee Date: Sun, 18 Jun 2023 16:29:23 +0300 Subject: [PATCH 3/3] Add option to auto-adjust number of chroma messages to keep / query based on context size. --- public/script.js | 10 +-- public/scripts/extensions.js | 4 +- .../extensions/infinity-context/index.js | 77 ++++++++++++++++++- 3 files changed, 82 insertions(+), 9 deletions(-) diff --git a/public/script.js b/public/script.js index f0a34925f..870fd7e3b 100644 --- a/public/script.js +++ b/public/script.js @@ -558,7 +558,7 @@ function getCurrentChatId() { } } -const CHARACTERS_PER_TOKEN_RATIO = 3.35; +export const CHARACTERS_PER_TOKEN_RATIO = 3.35; const talkativeness_default = 0.5; var is_advanced_char_open = false; @@ -1945,8 +1945,11 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, coreChat.pop(); } + // Determine token limit + let this_max_context = getMaxContextSize(); + if (extension_settings.chromadb.n_results !== 0) { - await runGenerationInterceptors(coreChat); + await runGenerationInterceptors(coreChat, this_max_context); console.log(`Core/all messages: ${coreChat.length}/${chat.length}`); } @@ -1993,9 +1996,6 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, chat2[i] = formatMessageHistoryItem(coreChat[j], isInstruct); } - // Determine token limit - let this_max_context = getMaxContextSize(); - // Adjust token limit for Horde let adjustedParams; if (main_api == 'koboldhorde' && (horde_settings.auto_adjust_context_length || horde_settings.auto_adjust_response_length)) { diff --git a/public/scripts/extensions.js b/public/scripts/extensions.js index 48b3be928..d4c1ea947 100644 --- a/public/scripts/extensions.js +++ b/public/scripts/extensions.js @@ -422,12 +422,12 @@ async function loadExtensionSettings(settings) { } } -async function runGenerationInterceptors(chat) { +async function runGenerationInterceptors(chat, contextSize) { for (const manifest of Object.values(manifests)) { const interceptorKey = manifest.generate_interceptor; if (typeof window[interceptorKey] === 'function') { try { - await window[interceptorKey](chat); + await window[interceptorKey](chat, contextSize); } catch (e) { console.error(`Failed running interceptor for ${manifest.display_name}`, e); } diff --git a/public/scripts/extensions/infinity-context/index.js b/public/scripts/extensions/infinity-context/index.js index 8c34d56ca..345855eec 100644 --- a/public/scripts/extensions/infinity-context/index.js +++ b/public/scripts/extensions/infinity-context/index.js @@ -1,4 +1,4 @@ -import { saveSettingsDebounced, getCurrentChatId, system_message_types, eventSource, event_types } from "../../../script.js"; +import { saveSettingsDebounced, getCurrentChatId, system_message_types, eventSource, event_types, CHARACTERS_PER_TOKEN_RATIO } from "../../../script.js"; import { humanizedDateTime } from "../../RossAscends-mods.js"; import { getApiUrl, extension_settings, getContext, doExtrasFetch } from "../../extensions.js"; import { getFileText, onlyUnique, splitRecursive, IndexedDBStore } from "../../utils.js"; @@ -29,6 +29,14 @@ const defaultSettings = { file_split_length_min: 512, file_split_length_max: 4096, file_split_length_step: 128, + + keep_context_proportion: 0.5, + keep_context_proportion_min: 0.0, + keep_context_proportion_max: 1.0, + keep_context_proportion_step: 0.05, + + auto_adjust: true, + freeze: false, }; const postHeaders = { @@ -92,6 +100,8 @@ async function loadSettings() { $('#chromadb_n_results').val(extension_settings.chromadb.n_results).trigger('input'); $('#chromadb_split_length').val(extension_settings.chromadb.split_length).trigger('input'); $('#chromadb_file_split_length').val(extension_settings.chromadb.file_split_length).trigger('input'); + $('#chromadb_keep_context_proportion').val(extension_settings.chromadb.keep_context_proportion).trigger('input'); + $('#chromadb_auto_adjust').prop('checked', extension_settings.chromadb.auto_adjust); $('#chromadb_freeze').prop('checked', extension_settings.chromadb.freeze); } @@ -390,7 +400,51 @@ async function onSelectInjectFile(e) { } } -window.chromadb_interceptGeneration = async (chat) => { +/* +* Automatically adjusts the extension settings for the optimal number of messages to keep and query based +* on the chat history and a specified maximum context length. +*/ +function doAutoAdjust(chat, maxContext) { + console.debug('CHROMADB: Auto-adjusting sliders (messages: %o, maxContext: %o)', chat.length, maxContext); + // Get mean message length + const meanMessageLength = chat.reduce((acc, cur) => acc + cur.mes.length, 0) / chat.length; + + if (Number.isNaN(meanMessageLength)) { + console.debug('CHROMADB: Mean message length is NaN, aborting auto-adjust'); + return; + } + + console.debug('CHROMADB: Mean message length (characters): %o', meanMessageLength); + // Convert to number of "tokens" + const meanMessageLengthTokens = Math.ceil(meanMessageLength / CHARACTERS_PER_TOKEN_RATIO); + console.debug('CHROMADB: Mean message length (tokens): %o', meanMessageLengthTokens); + // Get number of messages in context + const contextMessages = Math.max(1, Math.ceil(maxContext / meanMessageLengthTokens)); + // Round up to nearest 10 + const contextMessagesRounded = Math.ceil(contextMessages / 10) * 10; + console.debug('CHROMADB: Estimated context messages (rounded): %o', contextMessagesRounded); + // Messages to keep (proportional, rounded to nearest 5, minimum 10) + const messagesToKeep = Math.max(10, Math.ceil(contextMessagesRounded * extension_settings.chromadb.keep_context_proportion / 5) * 5); + console.debug('CHROMADB: Estimated messages to keep: %o', messagesToKeep); + // Messages to query (rounded) + const messagesToQuery = contextMessagesRounded - messagesToKeep; + console.debug('CHROMADB: Estimated messages to query: %o', messagesToQuery); + // Set extension settings + extension_settings.chromadb.keep_context = messagesToKeep; + extension_settings.chromadb.n_results = messagesToQuery; + // Update sliders + $('#chromadb_keep_context').val(messagesToKeep); + $('#chromadb_n_results').val(messagesToQuery); + // Update labels + $('#chromadb_keep_context_value').text(extension_settings.chromadb.keep_context); + $('#chromadb_n_results_value').text(extension_settings.chromadb.n_results); +} + +window.chromadb_interceptGeneration = async (chat, maxContext) => { + if (extension_settings.chromadb.auto_adjust) { + doAutoAdjust(chat, maxContext); + } + const currentChatId = getCurrentChatId(); const selectedStrategy = extension_settings.chromadb.strategy; if (currentChatId) { @@ -452,6 +506,17 @@ function onFreezeInput() { saveSettingsDebounced(); } +function onAutoAdjustInput() { + extension_settings.chromadb.auto_adjust = $('#chromadb_auto_adjust').is(':checked'); + saveSettingsDebounced(); +} + +function onKeepContextProportionInput() { + extension_settings.chromadb.keep_context_proportion = $('#chromadb_keep_context_proportion').val(); + $('#chromadb_keep_context_proportion_value').text(Math.round(extension_settings.chromadb.keep_context_proportion * 100)); + saveSettingsDebounced(); +} + jQuery(async () => { const settingsHtml = `
@@ -472,6 +537,8 @@ jQuery(async () => { + + @@ -480,6 +547,10 @@ jQuery(async () => { Freeze ChromaDB state +