Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance
Java decentralizedintranet-searchintranet-search-enginep2pp2p-networkprivacyprivacy-protectionsearch-enginesearchenginewebweb-search-enginewebsearchwebseearch-engine
Add configurable maximum length for RAG search documents
Michael Peter Christenc79b7a19c8d6
diff --git a/htroot/RAGConfig_p.html b/htroot/RAGConfig_p.htmlindex 51317e78d..c61b9641c 100644--- a/htroot/RAGConfig_p.html+++ b/htroot/RAGConfig_p.html@@ -53,6 +53,12 @@<textarea name="ai.llm-query-generator-prefix" class="rag-textarea form-control" rows="5">#[ai.llm-query-generator-prefix]#</textarea></div>+ <div class="rag-card">+ <h3>Search Document Max Length</h3>+ <p>Maximum character length of the virtual search document used as RAG attachment and as the `search` tool result. Content beyond this limit is cut off. Default: 30000.</p>+ <input type="number" min="1" step="1" name="ai.rag.search-document-maxlength" value="#[ai.rag.search-document-maxlength]#" class="form-control" />+ </div>+<button type="submit" class="btn btn-primary">Save RAG Settings</button></form>diff --git a/source/net/yacy/ai/RAGAugmentor.java b/source/net/yacy/ai/RAGAugmentor.javaindex 71b285504..a2dde813c 100644--- a/source/net/yacy/ai/RAGAugmentor.java+++ b/source/net/yacy/ai/RAGAugmentor.java@@ -68,11 +68,28 @@ import net.yacy.search.snippet.TextSnippet;*/public final class RAGAugmentor {+ public static final String SEARCH_DOCUMENT_MAX_LENGTH_CONFIG = "ai.rag.search-document-maxlength";+ public static final int SEARCH_DOCUMENT_MAX_LENGTH_DEFAULT = 30000;+/*** Utility class; not instantiable.*/private RAGAugmentor() {}+ public static int configuredSearchDocumentMaxLength() {+ final Switchboard sb = Switchboard.getSwitchboard();+ if (sb == null) return SEARCH_DOCUMENT_MAX_LENGTH_DEFAULT;+ final long configured = sb.getConfigLong(SEARCH_DOCUMENT_MAX_LENGTH_CONFIG, SEARCH_DOCUMENT_MAX_LENGTH_DEFAULT);+ if (configured <= 0) return SEARCH_DOCUMENT_MAX_LENGTH_DEFAULT;+ return configured > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) configured;+ }++ private static String truncateSearchDocument(final String markdown) {+ if (markdown == null || markdown.isEmpty()) return "";+ final int maxLength = configuredSearchDocumentMaxLength();+ return markdown.length() <= maxLength ? markdown : markdown.substring(0, maxLength);+ }+/*** Executes local index search.*@@ -117,8 +134,9 @@ public final class RAGAugmentor {} catch (JSONException e) {}}- ConcurrentLog.info("RAGProxy", "markdownChars=" + sb.length() + " resultCount=" + searchResults.length());- return sb.toString();+ final String markdown = truncateSearchDocument(sb.toString());+ ConcurrentLog.info("RAGProxy", "markdownChars=" + markdown.length() + " resultCount=" + searchResults.length());+ return markdown;}/**diff --git a/source/net/yacy/htroot/RAGConfig_p.java b/source/net/yacy/htroot/RAGConfig_p.javaindex 819d9832d..23a5beabe 100644--- a/source/net/yacy/htroot/RAGConfig_p.java+++ b/source/net/yacy/htroot/RAGConfig_p.java@@ -18,10 +18,13 @@ public class RAGConfig_p {final String systemPrompt = post.get("ai.system-prompt", "").trim();final String userPrefix = post.get("ai.llm-user-prefix", "").trim();final String queryPrefix = post.get("ai.llm-query-generator-prefix", "").trim();+ int searchDocumentMaxLength = post.getInt(net.yacy.ai.RAGAugmentor.SEARCH_DOCUMENT_MAX_LENGTH_CONFIG, net.yacy.ai.RAGAugmentor.SEARCH_DOCUMENT_MAX_LENGTH_DEFAULT);+ if (searchDocumentMaxLength <= 0) searchDocumentMaxLength = net.yacy.ai.RAGAugmentor.SEARCH_DOCUMENT_MAX_LENGTH_DEFAULT;sb.setConfig("ai.system-prompt", systemPrompt);sb.setConfig("ai.llm-user-prefix", userPrefix);sb.setConfig("ai.llm-query-generator-prefix", queryPrefix);+ sb.setConfig(net.yacy.ai.RAGAugmentor.SEARCH_DOCUMENT_MAX_LENGTH_CONFIG, Integer.toString(searchDocumentMaxLength));}// mark page as visited for gamification/quest tracking@@ -30,6 +33,7 @@ public class RAGConfig_p {prop.put("ai.system-prompt", sb.getConfig("ai.system-prompt", net.yacy.http.servlets.RAGProxyServlet.LLM_SYSTEM_PROMPT_DEFAULT));prop.put("ai.llm-user-prefix", sb.getConfig("ai.llm-user-prefix", "\n\nAdditional Information:\n\nbelow you find a collection of texts that might be useful to generate a response. Do not discuss these documents, just use them to answer the question above.\n\n"));prop.put("ai.llm-query-generator-prefix", sb.getConfig("ai.llm-query-generator-prefix", "Make a list of search words with low document frequency for the following prompt; use a JSON Array: "));+ prop.put("ai.rag.search-document-maxlength", sb.getConfig(net.yacy.ai.RAGAugmentor.SEARCH_DOCUMENT_MAX_LENGTH_CONFIG, Integer.toString(net.yacy.ai.RAGAugmentor.SEARCH_DOCUMENT_MAX_LENGTH_DEFAULT)));return prop;}