Add returnChunks and resultSize args to /db-search

7f5ce54b1f7b8e017f2b38b78584bf75481c86c1

zerofata <fxsliam@outlook.com>

Signed
1 files changed, +24 -10Ignore whitespace
public/scripts/extensions/vectors/index.js+24 -10
@@ -1613,25 +1613,39 @@ jQuery(async () => {
16131613 callback: async (args, query) => {
16141614 const clamp = (v) => Number.isNaN(v) ? null : Math.min(1, Math.max(0, v));
16151615 const threshold = clamp(Number(args?.threshold ?? settings.score_threshold));
1616+ const validateSize = (v) => Number.isNaN(v) || !Number.isInteger(v) || v < 1 ? null : v;
1617+ const resultSize = validateSize(Number(args?.resultSize)) ?? settings.chunk_count_db;
16161618 const source = String(args?.source ?? '');
16171619 const attachments = source ? getDataBankAttachmentsForSource(source, false) : getDataBankAttachments(false);
16181620 const collectionIds = await ingestDataBankAttachments(String(source));
16191621 const queryResults = await queryMultipleCollections(collectionIds, String(query), settings.chunk_count_dbresultSize, threshold);
1620-
1622+ const returnChunks = String(args?.returnChunks).toLowerCase() === 'true'
1621- // Map collection IDs to file URLs
1623+
1622- const urls = Object
1624+ if (returnChunks) {
1623- .keys(queryResults)
1625+ let textResult = '';
1624- .map(x => attachments.find(y => getFileCollectionId(y.url) === x))
1626+ for (const collectionId in queryResults) {
1625- .filter(x => x)
1627+ const metadata = queryResults[collectionId].metadata?.filter(x => x.text)?.sort((a, b) => a.index - b.index)?.map(x => x.text)?.filter(onlyUnique) || [];
1626- .map(x => x.url);
1628+ textResult += metadata.join('\n') + '\n\n';
1627-
1629+ }
1628- return JSON.stringify(urls);
1630+ return textResult;
1631+ } else {
1632+ // Map collection IDs to file URLs
1633+ const urls = Object
1634+ .keys(queryResults)
1635+ .map(x => attachments.find(y => getFileCollectionId(y.url) === x))
1636+ .filter(x => x)
1637+ .map(x => x.url);
1638+
1639+ return JSON.stringify(urls);
1640+ }
16291641 },
16301642 aliases: ['databank-search', 'data-bank-search'],
16311643 helpString: 'Search the Data Bank for a specific query using vector similarity. Returns a list of file URLs with the most relevant content.',
16321644 namedArgumentList: [
16331645 new SlashCommandNamedArgument('threshold', 'Threshold for the similarity score in the [0, 1] range. Uses the global config value if not set.', ARGUMENT_TYPE.NUMBER, false, false, ''),
1646+ new SlashCommandNamedArgument('resultSize', 'Maximum number of query results to return.', ARGUMENT_TYPE.NUMBER, false, false, ''),
16341647 new SlashCommandNamedArgument('source', 'Optional filter for the attachments by source.', ARGUMENT_TYPE.STRING, false, false, '', ['global', 'character', 'chat']),
1648+ new SlashCommandNamedArgument('returnChunks', 'If true, returns the actual content chunks instead of URLs.', ARGUMENT_TYPE.STRING, false, false, '', ['true', 'false']),
16351649 ],
16361650 unnamedArgumentList: [
16371651 new SlashCommandArgument('Query to search by.', ARGUMENT_TYPE.STRING, true, false),