ollama api is working. see #79

This commit is contained in:
mic 2024-08-20 22:51:48 +02:00
parent e2aa598f01
commit 4db9f0c8a5
2 changed files with 58 additions and 47 deletions

View file

@ -37,7 +37,7 @@ self.onmessage = async function(event) {
ollama = new Ollama(ollama_host, ollama_model, true); ollama = new Ollama(ollama_host, ollama_model, true);
} 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 });
//console.log(">>>>>>>>>>> conversationHistory: " + JSON.stringify(conversationHistory));
const response = await ollama.fetchResponse(conversationHistory); //4096); const response = await ollama.fetchResponse(conversationHistory); //4096);
postMessage({ type: 'messageSent' }); postMessage({ type: 'messageSent' });
@ -68,7 +68,8 @@ self.onmessage = async function(event) {
break; break;
} }
// lots of low-level Ollama response parsing stuff // 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 lines = chunk.split("\n");
const parsedLines = lines const parsedLines = lines
.map((line) => line.replace(/^chunk: /, "").trim()) // Remove the "chunk: " prefix .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 .map((line) => JSON.parse(line)); // Parse the JSON string
for (const parsedLine of parsedLines) { for (const parsedLine of parsedLines) {
const { response } = parsedLine; const { message } = parsedLine;
const { content } = message;
// Update the UI with the new content // Update the UI with the new content
if (response) { if (content) {
assistantResponseAccumulator += response; assistantResponseAccumulator += content;
postMessage({ type: 'newToken', payload: { token: response } }); 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);
}

View file

@ -57,27 +57,55 @@ export class Ollama {
} }
fetchResponse = async (messages) => { fetchResponse = async (messages) => {
try { try {
const response = await fetch(this.host + "/api/generate", { //console.log(">>>>>>>>>> messages: " +JSON.stringify(messages));
method: "POST", const response = await fetch(this.host + "/api/chat", {
headers: { method: "POST",
"Content-Type": "application/json", headers: {
}, "Content-Type": "application/json",
body: JSON.stringify({ },
model: this.model, body: JSON.stringify({
prompt: messages.join(' '), model: this.model,
stream: this.stream, messages: messages,
}), stream: this.stream,
}); }),
return response; });
}catch (error) { return response;
console.error("[ThunderAI] Ollama API request failed: " + error); }catch (error) {
let output = {}; console.error("[ThunderAI] Ollama API request failed: " + error);
output.is_exception = true; let output = {};
output.ok = false; output.is_exception = true;
output.error = "Ollama API request failed: " + error; output.ok = false;
return output; 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;
// }
// }
} }