streamed the sample response /stream

pull/854/head
ManishMadan2882 3 months ago
parent 2c991583ff
commit 7a005ef126

@ -9,6 +9,7 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"json-server": "^0.17.4",
"uuid": "^9.0.1"
},

@ -12,6 +12,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"json-server": "^0.17.4",
"uuid": "^9.0.1"
},

@ -1,7 +1,7 @@
import jsonServer from "json-server";
import routes from "./mocks/routes.json" assert { type: "json" };
import { v4 as uuid } from "uuid";
import cors from 'cors'
const server = jsonServer.create();
const router = jsonServer.router("./src/mocks/db.json");
const middlewares = jsonServer.defaults();
@ -9,7 +9,7 @@ const middlewares = jsonServer.defaults();
const localStorage = [];
server.use(middlewares);
server.use(cors({ origin: '*' }))
server.use(jsonServer.rewriter(routes));
server.use((req, res, next) => {
@ -49,16 +49,41 @@ router.render = (req, res) => {
} else {
res.status(404).jsonp({});
}
} else if (req.url === "/stream") {
res.status(200).jsonp({
data: "The answer is 42",
sources: [
"https://en.wikipedia.org/wiki/42_(number)",
"https://en.wikipedia.org/wiki/42_(number)",
],
conversation_id: "1234",
} else if (req.url === "/stream" && req.method === "POST") {
console.log('pinged !')
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
} else {
const message = ('Hi, How are you today?').split(' ');
let index = 0;
const interval = setInterval(() => {
if (index < message.length) {
res.write(`data: {"answer": "${message[index++]} "}\n`);
} else {
res.write(`data: {"type": "id", "id": "65cbc39d11f077b9eeb06d26"}\n`)
res.write(`data: {"type": "end"}\n`)
clearInterval(interval); // Stop the interval once the message is fully streamed
res.end(); // End the response
}
}, 500); // Send a word every 1 second
}
else if (req.url === '/search' && req.method === 'POST') {
res.status(200).json(
[
{
"text": "\n\n/api/answer\nIt's a POST request that sends a JSON in body with 4 values. It will receive an answer for a user provided question.\n",
"title": "API-docs.md"
},
{
"text": "\n\nOur Standards\n\nExamples of behavior that contribute to a positive environment for our\ncommunity include:\n* Demonstrating empathy and kindness towards other people\n",
"title": "How-to-use-different-LLM.md"
}
]
)
}
else {
res.status(res.statusCode).jsonp(res.locals.data);
}
};

Loading…
Cancel
Save