forked from ollama-ui/ollama-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
97 lines (84 loc) · 2.36 KB
/
api.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var rebuildRules = undefined;
if (typeof chrome !== "undefined" && chrome.runtime && chrome.runtime.id) {
rebuildRules = async function (domain) {
const domains = [domain];
/** @type {chrome.declarativeNetRequest.Rule[]} */
const rules = [{
id: 1,
condition: {
requestDomains: domains
},
action: {
type: 'modifyHeaders',
requestHeaders: [{
header: 'origin',
operation: 'set',
value: `http://${domain}`,
}],
},
}];
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: rules.map(r => r.id),
addRules: rules,
});
}
}
var ollama_host = localStorage.getItem("host-address");
if (!ollama_host){
ollama_host = 'http://localhost:11434'
} else {
document.getElementById("host-address").value = ollama_host;
}
if (rebuildRules){
rebuildRules(ollama_host);
}
function setHostAddress(){
ollama_host = document.getElementById("host-address").value;
localStorage.setItem("host-address", ollama_host);
populateModels();
if (rebuildRules){
rebuildRules(ollama_host);
}
}
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);
}
}