mirror of
https://github.com/arc53/DocsGPT.git
synced 2025-11-30 00:53:14 +00:00
streamed the sample response /stream
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user