ollama_api: getting models list. see #79

This commit is contained in:
mic 2024-08-20 17:42:04 +02:00
parent 4f7a018f67
commit 969dbb1127
5 changed files with 130 additions and 16 deletions

View file

@ -462,5 +462,9 @@
"Ollama_Models_Error_fetching": { "Ollama_Models_Error_fetching": {
"message": "Error trying to fetch Ollama models", "message": "Error trying to fetch Ollama models",
"description": "" "description": ""
},
"Ollama_Models_Error_NoModels": {
"message": "No models found",
"description": ""
} }
} }

58
js/api/ollama.js Normal file
View file

@ -0,0 +1,58 @@
/*
* ThunderAI [https://micz.it/thunderbird-addon-thunderai/]
* Copyright (C) 2024 Mic (m@micz.it)
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
export class Ollama {
host = '';
model = '';
stream = false;
constructor(host, model, stream = false) {
this.host = host.trim().replace(/\/+$/, "");
this.model = model;
this.stream = stream;
}
fetchModels = async () => {
const response = await fetch(this.host + "/api/tags", {
method: "GET",
headers: {
"Content-Type": "application/json"
},
});
if (!response.ok) {
const errorDetail = await response.text();
let err_msg = "[ThunderAI] Ollama API request failed: " + response.status + " " + response.statusText + ", Detail: " + errorDetail;
console.error(err_msg);
let output = {};
output.ok = false;
output.error = errorDetail;
return output;
}
let output = {};
output.ok = true;
let output_response = await response.json();
output.response = output_response;
//console.log(">>>>>>>>>> output_response: " + JSON.stringify(output_response));
return output;
}
}

View file

@ -102,7 +102,12 @@ tr.conntype_chatgpt_web, tr.conntype_chatgpt_web2{
} }
*/ */
#model_fetch_loading{ #chatgpt_model_fetch_loading{
display: none;
font-style: italic;
}
#ollama_model_fetch_loading{
display: none; display: none;
font-style: italic; font-style: italic;
} }

View file

@ -105,7 +105,7 @@
</label> </label>
</td> </td>
<td> <td>
<button id="btnUpdateChatGPTModels">__MSG_ChatGPT_Models_Fetch__</button> <span id="model_fetch_loading">__MSG_Loading__</span><br> <button id="btnUpdateChatGPTModels">__MSG_ChatGPT_Models_Fetch__</button> <span id="chatgpt_model_fetch_loading">__MSG_Loading__</span><br>
<label> <label>
<select id="chatgpt_model" name="chatgpt_model" class="option-input"> <select id="chatgpt_model" name="chatgpt_model" class="option-input">
</select> </select>
@ -130,7 +130,7 @@
</label> </label>
</td> </td>
<td> <td>
<button id="btnUpdateChatGPTModels">__MSG_Ollama_Models_Fetch__</button> <span id="model_fetch_loading">__MSG_Loading__</span><br> <button id="btnUpdateOllamaModels">__MSG_Ollama_Models_Fetch__</button> <span id="ollama_model_fetch_loading">__MSG_Loading__</span><br>
<label> <label>
<select id="ollama_model" name="ollama_model" class="option-input"> <select id="ollama_model" name="ollama_model" class="option-input">
</select> </select>

View file

