Refactor mistral max context

1f27a39f2928230fef84294ef66d553d30ac2116

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +76 -15Showing whitespace changes
public/scripts/openai.js+76 -15
@@ -4166,6 +4166,80 @@ function getMaxContextWindowAI(value) {
41664166}
41674167
41684168/**
4169+ * Get the maximum context size for the Mistral model
4170+ * @param {string} model Model identifier
4171+ * @param {boolean} isUnlocked Whether context limits are unlocked
4172+ * @returns {number} Maximum context size in tokens
4173+ */
4174+function getMistralMaxContext(model, isUnlocked) {
4175+ if (isUnlocked) {
4176+ return unlocked_max;
4177+ }
4178+
4179+ if (Array.isArray(model_list) && model_list.length > 0) {
4180+ const contextLength = model_list.find((record) => record.id === model)?.max_context_length;
4181+ if (contextLength) {
4182+ return contextLength;
4183+ }
4184+ }
4185+
4186+ const contextMap = {
4187+ 'codestral-2411-rc5': 262144,
4188+ 'codestral-2412': 262144,
4189+ 'codestral-2501': 262144,
4190+ 'codestral-latest': 262144,
4191+ 'codestral-mamba-2407': 262144,
4192+ 'codestral-mamba-latest': 262144,
4193+ 'open-codestral-mamba': 262144,
4194+ 'ministral-3b-2410': 131072,
4195+ 'ministral-3b-latest': 131072,
4196+ 'ministral-8b-2410': 131072,
4197+ 'ministral-8b-latest': 131072,
4198+ 'mistral-large-2407': 131072,
4199+ 'mistral-large-2411': 131072,
4200+ 'mistral-large-latest': 131072,
4201+ 'mistral-large-pixtral-2411': 131072,
4202+ 'mistral-tiny-2407': 131072,
4203+ 'mistral-tiny-latest': 131072,
4204+ 'open-mistral-nemo': 131072,
4205+ 'open-mistral-nemo-2407': 131072,
4206+ 'pixtral-12b': 131072,
4207+ 'pixtral-12b-2409': 131072,
4208+ 'pixtral-12b-latest': 131072,
4209+ 'pixtral-large-2411': 131072,
4210+ 'pixtral-large-latest': 131072,
4211+ 'open-mixtral-8x22b': 65536,
4212+ 'open-mixtral-8x22b-2404': 65536,
4213+ 'codestral-2405': 32768,
4214+ 'mistral-embed': 32768,
4215+ 'mistral-large-2402': 32768,
4216+ 'mistral-medium': 32768,
4217+ 'mistral-medium-2312': 32768,
4218+ 'mistral-medium-latest': 32768,
4219+ 'mistral-moderation-2411': 32768,
4220+ 'mistral-moderation-latest': 32768,
4221+ 'mistral-ocr-2503': 32768,
4222+ 'mistral-ocr-latest': 32768,
4223+ 'mistral-saba-2502': 32768,
4224+ 'mistral-saba-latest': 32768,
4225+ 'mistral-small': 32768,
4226+ 'mistral-small-2312': 32768,
4227+ 'mistral-small-2402': 32768,
4228+ 'mistral-small-2409': 32768,
4229+ 'mistral-small-2501': 32768,
4230+ 'mistral-small-2503': 32768,
4231+ 'mistral-small-latest': 32768,
4232+ 'mistral-tiny': 32768,
4233+ 'mistral-tiny-2312': 32768,
4234+ 'open-mistral-7b': 32768,
4235+ 'open-mixtral-8x7b': 32768,
4236+ };
4237+
4238+ // Return context size if model found, otherwise default to 32k
4239+ return Object.entries(contextMap).find(([key]) => model.includes(key))?.[1] || 32768;
4240+}
4241+
4242+/**
41694243 * Get the maximum context size for the Groq model
41704244 * @param {string} model Model identifier
41714245 * @param {boolean} isUnlocked Whether context limits are unlocked
@@ -4428,23 +4502,10 @@ async function onModelChange() {
44284502 }
44294503
44304504 if (oai_settings.chat_completion_source === chat_completion_sources.MISTRALAI) {
4431- if (oai_settings.max_context_unlocked) {
4505+ const maxContext = getMistralMaxContext(oai_settings.mistralai_model, oai_settings.max_context_unlocked);
44324506 $('#openai_max_context').attr('max', unlocked_maxmaxContext);
4433- } else if (["codestral-2411-rc5", "codestral-2412", "codestral-2501", "codestral-latest", "codestral-mamba-2407", "codestral-mamba-latest", "open-codestral-mamba"].includes(oai_settings.mistralai_model)) {
4434- $('#openai_max_context').attr('max', 262144);
4435- } else if (["ministral-3b-2410", "ministral-3b-latest", "ministral-8b-2410", "ministral-8b-latest", "mistral-large-2407", "mistral-large-2411", "mistral-large-latest", "mistral-large-pixtral-2411", "mistral-tiny-2407", "mistral-tiny-latest", "open-mistral-nemo", "open-mistral-nemo-2407", "pixtral-12b", "pixtral-12b-2409", "pixtral-12b-latest", "pixtral-large-2411", "pixtral-large-latest"].includes(oai_settings.mistralai_model)) {
4436- $('#openai_max_context').attr('max', 131072);
4437- } else if (["open-mixtral-8x22b", "open-mixtral-8x22b-2404"].includes(oai_settings.mistralai_model)) {
4438- $('#openai_max_context').attr('max', 65536);
4439- } else if (["codestral-2405", "mistral-embed", "mistral-large-2402", "mistral-medium", "mistral-medium-2312", "mistral-medium-latest", "mistral-moderation-2411", "mistral-moderation-latest", "mistral-ocr-2503", "mistral-ocr-latest", "mistral-saba-2502", "mistral-saba-latest", "mistral-small", "mistral-small-2312", "mistral-small-2402", "mistral-small-2409", "mistral-small-2501", "mistral-small-2503", "mistral-small-latest", "mistral-tiny", "mistral-tiny-2312", "open-mistral-7b", "open-mixtral-8x7b"].includes(oai_settings.mistralai_model)) {
4440- $('#openai_max_context').attr('max', 32768);
4441- } else {
4442- $('#openai_max_context').attr('max', 32768);
4443- }
44444507 oai_settings.openai_max_context = Math.min(oai_settings.openai_max_context, Number($('#openai_max_context').attr('max')));
44454508 $('#openai_max_context').val(oai_settings.openai_max_context).trigger('input');
4446-
4447- //mistral also caps temp at 1.0
44484509 oai_settings.temp_openai = Math.min(claude_max_temp, oai_settings.temp_openai);
44494510 $('#temp_openai').attr('max', claude_max_temp).val(oai_settings.temp_openai).trigger('input');
44504511 }