using taLogger in openai worker
This commit is contained in:
parent
317ed04077
commit
d79f3f880c
1 changed files with 10 additions and 7 deletions
|
|
@ -21,11 +21,14 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { OpenAI } from '../js/api/openai.js';
|
import { OpenAI } from '../js/api/openai.js';
|
||||||
|
import { taLogger } from '../js/mzta-logger.js';
|
||||||
|
|
||||||
let chatgpt_api_key = null;
|
let chatgpt_api_key = null;
|
||||||
let chatgpt_model = '';
|
let chatgpt_model = '';
|
||||||
let openai = null;
|
let openai = null;
|
||||||
let stopStreaming = false;
|
let stopStreaming = false;
|
||||||
|
let do_debug = false;
|
||||||
|
let taLog = null;
|
||||||
|
|
||||||
let conversationHistory = [];
|
let conversationHistory = [];
|
||||||
let assistantResponseAccumulator = '';
|
let assistantResponseAccumulator = '';
|
||||||
|
|
@ -34,8 +37,9 @@ self.onmessage = async function(event) {
|
||||||
if (event.data.type === 'init') {
|
if (event.data.type === 'init') {
|
||||||
chatgpt_api_key = event.data.chatgpt_api_key;
|
chatgpt_api_key = event.data.chatgpt_api_key;
|
||||||
chatgpt_model = event.data.chatgpt_model;
|
chatgpt_model = event.data.chatgpt_model;
|
||||||
//console.log(">>>>>>>>>>> chatgpt_api_key: " + chatgpt_api_key);
|
|
||||||
openai = new OpenAI(chatgpt_api_key, chatgpt_model, true);
|
openai = new OpenAI(chatgpt_api_key, chatgpt_model, true);
|
||||||
|
do_debug = event.data.do_debug;
|
||||||
|
taLog = new taLogger('model-worker-openai', 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 });
|
||||||
|
|
||||||
|
|
@ -51,7 +55,7 @@ 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.error("errorJSON.error.message: " + JSON.stringify(errorJSON.error.message));
|
||||||
}
|
}
|
||||||
postMessage({ type: 'error', payload: error_message });
|
postMessage({ type: 'error', payload: error_message });
|
||||||
throw new Error("[ThunderAI] OpenAI API request failed: " + response.status + " " + response.statusText + ", Detail: " + errorDetail);
|
throw new Error("[ThunderAI] OpenAI API request failed: " + response.status + " " + response.statusText + ", Detail: " + errorDetail);
|
||||||
|
|
@ -79,23 +83,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.log(">>>>>>>>>>>>>> [ThunderAI] last chunk: " + JSON.stringify(chunk));
|
taLog.warn("broken chunk: " + e);
|
||||||
console.warn("[ThunderAI | model-worker-openai] broken chunk: " + e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const parsedLine of parsedLines) {
|
for (const parsedLine of parsedLines) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue