Adding Slash Commands for Vector Storage Extension (#5008) * Add vector slash commands * Updated vector-threshold command per feedback. Want to validate it is correct before fixing other commands. * Added slash commands for several vector storage settings. * Added slash commands for vector storage, updated w/dev feedback * Fix min value of entries --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>

8926cdc5ce6a9fc8dd7ca90df364b98abecb54dd

adventchilde / aethel <93360954+adventchilde@users.noreply.github.com>

Signed
1 files changed, +170 -1Showing whitespace changes
public/scripts/extensions/vectors/index.js+170 -1
@@ -24,7 +24,7 @@ import {
2424import { collapseNewlines, registerDebugFunction } from '../../power-user.js';
2525import { SECRET_KEYS, secret_state } from '../../secrets.js';
2626import { getDataBankAttachments, getDataBankAttachmentsForSource, getFileAttachment } from '../../chats.js';
2727import { debounce, getStringHash as calculateHash, waitUntilCondition, onlyUnique, splitRecursive, trimToStartSentence, trimToEndSentence, escapeHtml, isTrueBoolean } from '../../utils.js';
2828import { debounce_timeout } from '../../constants.js';
2929import { getSortedEntries } from '../../world-info.js';
3030import { textgen_types, textgenerationwebui_settings } from '../../textgen-settings.js';
@@ -32,6 +32,7 @@ import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';
3232import { SlashCommand } from '../../slash-commands/SlashCommand.js';
3333import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js';
3434import { SlashCommandEnumValue, enumTypes } from '../../slash-commands/SlashCommandEnumValue.js';
35+import { commonEnumProviders } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
3536import { slashCommandReturnHelper } from '../../slash-commands/SlashCommandReturnHelper.js';
3637import { generateWebLlmChatPrompt, isWebLlmSupported } from '../shared.js';
3738import { WebLlmVectorProvider } from './webllm.js';
@@ -2036,6 +2037,174 @@ jQuery(async () => {
20362037 returns: ARGUMENT_TYPE.LIST,
20372038 }));
20382039
2040+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2041+ name: 'vector-threshold',
2042+ helpString: 'Set the vector score threshold or return the current threshold if no argument is provided.',
2043+ returns: 'score threshold value',
2044+ unnamedArgumentList: [
2045+ SlashCommandArgument.fromProps({
2046+ description: 'Score threshold (number).',
2047+ typeList: [ARGUMENT_TYPE.NUMBER],
2048+ }),
2049+ ],
2050+ callback: async (_args, value) => {
2051+ const raw = String(value ?? '').trim();
2052+ if (!raw) {
2053+ return String(settings.score_threshold);
2054+ }
2055+
2056+ const parsed = Number(raw);
2057+ if (!Number.isFinite(parsed) || parsed < 0 || parsed > 1) {
2058+ toastr.warning('Score threshold must be a number between 0 and 1.');
2059+ return '';
2060+ }
2061+
2062+ $('#vectors_score_threshold')
2063+ .val(parsed)
2064+ .trigger('input');
2065+
2066+ return String(settings.score_threshold);
2067+ },
2068+ }));
2069+
2070+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2071+ name: 'vector-query',
2072+ helpString: 'Set the vector query messages or returns the current query messages count if no argument is provided',
2073+ returns: 'the query messages value',
2074+ unnamedArgumentList: [
2075+ SlashCommandArgument.fromProps({
2076+ description: 'Query messages (number >= 0).',
2077+ typeList: [ARGUMENT_TYPE.NUMBER],
2078+ }),
2079+ ],
2080+ callback: async (_args, value) => {
2081+ const raw = String(value ?? '').trim();
2082+ if (!raw) {
2083+ return String(settings.query);
2084+ }
2085+
2086+ const parsed = Number(raw);
2087+ if (!Number.isFinite(parsed) || parsed < 0) {
2088+ toastr.warning('Query messages must be a number greater than or equal to 0.');
2089+ return '';
2090+ }
2091+
2092+ $('#vectors_query')
2093+ .val(parsed)
2094+ .trigger('input');
2095+
2096+ return String(settings.query);
2097+ },
2098+ }));
2099+
2100+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2101+ name: 'vector-max-entries',
2102+ helpString: 'Set the vector world info max entries or returns the current max entries if no argument is provided',
2103+ returns: 'world info max entries',
2104+ unnamedArgumentList: [
2105+ SlashCommandArgument.fromProps({
2106+ description: 'Max entries (number >= 0).',
2107+ typeList: [ARGUMENT_TYPE.NUMBER],
2108+ }),
2109+ ],
2110+ callback: async (_args, value) => {
2111+ const raw = String(value ?? '').trim();
2112+ if (!raw) {
2113+ return String(settings.max_entries);
2114+ }
2115+
2116+ const parsed = Number(raw);
2117+ if (!Number.isFinite(parsed) || parsed <= 0) {
2118+ toastr.warning('Max entries must be a number greater than 0.');
2119+ return '';
2120+ }
2121+
2122+ $('#vectors_max_entries')
2123+ .val(parsed)
2124+ .trigger('input');
2125+
2126+ return String(settings.max_entries);
2127+ },
2128+ }));
2129+
2130+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2131+ name: 'vector-chats-state',
2132+ helpString: 'Set whether chat vectorization is enabled or return the current boolean if no argument is provided',
2133+ returns: 'boolean for if chat vectorization is enabled',
2134+ unnamedArgumentList: [
2135+ SlashCommandArgument.fromProps({
2136+ description: 'boolean to set whether chat vectorization is enabled',
2137+ typeList: [ARGUMENT_TYPE.BOOLEAN],
2138+ enumList: commonEnumProviders.boolean('trueFalse')(),
2139+ }),
2140+ ],
2141+ callback: async (_args, value) => {
2142+ const raw = String(value ?? '').trim();
2143+ if (!raw) {
2144+ return String(settings.enabled_chats);
2145+ }
2146+
2147+ const parsed = isTrueBoolean(raw);
2148+ $('#vectors_enabled_chats')
2149+ .prop('checked', parsed)
2150+ .trigger('input');
2151+
2152+ return String(settings.enabled_chats);
2153+ },
2154+ }));
2155+
2156+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2157+ name: 'vector-files-state',
2158+ helpString: 'Set whether file vectorization is enabled or return the current boolean if no argument is provided',
2159+ returns: 'boolean for if file vectorization is enabled',
2160+ unnamedArgumentList: [
2161+ SlashCommandArgument.fromProps({
2162+ description: 'boolean to set whether file vectorization is enabled',
2163+ typeList: [ARGUMENT_TYPE.BOOLEAN],
2164+ enumList: commonEnumProviders.boolean('trueFalse')(),
2165+ }),
2166+ ],
2167+ callback: async (_args, value) => {
2168+ const raw = String(value ?? '').trim();
2169+ if (!raw) {
2170+ return String(settings.enabled_files);
2171+ }
2172+
2173+ const parsed = isTrueBoolean(raw) ;
2174+ $('#vectors_enabled_files')
2175+ .prop('checked', parsed)
2176+ .trigger('input');
2177+
2178+ return String(settings.enabled_files);
2179+ },
2180+ }));
2181+
2182+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2183+ name: 'vector-worldinfo-state',
2184+ helpString: 'Set whether world info vectorization is enabled or return the current boolean if no argument is provided',
2185+ returns: 'boolean for if world info vectorization is enabled',
2186+ unnamedArgumentList: [
2187+ SlashCommandArgument.fromProps({
2188+ description: 'boolean to set whether world info vectorization is enabled',
2189+ typeList: [ARGUMENT_TYPE.BOOLEAN],
2190+ enumList: commonEnumProviders.boolean('trueFalse')(),
2191+ }),
2192+ ],
2193+ callback: async (_args, value) => {
2194+ const raw = String(value ?? '').trim();
2195+ if (!raw) {
2196+ return String(settings.enabled_world_info);
2197+ }
2198+
2199+ const parsed = isTrueBoolean(raw);
2200+ $('#vectors_enabled_world_info')
2201+ .prop('checked', parsed)
2202+ .trigger('input');
2203+
2204+ return String(settings.enabled_world_info);
2205+ },
2206+ }));
2207+
20392208 registerDebugFunction('purge-everything', 'Purge all vector indices', 'Obliterate all stored vectors for all sources. No mercy.', async () => {
20402209 if (!confirm('Are you sure?')) {
20412210 return;