From c2d99076b2ccc8ffd712f844d099346e4140bf52 Mon Sep 17 00:00:00 2001 From: mic Date: Sat, 27 Dec 2025 22:21:20 +0100 Subject: [PATCH] temperature added to the Claude API. see #573 --- _locales/en/messages.json | 14 +++++++++++++- api_webchat/controller.js | 24 ++++++++++++++++++------ js/api/anthropic.js | 22 +++++++++++++++------- options/mzta-options-default.js | 1 + pages/_lib/connection-ui.js | 13 +++++++++++++ 5 files changed, 60 insertions(+), 14 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 40553e1a..4146fce8 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -59,6 +59,10 @@ "message": "More info", "description": "" }, + "prompt_string":{ + "message": "Prompt", + "description": "" + }, "customPrompts_managePrompts_info_default": { "message": "The default prompts are not editable. You can disable them, then copy the prompt text and paste it into a new one to create a modified version.", "description": "" @@ -1496,7 +1500,7 @@ "description": "" }, "prefs_OptionText_anthropic_max_tokens": { - "message": "Claude Max Tokens", + "message": "Max Tokens", "description": "" }, "prefs_OptionText_anthropic_max_tokens_Info": { @@ -1709,6 +1713,14 @@ "message": "You can improve Claude performance by using a System Prompt to give it a role. This technique, known as role prompting, is the most powerful way to use system prompts with Claude. The right role can turn Claude from a general assistant into your virtual domain expert.", "description": "" }, + "prefs_anthropic_temperature": { + "message": "Temperature", + "description": "" + }, + "prefs_anthropic_temperature_Info": { + "message": "Amount of randomness injected into the response. Defaults to 1.0. Ranges from 0.0 to 1.0. Use temperature closer to 0.0 for analytical / multiple choice, and closer to 1.0 for creative and generative tasks. Note that even with temperature of 0.0, the results will not be fully deterministic.", + "description": "" + }, "Optional_Permission_Denied_Model_Fetching": { "message": "You have denied the optional permission needed to fetch models for this integration.", "description": "" diff --git a/api_webchat/controller.js b/api_webchat/controller.js index 04ef964c..4d59c1c5 100644 --- a/api_webchat/controller.js +++ b/api_webchat/controller.js @@ -102,7 +102,7 @@ switch (llm) { if(prefs_api.chatgpt_developer_messages && prefs_api.chatgpt_developer_messages.length > 0) { additional_text_elements.push({label: browser.i18n.getMessage("ChatGPT_Developer_Messages"), value: prefs_api.chatgpt_developer_messages}); } - additional_text_elements.push({label: "Prompt", value: '[' + prompt_id + '] ' + decodeURIComponent(prompt_name)}); + additional_text_elements.push({label: browser.i18n.getMessage("prompt_string"), value: '[' + prompt_id + '] ' + decodeURIComponent(prompt_name)}); messagesArea.appendUserMessage(getAPIsInitMessageString({ api_string: "ChatGPT API", model_string: prefs_api.chatgpt_model, @@ -138,7 +138,7 @@ switch (llm) { if(prefs_api.google_gemini_thinking_budget.length > 0){ additional_text_elements.push({label: browser.i18n.getMessage("prefs_google_gemini_thinking_budget"), value: prefs_api.google_gemini_thinking_budget}); } - additional_text_elements.push({label: "Prompt", value: '[' + prompt_id + '] ' + decodeURIComponent(prompt_name)}); + additional_text_elements.push({label: browser.i18n.getMessage("prompt_string"), value: '[' + prompt_id + '] ' + decodeURIComponent(prompt_name)}); worker.postMessage({ type: 'init', google_gemini_api_key: prefs_api.google_gemini_api_key, @@ -186,7 +186,7 @@ switch (llm) { window_id: (await browser.windows.getCurrent()).id }); let additional_text_elements = []; - additional_text_elements.push({label: "Prompt", value: '[' + prompt_id + '] ' + decodeURIComponent(prompt_name)}); + additional_text_elements.push({label: browser.i18n.getMessage("prompt_string"), value: '[' + prompt_id + '] ' + decodeURIComponent(prompt_name)}); messagesArea.appendUserMessage(getAPIsInitMessageString({ api_string: "Ollama API", model_string: prefs_api.ollama_model, @@ -219,7 +219,7 @@ switch (llm) { i18nStrings: i18nStrings, }); let additional_text_elements = []; - additional_text_elements.push({label: "Prompt", value: '[' + prompt_id + '] ' + decodeURIComponent(prompt_name)}); + additional_text_elements.push({label: browser.i18n.getMessage("prompt_string"), value: '[' + prompt_id + '] ' + decodeURIComponent(prompt_name)}); messagesArea.appendUserMessage(getAPIsInitMessageString({ api_string: "OpenAI Compatible API", model_string: prefs_api.openai_comp_model, @@ -237,6 +237,7 @@ switch (llm) { anthropic_api_key: prefs_default.anthropic_api_key, anthropic_model: prefs_default.anthropic_model, anthropic_system_prompt: prefs_default.anthropic_system_prompt, + anthropic_temperature: prefs_default.anthropic_temperature, anthropic_version: prefs_default.anthropic_version, anthropic_max_tokens: prefs_default.anthropic_max_tokens, do_debug: prefs_default.do_debug, @@ -257,8 +258,19 @@ switch (llm) { i18nStrings: i18nStrings, }); let additional_text_elements = []; - additional_text_elements.push({label: "Prompt", value: '[' + prompt_id + '] ' + decodeURIComponent(prompt_name)}); - additional_text_elements.push({label: "System Prompt", value: prefs_api.anthropic_system_prompt}); + additional_text_elements.push({label: browser.i18n.getMessage("prompt_string"), value: '[' + prompt_id + '] ' + decodeURIComponent(prompt_name)}); + if(prefs_api.anthropic_system_prompt && prefs_api.anthropic_system_prompt.length > 0){ + additional_text_elements.push({label: browser.i18n.getMessage("Anthropic_System_Prompt"), value: prefs_api.anthropic_system_prompt}); + } + if(prefs_api.anthropic_version && prefs_api.anthropic_version.length > 0){ + additional_text_elements.push({label: browser.i18n.getMessage("_api_connecting_version"), value: prefs_api.anthropic_version}); + } + if(prefs_api.anthropic_max_tokens > 0){ + additional_text_elements.push({label: browser.i18n.getMessage("prefs_OptionText_anthropic_max_tokens"), value: prefs_api.anthropic_max_tokens}); + } + if(prefs_api.anthropic_temperature && prefs_api.anthropic_temperature.length > 0){ + additional_text_elements.push({label: browser.i18n.getMessage("prefs_anthropic_temperature"), value: prefs_api.anthropic_temperature}); + } messagesArea.appendUserMessage(getAPIsInitMessageString({ api_string: "Claude API", model_string: prefs_api.anthropic_model, diff --git a/js/api/anthropic.js b/js/api/anthropic.js index e1bb27a5..4c1dee50 100644 --- a/js/api/anthropic.js +++ b/js/api/anthropic.js @@ -25,6 +25,7 @@ export class Anthropic { version = ''; model = ''; system_prompt = ''; + temperature = ''; max_tokens = 4096; stream = false; @@ -33,6 +34,7 @@ export class Anthropic { version = '', model = '', system_prompt = '', + temperature = '', max_tokens = 4096, stream = false, } = {}) { @@ -40,6 +42,7 @@ export class Anthropic { this.version = version; this.model = model; this.system_prompt = system_prompt; + this.temperature = temperature; this.max_tokens = max_tokens > 0 ? max_tokens : 4096; this.stream = stream; } @@ -87,6 +90,17 @@ export class Anthropic { // console.log(">>>>>>>>>>> Anthropic API request: " + JSON.stringify(messages)); try { + + let claude_body = { + model: this.model, + max_tokens: this.max_tokens, + system: this.system_prompt, + messages: messages, + stream: this.stream, + }; + + if(this.temperature != '') claude_body.temperature = this.temperature; + const response = await fetch("https://api.anthropic.com/v1/messages", { method: "POST", headers: { @@ -95,13 +109,7 @@ export class Anthropic { "anthropic-version": this.version, "anthropic-dangerous-direct-browser-access": "true", }, - body: JSON.stringify({ - model: this.model, - max_tokens: this.max_tokens, - system: this.system_prompt, - messages: messages, - stream: this.stream, - }), + body: JSON.stringify(claude_body), }); return response; }catch (error) { diff --git a/options/mzta-options-default.js b/options/mzta-options-default.js index a10a5fea..a2dddb95 100644 --- a/options/mzta-options-default.js +++ b/options/mzta-options-default.js @@ -52,6 +52,7 @@ export const prefs_default = { anthropic_version: '2023-06-01', anthropic_max_tokens: 4096, anthropic_system_prompt: '', + anthropic_temperature: '', dynamic_menu_force_enter: false, dynamic_menu_order_alphabet: true, placeholders_use_default_value: false, diff --git a/pages/_lib/connection-ui.js b/pages/_lib/connection-ui.js index 38e41ed3..de13219d 100644 --- a/pages/_lib/connection-ui.js +++ b/pages/_lib/connection-ui.js @@ -410,6 +410,19 @@ export async function injectConnectionUI({ + + + + + + + +