Initial working commit

dnd
Stef Dunlap 2023-03-06 22:59:22 -05:00
parent a605a3f7be
commit 2821bf0b1b
1 changed files with 9 additions and 7 deletions

View File

@ -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 = '';
}
@ -75,4 +77,4 @@ function chatgpt(query, callback) {
}
}
});
}
}