From 4db9f0c8a5d1344ec88861f998ae7db4b5193eb8 Mon Sep 17 00:00:00 2001 From: mic Date: Tue, 20 Aug 2024 22:51:48 +0200 Subject: [PATCH] ollama api is working. see #79 --- api_webchat/model-worker-ollama.js | 33 ++++---------- js/api/ollama.js | 72 +++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 47 deletions(-) diff --git a/api_webchat/model-worker-ollama.js b/api_webchat/model-worker-ollama.js index 56e35006..2c11612b 100644 --- a/api_webchat/model-worker-ollama.js +++ b/api_webchat/model-worker-ollama.js @@ -37,7 +37,7 @@ self.onmessage = async function(event) { ollama = new Ollama(ollama_host, ollama_model, true); } else if (event.data.type === 'chatMessage') { conversationHistory.push({ role: 'user', content: event.data.message }); - + //console.log(">>>>>>>>>>> conversationHistory: " + JSON.stringify(conversationHistory)); const response = await ollama.fetchResponse(conversationHistory); //4096); postMessage({ type: 'messageSent' }); @@ -68,7 +68,8 @@ self.onmessage = async function(event) { break; } // lots of low-level Ollama response parsing stuff - const chunk = decoder.decode(value); console.log(">>>>>>>>>>>>> chunk: " + chunk); + const chunk = decoder.decode(value); + //console.log(">>>>>>>>>>>>> chunk: " + chunk); const lines = chunk.split("\n"); const parsedLines = lines .map((line) => line.replace(/^chunk: /, "").trim()) // Remove the "chunk: " prefix @@ -76,32 +77,14 @@ self.onmessage = async function(event) { .map((line) => JSON.parse(line)); // Parse the JSON string for (const parsedLine of parsedLines) { - const { response } = parsedLine; + const { message } = parsedLine; + const { content } = message; // Update the UI with the new content - if (response) { - assistantResponseAccumulator += response; - postMessage({ type: 'newToken', payload: { token: response } }); + if (content) { + assistantResponseAccumulator += content; + postMessage({ type: 'newToken', payload: { token: content } }); } } } } }; - -function parsePartialResponse(responseText) { - // Logica di parsing dei dati parziali - // Potresti voler spezzare i chunk in linee o altri delimitatori - const lines = responseText.split("\n"); - - // Filtro le linee valide e faccio il parsing di ogni linea JSON - return lines - .filter(line => line.trim().length > 0) - .map(line => { - try { - return JSON.parse(line); - } catch (e) { - console.warn('Error parsing line:', line); - return null; - } - }) - .filter(parsed => parsed !== null); -} \ No newline at end of file diff --git a/js/api/ollama.js b/js/api/ollama.js index 33733308..2ad59032 100644 --- a/js/api/ollama.js +++ b/js/api/ollama.js @@ -57,27 +57,55 @@ export class Ollama { } - fetchResponse = async (messages) => { - try { - const response = await fetch(this.host + "/api/generate", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - model: this.model, - prompt: messages.join(' '), - stream: this.stream, - }), - }); - return response; - }catch (error) { - console.error("[ThunderAI] Ollama API request failed: " + error); - let output = {}; - output.is_exception = true; - output.ok = false; - output.error = "Ollama API request failed: " + error; - return output; + fetchResponse = async (messages) => { + try { + //console.log(">>>>>>>>>> messages: " +JSON.stringify(messages)); + const response = await fetch(this.host + "/api/chat", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + model: this.model, + messages: messages, + stream: this.stream, + }), + }); + return response; + }catch (error) { + console.error("[ThunderAI] Ollama API request failed: " + error); + let output = {}; + output.is_exception = true; + output.ok = false; + output.error = "Ollama API request failed: " + error; + return output; + } } - } + + + // fetchResponse = async (messages) => { + // try { + // let sending = messages.join(' '); + // console.log(">>>>>>>>>> sending: " + sending); + // const response = await fetch(this.host + "/api/generate", { + // method: "POST", + // headers: { + // "Content-Type": "application/json", + // }, + // body: JSON.stringify({ + // model: this.model, + // prompt: sending, + // stream: this.stream, + // }), + // }); + // return response; + // }catch (error) { + // console.error("[ThunderAI] Ollama API request failed: " + error); + // let output = {}; + // output.is_exception = true; + // output.ok = false; + // output.error = "Ollama API request failed: " + error; + // return output; + // } + // } } \ No newline at end of file