using taLog

This commit is contained in:
mic 2024-10-03 21:31:50 +02:00
parent 8880c03592
commit 53e6a408ea

View file

@ -21,12 +21,15 @@
*/ */
import { Ollama } from '../js/api/ollama.js'; import { Ollama } from '../js/api/ollama.js';
import { taLogger } from '../js/mzta-logger.js';
let ollama_host = null; let ollama_host = null;
let ollama_model = ''; let ollama_model = '';
let ollama = null; let ollama = null;
let stopStreaming = false; let stopStreaming = false;
let i18nStrings = null; let i18nStrings = null;
let do_debug = false;
let taLog = null;
let conversationHistory = []; let conversationHistory = [];
let assistantResponseAccumulator = ''; let assistantResponseAccumulator = '';
@ -38,7 +41,9 @@ self.onmessage = async function(event) {
ollama_model = event.data.ollama_model; ollama_model = event.data.ollama_model;
//console.log(">>>>>>>>>>> ollama_host: " + ollama_host); //console.log(">>>>>>>>>>> ollama_host: " + ollama_host);
ollama = new Ollama(ollama_host, ollama_model, true); ollama = new Ollama(ollama_host, ollama_model, true);
do_debug = event.data.do_debug;
i18nStrings = event.data.i18nStrings; i18nStrings = event.data.i18nStrings;
taLog = new taLogger('model-worker-ollama', do_debug);
break; // init break; // init
case 'chatMessage': case 'chatMessage':
conversationHistory.push({ role: 'user', content: event.data.message }); conversationHistory.push({ role: 'user', content: event.data.message });
@ -55,7 +60,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; error_message = errorJSON.error;
//console.log(">>>>>>>>>>>>> errorJSON.error.message: " + JSON.stringify(errorJSON.error.message)); taLog.log("errorJSON.error.message: " + JSON.stringify(errorJSON.error.message));
} }
postMessage({ type: 'error', payload: i18nStrings["ollama_api_request_failed"] + ": " + error_message }); postMessage({ type: 'error', payload: i18nStrings["ollama_api_request_failed"] + ": " + error_message });
throw new Error("[ThunderAI] Ollama API request failed: " + response.status + " " + response.statusText + ", Detail: " + errorDetail); throw new Error("[ThunderAI] Ollama API request failed: " + response.status + " " + response.statusText + ", Detail: " + errorDetail);
@ -85,17 +90,22 @@ self.onmessage = async function(event) {
} }
// lots of low-level Ollama response parsing stuff // lots of low-level Ollama response parsing stuff
chunk += decoder.decode(value); chunk += decoder.decode(value);
//console.log(">>>>>>>>>>>>> chunk: " + chunk); taLog.log("chunk: " + 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);
taLog.log("last chunk: " + JSON.stringify(chunk));
try{ try{
parsedLines = lines parsedLines = lines
.map((line) => line.replace(/^chunk: /, "").trim()) // Remove the "chunk: " prefix .map((line) => line.replace(/^chunk: /, "").trim()) // Remove the "chunk: " 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) => {
taLog.log("line: " + JSON.stringify(line));
return JSON.parse(line);
});
}catch(e){ }catch(e){
console.warn("[ThunderAI | model-worker-ollama] broken chunk: " + e); taLog.warn("broken chunk: " + e);
} }
for (const parsedLine of parsedLines) { for (const parsedLine of parsedLines) {