Initial working commit
parent
a605a3f7be
commit
2821bf0b1b
14
index.js
14
index.js
|
@ -18,7 +18,7 @@ client.addListener('message', async (from, to, message) => {
|
|||
});
|
||||
|
||||
// function that calls the chatgpt streaming api (with server send events) and calls the callback function for each line
|
||||
function chatgpt(query, callback) {
|
||||
async function chatgpt(query, callback) {
|
||||
const apiUrl = 'https://api.openai.com/v1/chat/completions';
|
||||
|
||||
const response = await axios.post(apiUrl, {
|
||||
|
@ -40,17 +40,19 @@ function chatgpt(query, callback) {
|
|||
// parse if starts with data:
|
||||
for(part of parts) {
|
||||
if(part === 'data: [DONE]') {
|
||||
process.stdout.write('\n');
|
||||
callback(line);
|
||||
line = '';
|
||||
} else if(part.startsWith('data: ')) {
|
||||
let jsonString = part.slice(part.indexOf('{'), part.lastIndexOf('}') + 1);
|
||||
try {
|
||||
let json = JSON.parse(jsonString);
|
||||
let chunk = json.choices[0].delta.content;
|
||||
if(!chunk) { return; }
|
||||
//split the chunk into lines leaving the delimiter in the array
|
||||
const lines = str.split(/\r?\n/); // split by new lines
|
||||
const lines = chunk.split(/\r?\n/); // split by new lines
|
||||
|
||||
let hasStartNewline = str.startsWith("\n");
|
||||
let hasEndNewline = str.endsWith("\n");
|
||||
let hasStartNewline = chunk.startsWith("\n");
|
||||
let hasEndNewline = chunk.endsWith("\n");
|
||||
|
||||
if(hasStartNewline) {
|
||||
callback(line);
|
||||
|
@ -58,7 +60,7 @@ function chatgpt(query, callback) {
|
|||
}
|
||||
|
||||
for (let i = 0; i < lines.length - 1; i++) {
|
||||
callback(lines[i]);
|
||||
callback(line+lines[i]);
|
||||
line = '';
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue