From 17a0aeb632fe834da64b7b3b4357bbd2a1dd656a Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 28 Dec 2025 21:12:08 +0100 Subject: [PATCH] temperature added to Ollama API. see #574 --- _locales/en/messages.json | 8 ++++++++ api_webchat/controller.js | 5 +++++ js/api/ollama.js | 30 ++++-------------------------- js/workers/model-worker-ollama.js | 6 +++++- options/mzta-options-default.js | 1 + pages/_lib/connection-ui.js | 13 +++++++++++++ 6 files changed, 36 insertions(+), 27 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index eb6ab97a..8ba8a04c 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1585,6 +1585,14 @@ "message": "What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.", "description": "" }, + "ollama_temperature": { + "message": "Temperature", + "description": "" + }, + "ollama_temperature_Info": { + "message": "The temperature of the model. Increasing the temperature will make the model answer more creatively. The default value is 0.8. It is recommended to use values between 0 and 1.", + "description": "" + }, "prefs_ollama_think": { "message": "Enable thinking", "description": "" diff --git a/api_webchat/controller.js b/api_webchat/controller.js index e446c241..5f5cdfca 100644 --- a/api_webchat/controller.js +++ b/api_webchat/controller.js @@ -170,6 +170,7 @@ switch (llm) { ollama_host: prefs_default.ollama_host, ollama_model: prefs_default.ollama_model, ollama_num_ctx: prefs_default.ollama_num_ctx, + ollama_temperature: prefs_default.ollama_temperature, ollama_think: prefs_default.ollama_think, do_debug: prefs_default.do_debug, }); @@ -183,6 +184,7 @@ switch (llm) { ollama_host: prefs_api.ollama_host, ollama_model: prefs_api.ollama_model, ollama_num_ctx: prefs_api.ollama_num_ctx, + ollama_temperature: prefs_api.ollama_temperature, ollama_think: prefs_api.ollama_think, do_debug: prefs_api.do_debug, i18nStrings: i18nStrings @@ -194,6 +196,9 @@ switch (llm) { let additional_text_elements = []; additional_text_elements.push({label: browser.i18n.getMessage("prompt_string"), value: '[' + prompt_id + '] ' + decodeURIComponent(prompt_name)}); additional_text_elements.push({label: browser.i18n.getMessage("prefs_ollama_think"), value: (prefs_api.ollama_think ? 'Yes' : 'No')}); + if(prefs_api.ollama_temperature && prefs_api.ollama_temperature.length > 0){ + additional_text_elements.push({label: browser.i18n.getMessage("prefs_ollama_temperature"), value: prefs_api.ollama_temperature}); + } if(prefs_api.ollama_num_ctx > 0){ additional_text_elements.push({label: browser.i18n.getMessage("prefs_ollama_num_ctx"), value: prefs_api.ollama_num_ctx}); } diff --git a/js/api/ollama.js b/js/api/ollama.js index 9a4be836..a77653f1 100644 --- a/js/api/ollama.js +++ b/js/api/ollama.js @@ -22,6 +22,7 @@ export class Ollama { model = ''; stream = false; num_ctx = 0; + temperature = ''; think = false; constructor({ @@ -29,12 +30,14 @@ export class Ollama { model = '', stream = false, num_ctx = 0, + temperature = '', think = false, } = {}) { this.host = (host || '').trim().replace(/\/+$/, ""); this.model = model; this.stream = stream; this.num_ctx = num_ctx; + this.temperature = temperature; this.think = think; } @@ -90,6 +93,7 @@ export class Ollama { stream: this.stream, think: this.think, ...(this.num_ctx > 0 ? { options: { num_ctx: parseInt(this.num_ctx) } } : {}), + ...(parseFloat(this.temperature) != NaN ? { options: { temperature: parseFloat(this.temperature) } } : {}), }), }); return response; @@ -103,30 +107,4 @@ export class Ollama { } } - - // 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; - // } - // } } diff --git a/js/workers/model-worker-ollama.js b/js/workers/model-worker-ollama.js index d099cf2a..afa10062 100644 --- a/js/workers/model-worker-ollama.js +++ b/js/workers/model-worker-ollama.js @@ -41,12 +41,16 @@ self.onmessage = async function(event) { ollama_host = event.data.ollama_host; ollama_model = event.data.ollama_model; ollama_num_ctx = event.data.ollama_num_ctx; + ollama_temperature = event.data.ollama_temperature; + ollama_think = event.data.ollama_think; //console.log(">>>>>>>>>>> ollama_host: " + ollama_host); ollama = new Ollama({ host: ollama_host, model: ollama_model, stream: true, - num_ctx: ollama_num_ctx + num_ctx: ollama_num_ctx, + temperature: ollama_temperature, + think: ollama_think }); do_debug = event.data.do_debug; i18nStrings = event.data.i18nStrings; diff --git a/options/mzta-options-default.js b/options/mzta-options-default.js index c0b771f2..c524c89d 100644 --- a/options/mzta-options-default.js +++ b/options/mzta-options-default.js @@ -37,6 +37,7 @@ export const prefs_default = { ollama_host: '', ollama_model: '', ollama_num_ctx: 0, + ollama_temperature: '', ollama_think: false, openai_comp_host: '', // For OpenAI Compatible API as LM-Studio openai_comp_model: '', diff --git a/pages/_lib/connection-ui.js b/pages/_lib/connection-ui.js index 25367f5b..1b9c8e93 100644 --- a/pages/_lib/connection-ui.js +++ b/pages/_lib/connection-ui.js @@ -292,6 +292,19 @@ export async function injectConnectionUI({ + + + + + + + +