@ -20,6 +20,7 @@
import { prefs_default } from './mzta-options-default.js'; import { prefs_default } from './mzta-options-default.js';
import { taLogger } from '../js/mzta-logger.js'; import { taLogger } from '../js/mzta-logger.js';
import { OpenAI } from '../js/api/openai.js'; import { OpenAI } from '../js/api/openai.js';
import { Ollama } from '../js/api/ollama.js';
let taLog = new taLogger("mzta-options",true); let taLog = new taLogger("mzta-options",true);
@ -125,17 +126,17 @@ function showConnectionOptions() {
function warnAPIKeyEmpty() { function warnAPIKeyEmpty() {
let apiKeyInput = document.getElementById('chatgpt_api_key'); let apiKeyInput = document.getElementById('chatgpt_api_key');
let btnFetchModels = document.getElementById('btnUpdateChatGPTModels'); let btnFetchChatGPTModels = document.getElementById('btnUpdateChatGPTModels');
let modelChatGPT = document.getElementById('chatgpt_model'); let modelChatGPT = document.getElementById('chatgpt_model');
if(apiKeyInput.value === ''){ if(apiKeyInput.value === ''){
apiKeyInput.style.border = '2px solid red'; apiKeyInput.style.border = '2px solid red';
btnFetchModels.disabled = true; btnFetchChatGPTModels.disabled = true;
modelChatGPT.disabled = true; modelChatGPT.disabled = true;
modelChatGPT.selectedIndex = -1; modelChatGPT.selectedIndex = -1;
modelChatGPT.style.border = 'none'; modelChatGPT.style.border = 'none';
}else{ }else{
apiKeyInput.style.border = 'none'; apiKeyInput.style.border = 'none';
btnFetchModels.disabled = false; btnFetchChatGPTModels.disabled = false;
modelChatGPT.disabled = false; modelChatGPT.disabled = false;
if((modelChatGPT.selectedIndex === -1)||(modelChatGPT.value === '')){ if((modelChatGPT.selectedIndex === -1)||(modelChatGPT.value === '')){
modelChatGPT.style.border = '2px solid red'; modelChatGPT.style.border = '2px solid red';
@ -172,22 +173,23 @@ document.addEventListener('DOMContentLoaded', async () => {
conntype_select.addEventListener("change", warnAPIKeyEmpty); conntype_select.addEventListener("change", warnAPIKeyEmpty);
document.getElementById("chatgpt_api_key").addEventListener("change", warnAPIKeyEmpty); document.getElementById("chatgpt_api_key").addEventListener("change", warnAPIKeyEmpty);
let prefs = await browser.storage.sync.get({chatgpt_api_key: '', chatgpt_model: ''}); let prefs = await browser.storage.sync.get({chatgpt_model: '', ollama_model: ''});
let select_chatgpt_model = document.getElementById('chatgpt_model');
const option = document.createElement('option'); // OpenAI API ChatGPT model fetching
option.value = prefs.chatgpt_model; let select_chatgpt_model = document.getElementById('chatgpt_model');
option.text = prefs.chatgpt_model; const chatgpt_option = document.createElement('option');
select_chatgpt_model.appendChild(option); chatgpt_option.value = prefs.chatgpt_model;
chatgpt_option.text = prefs.chatgpt_model;
select_chatgpt_model.appendChild(chatgpt_option);
select_chatgpt_model.addEventListener("change", warnAPIKeyEmpty); select_chatgpt_model.addEventListener("change", warnAPIKeyEmpty);
document.getElementById('btnUpdateChatGPTModels').addEventListener('click', async () => { document.getElementById('btnUpdateChatGPTModels').addEventListener('click', async () => {
document.getElementById('model_fetch_loading').style.display = 'inline'; document.getElementById('chatgpt_model_fetch_loading').style.display = 'inline';
let openai = new OpenAI(document.getElementById("chatgpt_api_key").value, true); let openai = new OpenAI(document.getElementById("chatgpt_api_key").value, '', true);
openai.fetchModels().then((data) => { openai.fetchModels().then((data) => {
if(!data.ok){ if(!data.ok){
let errorDetail = JSON.parse(data.error); let errorDetail = JSON.parse(data.error);
document.getElementById('model_fetch_loading').style.display = 'none'; document.getElementById('chatgpt_model_fetch_loading').style.display = 'none';
console.error("[ThunderAI] " + browser.i18n.getMessage("ChatGPT_Models_Error_fetching")); console.error("[ThunderAI] " + browser.i18n.getMessage("ChatGPT_Models_Error_fetching"));
alert(browser.i18n.getMessage("ChatGPT_Models_Error_fetching")+": " + errorDetail.error.message); alert(browser.i18n.getMessage("ChatGPT_Models_Error_fetching")+": " + errorDetail.error.message);
return; return;
@ -201,9 +203,54 @@ document.addEventListener('DOMContentLoaded', async () => {
select_chatgpt_model.appendChild(option); select_chatgpt_model.appendChild(option);
} }
}); });
document.getElementById('model_fetch_loading').style.display = 'none'; document.getElementById('chatgpt_model_fetch_loading').style.display = 'none';
}) })
warnAPIKeyEmpty();
});
// Ollama API Model fetching
let select_ollama_model = document.getElementById('ollama_model');
const ollama_option = document.createElement('option');
ollama_option.value = prefs.ollama_model;
ollama_option.text = prefs.ollama_model;
select_ollama_model.appendChild(ollama_option);
// select_ollama_model.addEventListener("change", warnAPIKeyEmpty);
document.getElementById('btnUpdateOllamaModels').addEventListener('click', async () => {
document.getElementById('ollama_model_fetch_loading').style.display = 'inline';
let ollama = new Ollama(document.getElementById("ollama_host").value, true);
let data = await ollama.fetchModels();
if(!data){
document.getElementById('ollama_model_fetch_loading').style.display = 'none';
console.error("[ThunderAI] " + browser.i18n.getMessage("Ollama_Models_Error_fetching"));
alert(browser.i18n.getMessage("Ollama_Models_Error_fetching"));
return;
}
if(!data.ok){
let errorDetail = JSON.parse(data.error);
document.getElementById('ollama_model_fetch_loading').style.display = 'none';
console.error("[ThunderAI] " + browser.i18n.getMessage("Ollama_Models_Error_fetching"));
alert(browser.i18n.getMessage("Ollama_Models_Error_fetching")+": " + errorDetail.error.message);
return;
}
if(data.response.models.length == 0){
document.getElementById('ollama_model_fetch_loading').style.display = 'none';
console.error("[ThunderAI] " + browser.i18n.getMessage("Ollama_Models_Error_fetching"));
alert(browser.i18n.getMessage("Ollama_Models_Error_fetching")+": " + browser.i18n.getMessage("Ollama_Models_Error_NoModels"));
return;
}
taLog.log("Ollama models: " + JSON.stringify(data));
data.response.models.forEach(model => {
if (!Array.from(select_ollama_model.options).some(option => option.value === model.model)) {
const option = document.createElement('option');
option.value = model.model;
option.text = model.name + " (" + model.model + ")";
select_ollama_model.appendChild(option);
}
});
document.getElementById('ollama_model_fetch_loading').style.display = 'none';
warnAPIKeyEmpty(); warnAPIKeyEmpty();
}); });
}, { once: true }); }, { once: true });