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);
} 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);
}

View file

@ -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;
// }
// }
}