Add button to vectorize all chat

This commit is contained in:
Cohee 2023-09-08 15:25:10 +03:00
parent a5acc7872d
commit 3a3ff89047
2 changed files with 56 additions and 0 deletions

View File

@ -17,6 +17,48 @@ const settings = {
const moduleWorker = new ModuleWorkerWrapper(synchronizeChat);
async function onVectorizeAllClick() {
try {
if (!settings.enabled) {
return;
}
const chatId = getCurrentChatId();
const batchSize = 5;
const elapsedLog = [];
let finished = false;
$('#vectorize_progress').show();
$('#vectorize_progress_percent').text('0');
$('#vectorize_progress_eta').text('...');
while (!finished) {
const startTime = Date.now();
const remaining = await synchronizeChat(batchSize);
const elapsed = Date.now() - startTime;
elapsedLog.push(elapsed);
finished = remaining <= 0;
const total = getContext().chat.length;
const processed = total - remaining;
const processedPercent = Math.round((processed / total) * 100); // percentage of the work done
const averageElapsed = elapsedLog.slice(-5).reduce((a, b) => a + b, 0) / elapsedLog.length; // average time needed to process one item
const pace = averageElapsed / batchSize; // time needed to process one item
const remainingTime = Math.round(pace * remaining / 1000);
$('#vectorize_progress_percent').text(processedPercent);
$('#vectorize_progress_eta').text(remainingTime);
if (chatId !== getCurrentChatId()) {
throw new Error('Chat changed');
}
}
} catch (error) {
console.error('Vectors: Failed to vectorize all', error);
} finally {
$('#vectorize_progress').hide();
}
}
async function synchronizeChat(batchSize = 10) {
try {
if (!settings.enabled) {
@ -283,6 +325,7 @@ jQuery(async () => {
Object.assign(extension_settings.vectors, settings);
saveSettingsDebounced();
});
$('#vectors_vectorize_all').on('click', onVectorizeAllClick);
eventSource.on(event_types.CHAT_CHANGED, onChatEvent);
eventSource.on(event_types.MESSAGE_DELETED, onChatEvent);

View File

@ -16,6 +16,19 @@
<option value="local">Local</option>
<option value="openai">OpenAI</option>
</select>
<div>
Old messages are vectorized gradually as you chat.
To process all previous messages, click the button below.
</div>
<div id="vectors_vectorize_all" class="menu_button menu_button_icon">
Vectorize All
</div>
<div id="vectorize_progress" style="display: none;">
<small>
Processed <span id="vectorize_progress_percent">0</span>% of messages.
ETA: <span id="vectorize_progress_eta">...</span> seconds.
</small>
</div>
</div>
</div>
</div>