-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
71 lines (61 loc) · 1.92 KB
/
Copy pathapi.js
File metadata and controls
71 lines (61 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var ollama_host = localStorage.getItem("host-address");
if (!ollama_host){
ollama_host = 'http://localhost:11434'
} else {
document.getElementById("host-address").value = ollama_host;
}
const ollama_system_prompt = localStorage.getItem("system-prompt");
if (ollama_system_prompt){
document.getElementById("system-prompt").value = ollama_system_prompt;
}
function setHostAddress(){
ollama_host = document.getElementById("host-address").value;
localStorage.setItem("host-address", ollama_host);
populateModels();
}
function setSystemPrompt(){
const systemPrompt = document.getElementById("system-prompt").value;
localStorage.setItem("system-prompt", systemPrompt);
}
async function getModels(){
const response = await fetch(`${ollama_host}/api/tags`);
const data = await response.json();
return data;
}
// Function to send a POST request to the API
function postRequest(data, signal) {
const URL = `${ollama_host}/api/generate`;
return fetch(URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data),
signal: signal
});
}
// Function to stream the response from the server
async function getResponse(response, callback) {
const reader = response.body.getReader();
let partialLine = '';
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
// Decode the received value and split by lines
const textChunk = new TextDecoder().decode(value);
const lines = (partialLine + textChunk).split('\n');
partialLine = lines.pop(); // The last line might be incomplete
for (const line of lines) {
if (line.trim() === '') continue;
const parsedResponse = JSON.parse(line);
callback(parsedResponse); // Process each response word
}
}
// Handle any remaining line
if (partialLine.trim() !== '') {
const parsedResponse = JSON.parse(partialLine);
callback(parsedResponse);
}
}