Update server/chatStream.js
Browse files- server/chatStream.js +35 -4
server/chatStream.js
CHANGED
|
@@ -1,6 +1,40 @@
|
|
| 1 |
import OpenAI from "openai";
|
|
|
|
| 2 |
import { LIGHTNING_BASE } from "./config.js";
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
const SYSTEM_PROMPT =
|
| 5 |
"CRITICAL RULE: Every response MUST use HTML <span data-color=\"{COLOR NAME}\"> tags to color main points and headings. " +
|
| 6 |
"COLORS MUST HAVE MEANING AND CONSISTENCY ACROSS THE ENTIRE CONVERSATION. " +
|
|
@@ -259,10 +293,7 @@ async function processToolCalls(ws, toolCalls, tools, accessToken, clientId, abo
|
|
| 259 |
|
| 260 |
try {
|
| 261 |
if (call.function.name === "ollama_search") {
|
| 262 |
-
|
| 263 |
-
const client = await Client.connect("incognitolm/Web-Search");
|
| 264 |
-
const r = await client.predict("/perform_search", { query: args.query });
|
| 265 |
-
result = JSON.stringify(r.data);
|
| 266 |
}
|
| 267 |
|
| 268 |
else if (call.function.name === "read_web_page") {
|
|
|
|
| 1 |
import OpenAI from "openai";
|
| 2 |
+
import { Client } from "@gradio/client";
|
| 3 |
import { LIGHTNING_BASE } from "./config.js";
|
| 4 |
|
| 5 |
+
// ββ Gradio search client singleton βββββββββββββββββββββββββββββββββββββββββ
|
| 6 |
+
// Connect once at module load and reuse across all searches.
|
| 7 |
+
// If the connection drops, gradioClient is reset to null so the next call
|
| 8 |
+
// reconnects automatically rather than retrying a dead client forever.
|
| 9 |
+
let gradioClient = null;
|
| 10 |
+
let gradioConnecting = null; // in-flight connect promise, prevents thundering herd
|
| 11 |
+
|
| 12 |
+
async function getGradioClient() {
|
| 13 |
+
if (gradioClient) return gradioClient;
|
| 14 |
+
if (gradioConnecting) return gradioConnecting;
|
| 15 |
+
gradioConnecting = Client.connect("incognitolm/Web-Search")
|
| 16 |
+
.then(c => { gradioClient = c; gradioConnecting = null; return c; })
|
| 17 |
+
.catch(e => { gradioConnecting = null; throw e; });
|
| 18 |
+
return gradioConnecting;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
async function gradioSearch(query) {
|
| 22 |
+
// Try once; if it fails reset the singleton so the next call gets a fresh connection.
|
| 23 |
+
try {
|
| 24 |
+
const client = await getGradioClient();
|
| 25 |
+
const r = await client.predict("/perform_search", { query });
|
| 26 |
+
// r.data is an array; the search results are in the first element.
|
| 27 |
+
// Accept a string, an array, or an object β normalise all to a string.
|
| 28 |
+
const raw = Array.isArray(r.data) ? r.data[0] : r.data;
|
| 29 |
+
if (!raw) throw new Error("Empty response from search endpoint");
|
| 30 |
+
return typeof raw === "string" ? raw : JSON.stringify(raw);
|
| 31 |
+
} catch (err) {
|
| 32 |
+
// Invalidate the singleton so the next search attempt reconnects.
|
| 33 |
+
gradioClient = null;
|
| 34 |
+
throw err;
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
const SYSTEM_PROMPT =
|
| 39 |
"CRITICAL RULE: Every response MUST use HTML <span data-color=\"{COLOR NAME}\"> tags to color main points and headings. " +
|
| 40 |
"COLORS MUST HAVE MEANING AND CONSISTENCY ACROSS THE ENTIRE CONVERSATION. " +
|
|
|
|
| 293 |
|
| 294 |
try {
|
| 295 |
if (call.function.name === "ollama_search") {
|
| 296 |
+
result = await gradioSearch(args.query);
|
|
|
|
|
|
|
|
|
|
| 297 |
}
|
| 298 |
|
| 299 |
else if (call.function.name === "read_web_page") {
|