Add returnChunks and resultSize args to /db-search
Signed| @@ -1613,11 +1613,22 @@ jQuery(async () => { | |||
| 1613 | callback: async (args, query) => { | 1613 | callback: async (args, query) => { |
| 1614 | const clamp = (v) => Number.isNaN(v) ? null : Math.min(1, Math.max(0, v)); | 1614 | const clamp = (v) => Number.isNaN(v) ? null : Math.min(1, Math.max(0, v)); |
| 1615 | const threshold = clamp(Number(args?.threshold ?? settings.score_threshold)); | 1615 | 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; | ||
| 1616 | const source = String(args?.source ?? ''); | 1618 | const source = String(args?.source ?? ''); |
| 1617 | const attachments = source ? getDataBankAttachmentsForSource(source, false) : getDataBankAttachments(false); | 1619 | const attachments = source ? getDataBankAttachmentsForSource(source, false) : getDataBankAttachments(false); |
| 1618 | const collectionIds = await ingestDataBankAttachments(String(source)); | 1620 | const collectionIds = await ingestDataBankAttachments(String(source)); |
| 1619 | const queryResults = await queryMultipleCollections(collectionIds, String(query), settings.chunk_count_db, threshold); | 1621 | const queryResults = await queryMultipleCollections(collectionIds, String(query), resultSize, threshold); |
| 1622 | const returnChunks = String(args?.returnChunks).toLowerCase() === 'true' | ||
| 1620 | 1623 | ||
| 1624 | if (returnChunks) { | ||
| 1625 | let textResult = ''; | ||
| 1626 | for (const collectionId in queryResults) { | ||
| 1627 | const metadata = queryResults[collectionId].metadata?.filter(x => x.text)?.sort((a, b) => a.index - b.index)?.map(x => x.text)?.filter(onlyUnique) || []; | ||
| 1628 | textResult += metadata.join('\n') + '\n\n'; | ||
| 1629 | } | ||
| 1630 | return textResult; | ||
| 1631 | } else { | ||
| 1621 | // Map collection IDs to file URLs | 1632 | // Map collection IDs to file URLs |
| 1622 | const urls = Object | 1633 | const urls = Object |
| 1623 | .keys(queryResults) | 1634 | .keys(queryResults) |
| @@ -1626,12 +1637,15 @@ jQuery(async () => { | |||
| 1626 | .map(x => x.url); | 1637 | .map(x => x.url); |
| 1627 | 1638 | ||
| 1628 | return JSON.stringify(urls); | 1639 | return JSON.stringify(urls); |
| 1640 | } | ||
| 1629 | }, | 1641 | }, |
| 1630 | aliases: ['databank-search', 'data-bank-search'], | 1642 | aliases: ['databank-search', 'data-bank-search'], |
| 1631 | helpString: 'Search the Data Bank for a specific query using vector similarity. Returns a list of file URLs with the most relevant content.', | 1643 | helpString: 'Search the Data Bank for a specific query using vector similarity. Returns a list of file URLs with the most relevant content.', |
| 1632 | namedArgumentList: [ | 1644 | namedArgumentList: [ |
| 1633 | 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, ''), | 1645 | 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, ''), | ||
| 1634 | new SlashCommandNamedArgument('source', 'Optional filter for the attachments by source.', ARGUMENT_TYPE.STRING, false, false, '', ['global', 'character', 'chat']), | 1647 | 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']), | ||
| 1635 | ], | 1649 | ], |
| 1636 | unnamedArgumentList: [ | 1650 | unnamedArgumentList: [ |
| 1637 | new SlashCommandArgument('Query to search by.', ARGUMENT_TYPE.STRING, true, false), | 1651 | new SlashCommandArgument('Query to search by.', ARGUMENT_TYPE.STRING, true, false), |