using taLog
This commit is contained in:
parent
d79f3f880c
commit
8880c03592
1 changed files with 13 additions and 7 deletions
|
|
@ -21,12 +21,16 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { OpenAIComp } from '../js/api/openai_comp.js';
|
import { OpenAIComp } from '../js/api/openai_comp.js';
|
||||||
|
import { taLogger } from '../js/mzta-logger.js';
|
||||||
|
|
||||||
let openai_comp_host = null;
|
let openai_comp_host = null;
|
||||||
let openai_comp_model = '';
|
let openai_comp_model = '';
|
||||||
let openai_comp_api_key = '';
|
let openai_comp_api_key = '';
|
||||||
let openai_comp = null;
|
let openai_comp = null;
|
||||||
let stopStreaming = false;
|
let stopStreaming = false;
|
||||||
|
let i18nStrings = null;
|
||||||
|
let do_debug = false;
|
||||||
|
let taLog = null;
|
||||||
|
|
||||||
let conversationHistory = [];
|
let conversationHistory = [];
|
||||||
let assistantResponseAccumulator = '';
|
let assistantResponseAccumulator = '';
|
||||||
|
|
@ -37,6 +41,9 @@ self.onmessage = async function(event) {
|
||||||
openai_comp_model = event.data.openai_comp_model;
|
openai_comp_model = event.data.openai_comp_model;
|
||||||
openai_comp_api_key = event.data.openai_comp_api_key;
|
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);
|
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') {
|
} else if (event.data.type === 'chatMessage') {
|
||||||
conversationHistory.push({ role: 'user', content: event.data.message });
|
conversationHistory.push({ role: 'user', content: event.data.message });
|
||||||
|
|
||||||
|
|
@ -52,9 +59,9 @@ self.onmessage = async function(event) {
|
||||||
const errorJSON = await response.json();
|
const errorJSON = await response.json();
|
||||||
errorDetail = JSON.stringify(errorJSON);
|
errorDetail = JSON.stringify(errorJSON);
|
||||||
error_message = errorJSON.error.message;
|
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);
|
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
|
// lots of low-level OpenAI response parsing stuff
|
||||||
chunk += decoder.decode(value);
|
chunk += decoder.decode(value);
|
||||||
// console.log(">>>>>>>>>>>>>> [ThunderAI] chunk: " + JSON.stringify(chunk));
|
taLog.log("chunk: " + JSON.stringify(chunk));
|
||||||
const lines = chunk.split("\n");
|
const lines = chunk.split("\n");
|
||||||
let parsedLines = [];
|
let parsedLines = [];
|
||||||
chunk = chunk.substring(chunk.lastIndexOf('\n') + 1);
|
chunk = chunk.substring(chunk.lastIndexOf('\n') + 1);
|
||||||
// console.log(">>>>>>>>>>>>>> [ThunderAI] last chunk: " + JSON.stringify(chunk));
|
taLog.log("last chunk: " + JSON.stringify(chunk));
|
||||||
try{
|
try{
|
||||||
parsedLines = lines
|
parsedLines = lines
|
||||||
.map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix
|
.map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix
|
||||||
.filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]"
|
.filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]"
|
||||||
// .map((line) => JSON.parse(line)); // Parse the JSON string
|
// .map((line) => JSON.parse(line)); // Parse the JSON string
|
||||||
.map((line) => {
|
.map((line) => {
|
||||||
// console.log(">>>>>>>>>>>>> [ThunderAI] line: " + JSON.stringify(line));
|
taLog.log("line: " + JSON.stringify(line));
|
||||||
return JSON.parse(line);
|
return JSON.parse(line);
|
||||||
});
|
});
|
||||||
}catch(e){
|
}catch(e){
|
||||||
// console.error(">>>>>>>>>>>>> [ThunderAI] error: " + e);
|
taLog.warn("broken chunk: " + e);
|
||||||
console.warn("[ThunderAI | model-worker-openai_comp] broken chunk: " + e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const parsedLine of parsedLines) {
|
for (const parsedLine of parsedLines) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue