using taLog

This commit is contained in:
mic 2024-10-03 21:27:49 +02:00
parent d79f3f880c
commit 8880c03592

View file

@ -21,12 +21,16 @@
*/
import { OpenAIComp } from '../js/api/openai_comp.js';
import { taLogger } from '../js/mzta-logger.js';
let openai_comp_host = null;
let openai_comp_model = '';
let openai_comp_api_key = '';
let openai_comp = null;
let stopStreaming = false;
let i18nStrings = null;
let do_debug = false;
let taLog = null;
let conversationHistory = [];
let assistantResponseAccumulator = '';
@ -37,6 +41,9 @@ self.onmessage = async function(event) {
openai_comp_model = event.data.openai_comp_model;
openai_comp_api_key = event.data.openai_comp_api_key;
openai_comp = new OpenAIComp(openai_comp_host, openai_comp_model, openai_comp_api_key, true);
do_debug = event.data.do_debug;
i18nStrings = event.data.i18nStrings;
taLog = new taLogger('model-worker-openai_comp', do_debug);
} else if (event.data.type === 'chatMessage') {
conversationHistory.push({ role: 'user', content: event.data.message });
@ -52,9 +59,9 @@ self.onmessage = async function(event) {
const errorJSON = await response.json();
errorDetail = JSON.stringify(errorJSON);
error_message = errorJSON.error.message;
//console.log(">>>>>>>>>>>>> errorJSON.error.message: " + JSON.stringify(errorJSON.error.message));
taLog.log("errorJSON.error.message: " + JSON.stringify(errorJSON.error.message));
}
postMessage({ type: 'error', payload: error_message });
postMessage({ type: 'error', payload: i18nStrings["OpenAIComp_api_request_failed"] + ": " + error_message });
throw new Error("[ThunderAI] OpenAI Comp API request failed: " + response.status + " " + response.statusText + ", Detail: " + errorDetail);
}
@ -80,23 +87,22 @@ self.onmessage = async function(event) {
}
// lots of low-level OpenAI response parsing stuff
chunk += decoder.decode(value);
// console.log(">>>>>>>>>>>>>> [ThunderAI] chunk: " + JSON.stringify(chunk));
taLog.log("chunk: " + JSON.stringify(chunk));
const lines = chunk.split("\n");
let parsedLines = [];
chunk = chunk.substring(chunk.lastIndexOf('\n') + 1);
// console.log(">>>>>>>>>>>>>> [ThunderAI] last chunk: " + JSON.stringify(chunk));
taLog.log("last chunk: " + JSON.stringify(chunk));
try{
parsedLines = lines
.map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix
.filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]"
// .map((line) => JSON.parse(line)); // Parse the JSON string
.map((line) => {
// console.log(">>>>>>>>>>>>> [ThunderAI] line: " + JSON.stringify(line));
taLog.log("line: " + JSON.stringify(line));
return JSON.parse(line);
});
}catch(e){
// console.error(">>>>>>>>>>>>> [ThunderAI] error: " + e);
console.warn("[ThunderAI | model-worker-openai_comp] broken chunk: " + e);
taLog.warn("broken chunk: " + e);
}
for (const parsedLine of parsedLines) {