incognitolm commited on
Commit ·
4975320
1
Parent(s): 2ec195c
fastapi search
Browse files- server/chatStream.js +37 -26
server/chatStream.js
CHANGED
|
@@ -23,33 +23,44 @@ import { LIGHTNING_BASE } from "./config.js";
|
|
| 23 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 24 |
const WORKER_PATH = path.join(__dirname, "searchWorker.js");
|
| 25 |
|
| 26 |
-
function gradioSearch(query) {
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
worker.terminate();
|
| 32 |
-
reject(new Error("Search timed out after 45s"));
|
| 33 |
-
}, 45_000);
|
| 34 |
-
|
| 35 |
-
worker.on("message", (msg) => {
|
| 36 |
-
clearTimeout(timeout);
|
| 37 |
-
if (msg.ok) resolve(msg.result);
|
| 38 |
-
else reject(new Error(msg.error));
|
| 39 |
-
});
|
| 40 |
-
|
| 41 |
-
worker.on("error", (err) => {
|
| 42 |
-
clearTimeout(timeout);
|
| 43 |
-
reject(err);
|
| 44 |
-
});
|
| 45 |
-
|
| 46 |
-
worker.on("exit", (code) => {
|
| 47 |
-
// By the time this fires the promise is already settled via "message".
|
| 48 |
-
// Only reject if the worker crashed without posting anything.
|
| 49 |
-
clearTimeout(timeout);
|
| 50 |
-
if (code !== 0) reject(new Error(`Search worker exited with code ${code}`));
|
| 51 |
-
});
|
| 52 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
const SYSTEM_PROMPT =
|
|
|
|
| 23 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 24 |
const WORKER_PATH = path.join(__dirname, "searchWorker.js");
|
| 25 |
|
| 26 |
+
async function gradioSearch(query) {
|
| 27 |
+
const req = await fetch("https://incognitolm-Web-Search.hf.space/api/search", {
|
| 28 |
+
method: "POST",
|
| 29 |
+
headers: { "Content-Type": "application/json" },
|
| 30 |
+
body: JSON.stringify({ query }),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
});
|
| 32 |
+
const res = await req.json();
|
| 33 |
+
if (!req.ok || !res.result) {
|
| 34 |
+
throw new Error(`Search API error: ${res.error || req.statusText}`);
|
| 35 |
+
}
|
| 36 |
+
return res.results[0];
|
| 37 |
+
|
| 38 |
+
// return new Promise((resolve, reject) => {
|
| 39 |
+
// const worker = new Worker(WORKER_PATH, { workerData: { query } });
|
| 40 |
+
|
| 41 |
+
// const timeout = setTimeout(() => {
|
| 42 |
+
// worker.terminate();
|
| 43 |
+
// reject(new Error("Search timed out after 45s"));
|
| 44 |
+
// }, 45_000);
|
| 45 |
+
|
| 46 |
+
// worker.on("message", (msg) => {
|
| 47 |
+
// clearTimeout(timeout);
|
| 48 |
+
// if (msg.ok) resolve(msg.result);
|
| 49 |
+
// else reject(new Error(msg.error));
|
| 50 |
+
// });
|
| 51 |
+
|
| 52 |
+
// worker.on("error", (err) => {
|
| 53 |
+
// clearTimeout(timeout);
|
| 54 |
+
// reject(err);
|
| 55 |
+
// });
|
| 56 |
+
|
| 57 |
+
// worker.on("exit", (code) => {
|
| 58 |
+
// // By the time this fires the promise is already settled via "message".
|
| 59 |
+
// // Only reject if the worker crashed without posting anything.
|
| 60 |
+
// clearTimeout(timeout);
|
| 61 |
+
// if (code !== 0) reject(new Error(`Search worker exited with code ${code}`));
|
| 62 |
+
// });
|
| 63 |
+
// });
|
| 64 |
}
|
| 65 |
|
| 66 |
const SYSTEM_PROMPT =
|