From 19565ec7c56f53d0d370ee90ff0ca31d8d933c68 Mon Sep 17 00:00:00 2001 From: mic Date: Tue, 20 May 2025 22:05:00 +0200 Subject: [PATCH] max_tokens added to anthropic. see #349 --- _locales/en/messages.json | 10 +++++++++- js/api/anthropic.js | 16 ++++++---------- options/mzta-options-default.js | 1 + options/mzta-options.html | 14 +++++++++----- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 51330cbf..b35be7ba 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1466,5 +1466,13 @@ "Anthropic_Version_Info": { "message": "REQUIRED. Do not change this value unless you know what you're doing. More info at: ", "description": "" - } + }, + "prefs_OptionText_anthropic_max_tokens": { + "message": "Anthropic Max Tokens", + "description": "" + }, + "prefs_OptionText_anthropic_max_tokens_Info": { + "message": "The maximum number of tokens to generate in the completion. The token count of your prompt plus max_tokens cannot exceed the model's context length.", + "description": "" + } } \ No newline at end of file diff --git a/js/api/anthropic.js b/js/api/anthropic.js index 30fe8663..ffb9c9e2 100644 --- a/js/api/anthropic.js +++ b/js/api/anthropic.js @@ -24,14 +24,14 @@ export class Anthropic { apiKey = ''; version = ''; model = ''; - developer_messages = ''; + max_tokens = 4096; stream = false; - constructor(apiKey, version, model, developer_messages, stream) { + constructor(apiKey, version, model, max_tokens, stream) { this.apiKey = apiKey; this.version = version; this.model = model; - this.developer_messages = developer_messages; + this.max_tokens = max_tokens > 0 ? max_tokens : 4096; this.stream = stream; } @@ -73,11 +73,7 @@ export class Anthropic { } } - fetchResponse = async (messages, maxTokens = 0) => { - - if(this.developer_messages !== ''){ - messages.push({role: "developer", content: [{"type": "text", "text": this.developer_messages}]}); - } + fetchResponse = async (messages) => { // console.log(">>>>>>>>>>> Anthropic API request: " + JSON.stringify(messages)); @@ -90,10 +86,10 @@ export class Anthropic { "anthropic-version": this.version, }, body: JSON.stringify({ - model: this.model, + model: this.model, + max_tokens: this.max_tokens, messages: messages, stream: this.stream, - ...(maxTokens > 0 ? { 'max_tokens': parseInt(maxTokens) } : {}) }), }); return response; diff --git a/options/mzta-options-default.js b/options/mzta-options-default.js index a7aa5e89..12ca7726 100644 --- a/options/mzta-options-default.js +++ b/options/mzta-options-default.js @@ -45,6 +45,7 @@ export const prefs_default = { anthropic_api_key: '', anthropic_model: '', anthropic_version: '2023-06-01', + anthropic_max_tokens: 4096, dynamic_menu_force_enter: false, dynamic_menu_order_alphabet: true, placeholders_use_default_value: false, diff --git a/options/mzta-options.html b/options/mzta-options.html index 187fd401..7c3e85f9 100644 --- a/options/mzta-options.html +++ b/options/mzta-options.html @@ -425,11 +425,15 @@ - - - - - + + __MSG_prefs_OptionText_anthropic_max_tokens__ + + + + __MSG_prefs_OptionText_max_prompt_length__