Add returnChunks and resultSize args to /db-search
Signed| @@ -1613,11 +1613,22 @@ jQuery(async () => { | ||
| 1613 | 1613 | callback: async (args, query) => { |
| 1614 | 1614 | const clamp = (v) => Number.isNaN(v) ? null : Math.min(1, Math.max(0, v)); |
| 1615 | 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 | 1618 | const source = String(args?.source ?? ''); |
| 1617 | 1619 | const attachments = source ? getDataBankAttachmentsForSource(source, false) : getDataBankAttachments(false); |
| 1618 | 1620 | const collectionIds = await ingestDataBankAttachments(String(source)); |
| 1619 | 1621 | const queryResults = await queryMultipleCollections(collectionIds, String(query), settings.chunk_count_dbresultSize, 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 | 1632 | // Map collection IDs to file URLs |
| 1622 | 1633 | const urls = Object |
| 1623 | 1634 | .keys(queryResults) |
| @@ -1626,12 +1637,15 @@ jQuery(async () => { | ||
| 1626 | 1637 | .map(x => x.url); |
| 1627 | 1638 | |
| 1628 | 1639 | return JSON.stringify(urls); |
| 1640 | + } | |
| 1629 | 1641 | }, |
| 1630 | 1642 | aliases: ['databank-search', 'data-bank-search'], |
| 1631 | 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 | 1644 | namedArgumentList: [ |
| 1633 | 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 | 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 | 1650 | unnamedArgumentList: [ |
| 1637 | 1651 | new SlashCommandArgument('Query to search by.', ARGUMENT_TYPE.STRING, true, false), |