diff --git a/index.js b/index.js index 6a330a0..76c51f7 100644 --- a/index.js +++ b/index.js @@ -90,7 +90,7 @@ async function chatgpt(query, callback) { let parts = data.split('\n'); // parse if starts with data: for(part of parts) { - console.log(part); + console.log(part); if(part === 'data: [DONE]') { callback(context.finish_current_response()); } else if(part.startsWith('data: ')) { @@ -98,11 +98,10 @@ async function chatgpt(query, callback) { try { let json = JSON.parse(jsonString); let chunk = json.choices[0].delta.content; - if(!chunk) { - callback(line); - line = ''; - continue; - } + if (!chunk) { + callback(context.end_line()); + continue; + } //split the chunk into lines leaving the delimiter in the array const lines = chunk.split(/\r?\n/); // split by new lines @@ -124,10 +123,9 @@ async function chatgpt(query, callback) { callback(context.end_line()); } - if(line.length > 400) { - callback(line); - line = ''; - } + if (line.length > 400) { + callback(context.end_line()); + } } catch (e) { console.log(e); console.log(part);