Add poe streaming

This commit is contained in:
SillyLossy
2023-04-12 02:26:09 +03:00
parent 2040b8d3ff
commit e1b514ef5a
4 changed files with 79 additions and 17 deletions

View File

@ -1776,22 +1776,39 @@ app.post('/generate_poe', jsonParser, async (request, response) => {
const token = request.body.token;
const prompt = request.body.prompt;
const bot = request.body.bot ?? POE_DEFAULT_BOT;
const streaming = request.body.streaming ?? false;
try {
const client = await getPoeClient(token);
let reply;
for await (const mes of client.send_message(bot, prompt)) {
reply = mes.text;
if (streaming) {
response.writeHead(200, {
'Transfer-Encoding': 'chunked',
'Cache-Control': 'no-transform',
});
let reply = '';
for await (const mes of client.send_message(bot, prompt)) {
let newText = mes.text.substring(reply.length);
reply = mes.text;
response.write(newText);
}
console.log(reply);
client.disconnect_ws();
response.end();
}
else {
let reply;
for await (const mes of client.send_message(bot, prompt)) {
reply = mes.text;
}
console.log(reply);
client.disconnect_ws();
return response.send({ 'reply': reply });
}
console.log(reply);
client.disconnect_ws();
return response.send({ 'reply': reply });
}
catch {
catch (error) {
console.error(error);
return response.sendStatus(500);
}
});