Add sort options, remove duplicate entires from custom.

This commit is contained in:
BlipRanger
2023-06-25 00:17:58 -04:00
parent 71201377ef
commit 2836704c4e

View File

@@ -9,6 +9,7 @@ const dbStore = new IndexedDBStore('SillyTavern', MODULE_NAME);
const defaultSettings = { const defaultSettings = {
strategy: 'original', strategy: 'original',
sort_strategy: 'date',
keep_context: 10, keep_context: 10,
keep_context_min: 1, keep_context_min: 1,
@@ -101,6 +102,10 @@ async function loadSettings() {
"selected", "selected",
"true" "true"
); );
$("#chromadb_sort_strategy option[value=" + extension_settings.chromadb.sort_strategy + "]").attr(
"selected",
"true"
);
$('#chromadb_keep_context').val(extension_settings.chromadb.keep_context).trigger('input'); $('#chromadb_keep_context').val(extension_settings.chromadb.keep_context).trigger('input');
$('#chromadb_n_results').val(extension_settings.chromadb.n_results).trigger('input'); $('#chromadb_n_results').val(extension_settings.chromadb.n_results).trigger('input');
$('#chromadb_split_length').val(extension_settings.chromadb.split_length).trigger('input'); $('#chromadb_split_length').val(extension_settings.chromadb.split_length).trigger('input');
@@ -138,6 +143,13 @@ function onRecallStrategyChange() {
saveSettingsDebounced(); saveSettingsDebounced();
} }
function onSortStrategyChange() {
console.log('changing chromadb sort strat');
extension_settings.chromadb.sort_strategy = $('#chromadb_sort_strategy').val();
saveSettingsDebounced();
}
function onKeepContextInput() { function onKeepContextInput() {
extension_settings.chromadb.keep_context = Number($('#chromadb_keep_context').val()); extension_settings.chromadb.keep_context = Number($('#chromadb_keep_context').val());
$('#chromadb_keep_context_value').text(extension_settings.chromadb.keep_context); $('#chromadb_keep_context_value').text(extension_settings.chromadb.keep_context);
@@ -401,7 +413,6 @@ async function queryMultiMessages(chat_id, query) {
if (queryMessagesResult.ok) { if (queryMessagesResult.ok) {
const queryMessagesData = await queryMessagesResult.json(); const queryMessagesData = await queryMessagesResult.json();
return queryMessagesData; return queryMessagesData;
} }
@@ -520,6 +531,7 @@ window.chromadb_interceptGeneration = async (chat, maxContext) => {
const recallStrategy = extension_settings.chromadb.recall_strategy; const recallStrategy = extension_settings.chromadb.recall_strategy;
const recallMsg = extension_settings.chromadb.recall_msg; const recallMsg = extension_settings.chromadb.recall_msg;
const chromaDepth = extension_settings.chromadb.chroma_depth; const chromaDepth = extension_settings.chromadb.chroma_depth;
const chromaSortStrategy = extension_settings.chromadb.sort_strategy;
if (currentChatId) { if (currentChatId) {
const messagesToStore = chat.slice(0, -extension_settings.chromadb.keep_context); const messagesToStore = chat.slice(0, -extension_settings.chromadb.keep_context);
@@ -539,9 +551,15 @@ window.chromadb_interceptGeneration = async (chat, maxContext) => {
queriedMessages = await queryMessages(currentChatId, lastMessage.mes); queriedMessages = await queryMessages(currentChatId, lastMessage.mes);
} }
if(chromaSortStrategy === "date"){
queriedMessages.sort((a, b) => a.date - b.date); queriedMessages.sort((a, b) => a.date - b.date);
}
else{
queriedMessages.sort((a, b) => b.distance - a.distance);
}
const newChat = [];
let newChat = [];
if (selectedStrategy === 'ross') { if (selectedStrategy === 'ross') {
//adds chroma to the end of chat and allows Generate() to cull old messages naturally. //adds chroma to the end of chat and allows Generate() to cull old messages naturally.
@@ -590,8 +608,13 @@ window.chromadb_interceptGeneration = async (chat, maxContext) => {
send_date: 0, send_date: 0,
} }
); );
//prototype chroma duplicate removal
let chatset = new Set(chat.map(obj => obj.mes));
newChat = newChat.filter(obj => !chatset.has(obj.mes));
if(chromaDepth === -1){ if(chromaDepth === -1){
chat.splice(messagesToStore.length, 0, ...newChat); chat.splice(chat.length, 0, ...newChat);
} }
else{ else{
chat.splice(chromaDepth, 0, ...newChat); chat.splice(chromaDepth, 0, ...newChat);
@@ -665,7 +688,12 @@ jQuery(async () => {
<span>Memory Recall Strategy</span> <span>Memory Recall Strategy</span>
<select id="chromadb_recall_strategy"> <select id="chromadb_recall_strategy">
<option value="original">Recall only from this chat</option> <option value="original">Recall only from this chat</option>
<option value="multichat">Recall from all card chats (experimental)</option> <option value="multichat">Recall from all character chats (experimental)</option>
</select>
<span>Memory Sort Strategy</span>
<select id="chromadb_sort_strategy">
<option value="date">Sort memories by date</option>
<option value="distance">Sort memories by relevance</option>
</select> </select>
<label for="chromadb_keep_context">How many original chat messages to keep: (<span id="chromadb_keep_context_value"></span>) messages</label> <label for="chromadb_keep_context">How many original chat messages to keep: (<span id="chromadb_keep_context_value"></span>) messages</label>
<label for="chromadb_keep_context"><small>How many original chat messages to keep: (<span id="chromadb_keep_context_value"></span>) messages</small></label> <label for="chromadb_keep_context"><small>How many original chat messages to keep: (<span id="chromadb_keep_context_value"></span>) messages</small></label>
@@ -713,6 +741,7 @@ jQuery(async () => {
$('#extensions_settings2').append(settingsHtml); $('#extensions_settings2').append(settingsHtml);
$('#chromadb_strategy').on('change', onStrategyChange); $('#chromadb_strategy').on('change', onStrategyChange);
$('#chromadb_recall_strategy').on('change', onRecallStrategyChange); $('#chromadb_recall_strategy').on('change', onRecallStrategyChange);
$('#chromadb_sort_strategy').on('change', onSortStrategyChange);
$('#chromadb_keep_context').on('input', onKeepContextInput); $('#chromadb_keep_context').on('input', onKeepContextInput);
$('#chromadb_n_results').on('input', onNResultsInput); $('#chromadb_n_results').on('input', onNResultsInput);
$('#chromadb_custom_depth').on('input', onChromaDepthInput); $('#chromadb_custom_depth').on('input', onChromaDepthInput);