From 3ce14883b9583937f1a1c26c5876d47c018b18bb Mon Sep 17 00:00:00 2001 From: BlipRanger <1860540+BlipRanger@users.noreply.github.com> Date: Sat, 24 Jun 2023 15:36:26 -0400 Subject: [PATCH 1/5] First try at chromadb multichat --- .../extensions/infinity-context/index.js | 61 ++++++++++++++++++- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/public/scripts/extensions/infinity-context/index.js b/public/scripts/extensions/infinity-context/index.js index 11282d5f8..30f8f10f4 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, CHARACTERS_PER_TOKEN_RATIO } from "../../../script.js"; +import { saveSettingsDebounced, getCurrentChatId, system_message_types, eventSource, event_types, getRequestHeaders, 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"; @@ -114,6 +114,13 @@ function onStrategyChange() { saveSettingsDebounced(); } +function onRecallStrategyChange() { + console.log('changing chromadb recall strat'); + extension_settings.chromadb.recall_strategy = $('#chromadb_recall_strategy').val(); + + saveSettingsDebounced(); +} + function onKeepContextInput() { extension_settings.chromadb.keep_context = Number($('#chromadb_keep_context').val()); $('#chromadb_keep_context_value').text(extension_settings.chromadb.keep_context); @@ -339,6 +346,40 @@ async function queryMessages(chat_id, query) { return []; } +async function queryMultiMessages(chat_id, query) { + const context = getContext(); + const response = await fetch("/getallchatsofcharacter", { + method: 'POST', + body: JSON.stringify({ avatar_url: context.characters[context.characterId].avatar}), + headers: getRequestHeaders(), + }); + if (!response.ok) { + return; + } + let data = await response.json(); + data = Object.values(data); + let chat_list = data.sort((a, b) => a["file_name"].localeCompare(b["file_name"])).reverse(); + + // Extracting chat_ids from the chat_list + chat_list = chat_list.map(chat => chat.file_name.replace(/\.[^/.]+$/, "")); + const url = new URL(getApiUrl()); + url.pathname = '/api/chromadb/multiquery'; + + const queryMessagesResult = await fetch(url, { + method: 'POST', + body: JSON.stringify({ chat_list, query, n_results: extension_settings.chromadb.n_results }), + headers: postHeaders, + }); + + if (queryMessagesResult.ok) { + const queryMessagesData = await queryMessagesResult.json(); + + return queryMessagesData; + } + + return []; +} + async function onSelectInjectFile(e) { const file = e.target.files[0]; const currentChatId = getCurrentChatId(); @@ -448,6 +489,7 @@ window.chromadb_interceptGeneration = async (chat, maxContext) => { const currentChatId = getCurrentChatId(); const selectedStrategy = extension_settings.chromadb.strategy; + const recallStrategy = extension_settings.chromadb.recall_strategy; if (currentChatId) { const messagesToStore = chat.slice(0, -extension_settings.chromadb.keep_context); @@ -456,8 +498,16 @@ window.chromadb_interceptGeneration = async (chat, maxContext) => { const lastMessage = chat[chat.length - 1]; + let queriedMessages; + console.debug(recallStrategy) if (lastMessage) { - const queriedMessages = await queryMessages(currentChatId, lastMessage.mes); + if (recallStrategy === 'multichat'){ + console.log("Utilizing multichat") + queriedMessages = await queryMultiMessages(currentChatId, lastMessage.mes); + } + else{ + queriedMessages = await queryMessages(currentChatId, lastMessage.mes); + } queriedMessages.sort((a, b) => a.date - b.date); @@ -548,6 +598,12 @@ jQuery(async () => { + Memory Recall Strategy + + @@ -592,6 +648,7 @@ jQuery(async () => { $('#extensions_settings2').append(settingsHtml); $('#chromadb_strategy').on('change', onStrategyChange); + $('#chromadb_recall_strategy').on('change', onRecallStrategyChange); $('#chromadb_keep_context').on('input', onKeepContextInput); $('#chromadb_n_results').on('input', onNResultsInput); $('#chromadb_split_length').on('input', onSplitLengthInput); From 9de7db8f2d26464eaf9130118509f729700ecba8 Mon Sep 17 00:00:00 2001 From: BlipRanger <1860540+BlipRanger@users.noreply.github.com> Date: Sat, 24 Jun 2023 20:41:54 -0400 Subject: [PATCH 2/5] Add custom chroma strat --- .../extensions/infinity-context/index.js | 74 ++++++++++++++++++- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/public/scripts/extensions/infinity-context/index.js b/public/scripts/extensions/infinity-context/index.js index 30f8f10f4..6d52d70f4 100644 --- a/public/scripts/extensions/infinity-context/index.js +++ b/public/scripts/extensions/infinity-context/index.js @@ -20,6 +20,11 @@ const defaultSettings = { n_results_max: 500, n_results_step: 1, + chroma_depth: 20, + chroma_depth_min: -1, + chroma_depth_max: 500, + chroma_depth_step: 1, + split_length: 384, split_length_min: 64, split_length_max: 4096, @@ -100,17 +105,29 @@ 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_keep_context_proportion').val(extension_settings.chromadb.keep_context_proportion).trigger('input'); + $('#chromadb_custom_depth').val(extension_settings.chromadb.chroma_depth).trigger('input'); + $('#chromadb_custom_msg').val(extension_settings.chromadb.recall_msg).trigger('input'); $('#chromadb_auto_adjust').prop('checked', extension_settings.chromadb.auto_adjust); $('#chromadb_freeze').prop('checked', extension_settings.chromadb.freeze); enableDisableSliders(); + onStrategyChange(); } function onStrategyChange() { console.debug('changing chromadb strat'); extension_settings.chromadb.strategy = $('#chromadb_strategy').val(); - - //$('#chromadb_strategy').select(extension_settings.chromadb.strategy); + if(extension_settings.chromadb.strategy === "custom"){ + $('#chromadb_custom_depth').show(); + $('label[for="chromadb_custom_depth"]').show(); + $('#chromadb_custom_msg').show(); + $('label[for="chromadb_custom_msg"]').show(); + } else { + $('#chromadb_custom_depth').hide(); + $('label[for="chromadb_custom_depth"]').hide(); + $('#chromadb_custom_msg').hide(); + $('label[for="chromadb_custom_msg"]').hide(); + } saveSettingsDebounced(); } @@ -133,6 +150,17 @@ function onNResultsInput() { saveSettingsDebounced(); } +function onChromaDepthInput() { + extension_settings.chromadb.chroma_depth = Number($('#chromadb_custom_depth').val()); + $('#chromadb_custom_depth_value').text(extension_settings.chromadb.chroma_depth); + saveSettingsDebounced(); +} + +function onChromaMsgInput() { + extension_settings.chromadb.recall_msg = $('#chromadb_custom_msg').val(); + saveSettingsDebounced(); +} + function onSplitLengthInput() { extension_settings.chromadb.split_length = Number($('#chromadb_split_length').val()); $('#chromadb_split_length_value').text(extension_settings.chromadb.split_length); @@ -490,6 +518,8 @@ window.chromadb_interceptGeneration = async (chat, maxContext) => { const currentChatId = getCurrentChatId(); const selectedStrategy = extension_settings.chromadb.strategy; const recallStrategy = extension_settings.chromadb.recall_strategy; + const recallMsg = extension_settings.chromadb.recall_msg; + const chromaDepth = extension_settings.chromadb.chroma_depth; if (currentChatId) { const messagesToStore = chat.slice(0, -extension_settings.chromadb.keep_context); @@ -538,7 +568,35 @@ window.chromadb_interceptGeneration = async (chat, maxContext) => { ); chat.splice(chat.length, 0, ...newChat); } - + if (selectedStrategy === 'custom') { + const context = getContext(); + const charname = context.name2; + newChat.push( + { + is_name: false, + is_user: false, + mes: recallMsg + " [", + name: "system", + send_date: 0, + } + ); + newChat.push(...queriedMessages.map(m => m.meta).filter(onlyUnique).map(JSON.parse)); + newChat.push( + { + is_name: false, + is_user: false, + mes: `]\n`, + name: "system", + send_date: 0, + } + ); + if(chromaDepth === -1){ + chat.splice(messagesToStore.length, 0, ...newChat); + } + else{ + chat.splice(chromaDepth, 0, ...newChat); + } + } if (selectedStrategy === 'original') { //removes .length # messages from the start of 'kept messages' //replaces them with chromaDB results (with no separator) @@ -552,6 +610,7 @@ window.chromadb_interceptGeneration = async (chat, maxContext) => { } } + function onFreezeInput() { extension_settings.chromadb.freeze = $('#chromadb_freeze').is(':checked'); saveSettingsDebounced(); @@ -597,7 +656,12 @@ jQuery(async () => { + + + + Memory Recall Strategy - + + + Memory Sort Strategy + @@ -713,6 +741,7 @@ jQuery(async () => { $('#extensions_settings2').append(settingsHtml); $('#chromadb_strategy').on('change', onStrategyChange); $('#chromadb_recall_strategy').on('change', onRecallStrategyChange); + $('#chromadb_sort_strategy').on('change', onSortStrategyChange); $('#chromadb_keep_context').on('input', onKeepContextInput); $('#chromadb_n_results').on('input', onNResultsInput); $('#chromadb_custom_depth').on('input', onChromaDepthInput); From bd74939a55136773a3d966c24492d05ad9e80d35 Mon Sep 17 00:00:00 2001 From: BlipRanger <1860540+BlipRanger@users.noreply.github.com> Date: Sun, 25 Jun 2023 01:01:47 -0400 Subject: [PATCH 4/5] Add templating for custom injection --- .../extensions/infinity-context/index.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/public/scripts/extensions/infinity-context/index.js b/public/scripts/extensions/infinity-context/index.js index 02aa74e84..9f326c4df 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, getRequestHeaders, CHARACTERS_PER_TOKEN_RATIO } from "../../../script.js"; +import { saveSettingsDebounced, getCurrentChatId, system_message_types, eventSource, event_types, getRequestHeaders, CHARACTERS_PER_TOKEN_RATIO, substituteParams, } 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"; @@ -25,6 +25,7 @@ const defaultSettings = { chroma_depth_min: -1, chroma_depth_max: 500, chroma_depth_step: 1, + chroma_default_msg: "In a past conversation: {{memories}}", split_length: 384, split_length_min: 64, @@ -529,7 +530,7 @@ window.chromadb_interceptGeneration = async (chat, maxContext) => { const currentChatId = getCurrentChatId(); const selectedStrategy = extension_settings.chromadb.strategy; const recallStrategy = extension_settings.chromadb.recall_strategy; - const recallMsg = extension_settings.chromadb.recall_msg; + let recallMsg = extension_settings.chromadb.recall_msg || defaultSettings.chroma_default_msg; const chromaDepth = extension_settings.chromadb.chroma_depth; const chromaSortStrategy = extension_settings.chromadb.sort_strategy; if (currentChatId) { @@ -588,12 +589,16 @@ window.chromadb_interceptGeneration = async (chat, maxContext) => { } if (selectedStrategy === 'custom') { const context = getContext(); - const charname = context.name2; + recallMsg = substituteParams(recallMsg, context.name1, context.name2); + console.log(recallMsg) + let recallStart = recallMsg.split('{{memories}}')[0] + let recallEnd = recallMsg.split('{{memories}}')[1] + newChat.push( { is_name: false, is_user: false, - mes: recallMsg + " [", + mes: recallStart, name: "system", send_date: 0, } @@ -603,7 +608,7 @@ window.chromadb_interceptGeneration = async (chat, maxContext) => { { is_name: false, is_user: false, - mes: `]\n`, + mes: recallEnd + `\n`, name: "system", send_date: 0, } @@ -682,7 +687,7 @@ jQuery(async () => { - + Memory Recall Strategy From d004a3141ecf4d498d29c5266bcf92c8fa8d6464 Mon Sep 17 00:00:00 2001 From: BlipRanger <1860540+BlipRanger@users.noreply.github.com> Date: Sun, 25 Jun 2023 01:03:47 -0400 Subject: [PATCH 5/5] Default bracket, make sure we have memories. --- public/scripts/extensions/infinity-context/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/public/scripts/extensions/infinity-context/index.js b/public/scripts/extensions/infinity-context/index.js index 9f326c4df..cecfe2ee1 100644 --- a/public/scripts/extensions/infinity-context/index.js +++ b/public/scripts/extensions/infinity-context/index.js @@ -25,7 +25,7 @@ const defaultSettings = { chroma_depth_min: -1, chroma_depth_max: 500, chroma_depth_step: 1, - chroma_default_msg: "In a past conversation: {{memories}}", + chroma_default_msg: "In a past conversation: [{{memories}}]", split_length: 384, split_length_min: 64, @@ -590,7 +590,9 @@ window.chromadb_interceptGeneration = async (chat, maxContext) => { if (selectedStrategy === 'custom') { const context = getContext(); recallMsg = substituteParams(recallMsg, context.name1, context.name2); - console.log(recallMsg) + if (!text.includes("{{memories}}")) { + text += " {{memories}}"; + } let recallStart = recallMsg.split('{{memories}}')[0] let recallEnd = recallMsg.split('{{memories}}')[1]