Make sure a line doesn't get cut off
parent
2821bf0b1b
commit
cb42f77ed2
12
index.js
12
index.js
|
@ -39,6 +39,7 @@ async function chatgpt(query, callback) {
|
|||
let parts = data.split('\n');
|
||||
// parse if starts with data:
|
||||
for(part of parts) {
|
||||
console.log(part);
|
||||
if(part === 'data: [DONE]') {
|
||||
callback(line);
|
||||
line = '';
|
||||
|
@ -47,7 +48,11 @@ async function chatgpt(query, callback) {
|
|||
try {
|
||||
let json = JSON.parse(jsonString);
|
||||
let chunk = json.choices[0].delta.content;
|
||||
if(!chunk) { return; }
|
||||
if(!chunk) {
|
||||
callback(line);
|
||||
line = '';
|
||||
continue;
|
||||
}
|
||||
//split the chunk into lines leaving the delimiter in the array
|
||||
const lines = chunk.split(/\r?\n/); // split by new lines
|
||||
|
||||
|
@ -70,6 +75,11 @@ async function chatgpt(query, callback) {
|
|||
callback(line);
|
||||
line = '';
|
||||
}
|
||||
|
||||
if(line.length > 400) {
|
||||
callback(line);
|
||||
line = '';
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
console.log(part);
|
||||
|
|
Loading…
Reference in New Issue