Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance
Java decentralizedintranet-searchintranet-search-enginep2pp2p-networkprivacyprivacy-protectionsearch-enginesearchenginewebweb-search-enginewebsearchwebseearch-engine
added automatic serch result feedback to chat attachment
Michael Peter Christena5ed11a5dc99
diff --git a/htroot/yacychat.html b/htroot/yacychat.htmlindex 778589d63..120efb8bc 100644--- a/htroot/yacychat.html+++ b/htroot/yacychat.html@@ -381,7 +381,7 @@padding: 8px;border-radius: 4px;font-family: 'Roboto Mono', 'Menlo', 'Consolas', monospace;- font-size: 0.95rem;+ font-size: 1rem;white-space: pre-wrap;}@@ -1113,11 +1113,38 @@let assistantText = '';let sawFirstDelta = false;let focusShown = false;+ let searchApplied = false;if (typeof onFirstToken === 'function' && !focusShown) {focusShown = true;onFirstToken();}let autoScroll = true;+ let pending = '';++ const applySearchAttachment = parsed => {+ if (searchApplied) return;+ if (!parsed) return;+ const searchName = parsed['search-filename'];+ const searchBase64 = parsed['search-text-base64'];+ if (!searchName || !searchBase64) return;+ const dataUrl = `data:text/markdown;base64,${searchBase64}`;+ const textContent = base64ToUtf8(searchBase64);+ for (let i = state.messages.length - 1; i >= 0; i--) {+ const msg = state.messages[i];+ if (msg && msg.role === 'user' && msg.search) {+ msg.attachment = {+ kind: 'text',+ name: searchName,+ dataUrl,+ mime: 'text/markdown',+ textContent+ };+ delete msg.search;+ searchApplied = true;+ break;+ }+ }+ };const performAutoScroll = () => {if (!autoScroll) return;@@ -1129,37 +1156,40 @@};const parseChunk = chunk => {- const segments = chunk- .split(/\n+/)- .flatMap(line => line.split(/(?=data:)/))- .map(line => line.trim())- .filter(Boolean);- for (const line of segments) {- if (line === 'data: [DONE]' || line === '[DONE]') {- return true;- }- const clean = line.replace(/^data:\s*/i, '');- if (!clean) continue;- try {- const parsed = JSON.parse(clean);- const delta = parsed?.choices?.[0]?.delta?.content;- if (delta) {- if (!sawFirstDelta) {- sawFirstDelta = true;- applyMessageContent(assistantNode, 'assistant', '');- if (!focusShown && typeof onFirstToken === 'function') {- focusShown = true;- onFirstToken();+ pending += chunk;+ const lines = pending.split(/\r?\n/);+ pending = lines.pop() || '';+ for (const raw of lines) {+ const fragments = raw.split(/(?=data:\s*)/).map(f => f.trim()).filter(Boolean);+ for (const line of fragments) {+ if (line === 'data: [DONE]' || line === '[DONE]') {+ return true;+ }+ if (!line.startsWith('data:')) continue;+ const clean = line.replace(/^data:\s*/i, '');+ if (!clean) continue;+ try {+ const parsed = JSON.parse(clean);+ applySearchAttachment(parsed);+ const delta = parsed?.choices?.[0]?.delta?.content;+ if (delta) {+ if (!sawFirstDelta) {+ sawFirstDelta = true;+ applyMessageContent(assistantNode, 'assistant', '');+ if (!focusShown && typeof onFirstToken === 'function') {+ focusShown = true;+ onFirstToken();+ }}+ assistantText += delta;+ applyMessageContent(assistantNode, 'assistant', assistantText);+ performAutoScroll();}- assistantText += delta;- applyMessageContent(assistantNode, 'assistant', assistantText);- performAutoScroll();- }} catch (err) {console.warn('Failed to parse line', line, err);}}+ }return false;};let done = false;@@ -1173,6 +1203,9 @@}done = parseChunk(chunk);}+ if (!done && pending.trim()) {+ parseChunk('\n');+ }if (!focusShown && typeof onFirstToken === 'function') {focusShown = true;onFirstToken();@@ -1185,6 +1218,7 @@persistConversation();updateClearChatVisibility();updateSystemToggleButton();+ renderConversation({ skipScroll: true });}function resizeComposer() {@@ -1247,20 +1281,6 @@return '';}- function collectRecentPairs() {- const pairs = [];- let pendingUser = null;- for (const msg of state.messages) {- if (msg.role === 'user') {- pendingUser = messageText(msg.content);- } else if (msg.role === 'assistant' && pendingUser !== null) {- pairs.push({ user: pendingUser, assistant: messageText(msg.content) });- pendingUser = null;- }- }- return pairs.slice(-3);- }-function renderConversation(options = {}) {const { skipScroll = false } = options;closeAttachmentPopover();@@ -1280,8 +1300,11 @@function persistConversation() {try {- const pairs = collectRecentPairs();- localStorage.setItem(STORAGE_KEY, JSON.stringify(pairs));+ const payload = {+ model: state.config.model,+ messages: state.messages+ };+ localStorage.setItem(STORAGE_KEY, JSON.stringify(payload));} catch (err) {console.warn('Failed to persist conversation', err);}@@ -1294,13 +1317,19 @@} catch (err) {console.warn('Failed to parse stored conversation', err);}- if (!Array.isArray(stored) || !stored.length) return;- for (const pair of stored) {- if (pair?.user) {- state.messages.push({ role: 'user', content: pair.user });+ if (Array.isArray(stored) && stored.length) {+ for (const pair of stored) {+ if (pair?.user) {+ state.messages.push({ role: 'user', content: pair.user });+ }+ if (pair?.assistant) {+ state.messages.push({ role: 'assistant', content: pair.assistant });+ }}- if (pair?.assistant) {- state.messages.push({ role: 'assistant', content: pair.assistant });+ } else if (stored && Array.isArray(stored.messages)) {+ state.messages = stored.messages.map(msg => ({ ...msg }));+ if (stored.model) {+ state.config.model = stored.model;}}ensureSystemMessage();@@ -1359,6 +1388,14 @@return `data:${safeMime};base64,${encoded}`;}+ function base64ToUtf8(base64) {+ try {+ return decodeURIComponent(escape(atob(base64)));+ } catch (err) {+ return '';+ }+ }+function getFileExtension(name) {if (!name) return '';const dot = name.lastIndexOf('.');diff --git a/source/net/yacy/http/servlets/RAGProxyServlet.java b/source/net/yacy/http/servlets/RAGProxyServlet.javaindex f67a407ac..0f37915e6 100644--- a/source/net/yacy/http/servlets/RAGProxyServlet.java+++ b/source/net/yacy/http/servlets/RAGProxyServlet.java@@ -33,6 +33,8 @@ import java.util.ArrayList;import java.util.Base64;import java.util.Iterator;import java.util.List;+import java.util.concurrent.BlockingQueue;+import java.util.concurrent.LinkedBlockingQueue;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;@@ -50,6 +52,7 @@ import org.apache.solr.servlet.cache.Method;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;+import org.json.JSONTokener;import net.yacy.ai.LLM;import net.yacy.cora.federate.solr.SolrType;@@ -156,11 +159,12 @@ public class RAGProxyServlet extends HttpServlet {//List<DataURL> data_urls = userObject.getContentAttachments(); // this list is a copy of the content data_urls// RAG+ String searchResultQuery = "";String searchResultMarkdown = "";if (rag) {// modify system and user prompt here in bodyObject to enable RAG- String query = this.searchWordsForPrompt(llm4tldr.llm, llm4tldr.model, user);- searchResultMarkdown = searchResultsAsMarkdown(query, 10);+ searchResultQuery = this.searchWordsForPrompt(llm4tldr.llm, llm4tldr.model, user);+ searchResultMarkdown = searchResultsAsMarkdown(searchResultQuery, 4);user += LLM_USER_PREFIX;user += searchResultMarkdown;userObject.setContentText(user);@@ -170,8 +174,8 @@ public class RAGProxyServlet extends HttpServlet {body = bodyObject.toString();// Open request to back-end service- URL url = new URI(llm4Chat.llm.hoststub + "/v1/chat/completions").toURL();- HttpURLConnection conn = (HttpURLConnection) url.openConnection();+ final URL url = new URI(llm4Chat.llm.hoststub + "/v1/chat/completions").toURL();+ final HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("POST");conn.setRequestProperty("Content-Type", "application/json");if (!llm4Chat.llm.api_key.isEmpty()) {@@ -192,14 +196,41 @@ public class RAGProxyServlet extends HttpServlet {hresponse.setStatus(status);if (status == 200) {- // read the response of the back-end line-by-line and write it to the client line-by-line- final BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));- String inputLine;- while ((inputLine = in.readLine()) != null) {- out.print(inputLine); // i.e. data: {"id":"chatcmpl-69","object":"chat.completion.chunk","created":1715908287,"model":"llama3:8b","system_fingerprint":"fp_ollama","choices":[{"index":0,"delta":{"role":"assistant","content":"ߘ"},"finish_reason":null}]}- out.flush();- }- in.close();+ final BlockingQueue<String> inputQueue = new LinkedBlockingQueue<>();+ final String POISON = "POISON";+ Thread readerThread = new Thread(() -> {+ // read the response of the back-end line-by-line and push it to a stack concurrently+ try {+ final BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));+ String inputLine;+ while ((inputLine = in.readLine()) != null) {inputQueue.put(inputLine);}+ in.close();+ inputQueue.put(POISON);+ } catch (IOException | InterruptedException e) {+ }+ });+ readerThread.start();++ // read the stack line-by-line and write it to the client line-by-line+ try {+ String inputLine;+ int count = 0;+ while (!(inputLine = inputQueue.take()).equals(POISON)) {+ if (count == 0 && searchResultMarkdown.length() > 0) {+ // for the first line we modify the data line to integrate the search result as file+ int p = inputLine.indexOf('{');+ if (p > 0) {+ JSONObject j = new JSONObject(new JSONTokener(inputLine.substring(p)));+ j.put("search-filename", "search_result_"+ searchResultQuery.replace(' ', '_') + ".md");+ j.put("search-text-base64", new String(Base64.getEncoder().encode(searchResultMarkdown.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8));+ inputLine = inputLine.substring(0, p) + j.toString();+ }+ }+ out.print(inputLine); // i.e. data: {"id":"chatcmpl-69","object":"chat.completion.chunk","created":1715908287,"model":"llama3:8b","system_fingerprint":"fp_ollama","choices":[{"index":0,"delta":{"role":"assistant","content":"ߘ"},"finish_reason":null}]}+ out.flush();+ count++;+ }+ } catch (InterruptedException e) {}}out.close(); // close this here to end transmission} catch (JSONException | URISyntaxException e) {