From 7f08376f0c06e1a7fba983a2fa93deb8dfbe7cb0 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 23 Jun 2026 23:18:05 +0200 Subject: [PATCH] refac --- backend/open_webui/main.py | 5 ++ .../open_webui/utils/context_compaction.py | 18 ++++++- backend/open_webui/utils/middleware.py | 1 + backend/open_webui/utils/payload.py | 1 + .../Settings/Advanced/AdvancedParams.svelte | 47 +++++++++++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index 25dda87f78..79eedf74b0 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -1065,6 +1065,7 @@ async def chat_completion( # Chat Params stream_delta_chunk_size = form_data.get('params', {}).get('stream_delta_chunk_size') reasoning_tags = form_data.get('params', {}).get('reasoning_tags') + compact_token_threshold = form_data.get('params', {}).get('compact_token_threshold') # Model Params if model_info_params.get('stream_response') is not None: @@ -1076,6 +1077,9 @@ async def chat_completion( if model_info_params.get('reasoning_tags') is not None: reasoning_tags = model_info_params.get('reasoning_tags') + if model_info_params.get('compact_token_threshold') is not None: + compact_token_threshold = model_info_params.get('compact_token_threshold') + # parent_id signals intent: # null → new chat (root message, no parent) # value → follow-up (user message's parentId = prev assistant) @@ -1133,6 +1137,7 @@ async def chat_completion( 'params': { 'stream_delta_chunk_size': stream_delta_chunk_size, 'reasoning_tags': reasoning_tags, + 'compact_token_threshold': compact_token_threshold, 'function_calling': ( form_data.get('params', {}).get('function_calling') or model_info_params.get('function_calling') diff --git a/backend/open_webui/utils/context_compaction.py b/backend/open_webui/utils/context_compaction.py index 416a3d3b24..f5d51e6834 100644 --- a/backend/open_webui/utils/context_compaction.py +++ b/backend/open_webui/utils/context_compaction.py @@ -53,8 +53,9 @@ async def compact_messages_for_request( return messages, None, False messages, previous_summary = _apply_latest_summary_checkpoint(messages) + token_threshold = _resolve_token_threshold(config['token_threshold'], metadata) if ( - not _exceeds_token_threshold(messages, system_prompt, previous_summary, config['token_threshold']) + not _exceeds_token_threshold(messages, system_prompt, previous_summary, token_threshold) or len(messages) <= 3 ): return messages, previous_summary, False @@ -147,6 +148,21 @@ async def _load_config() -> dict: } +def _parse_positive_int(value: Any) -> int | None: + try: + parsed = int(value) + except (TypeError, ValueError): + return None + return parsed if parsed > 0 else None + + +def _resolve_token_threshold(global_threshold: int, metadata: dict) -> int: + configured_threshold = _parse_positive_int((metadata.get('params') or {}).get('compact_token_threshold')) + if configured_threshold is None: + return global_threshold + return min(configured_threshold, global_threshold) + + def _apply_latest_summary_checkpoint(messages: list[dict]) -> tuple[list[dict], str | None]: summary = None summary_idx = None diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 2f9c078576..82ca932646 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -2076,6 +2076,7 @@ def apply_params_to_form_data(form_data, model): 'stream_delta_chunk_size': int, 'function_calling': str, 'reasoning_tags': list, + 'compact_token_threshold': int, 'system': str, } diff --git a/backend/open_webui/utils/payload.py b/backend/open_webui/utils/payload.py index 98c84b41ec..7799fa4754 100644 --- a/backend/open_webui/utils/payload.py +++ b/backend/open_webui/utils/payload.py @@ -72,6 +72,7 @@ def remove_open_webui_params(params: dict) -> dict: 'stream_delta_chunk_size': int, 'function_calling': str, 'reasoning_tags': list, + 'compact_token_threshold': int, 'system': str, } diff --git a/src/lib/components/chat/Settings/Advanced/AdvancedParams.svelte b/src/lib/components/chat/Settings/Advanced/AdvancedParams.svelte index 39c29b36c1..b2cd92bc7c 100644 --- a/src/lib/components/chat/Settings/Advanced/AdvancedParams.svelte +++ b/src/lib/components/chat/Settings/Advanced/AdvancedParams.svelte @@ -16,6 +16,7 @@ // Advanced stream_response: null, // Set stream responses for this model individually stream_delta_chunk_size: null, // Set the chunk size for streaming responses + compact_token_threshold: null, function_calling: null, reasoning_tags: null, seed: null, @@ -145,6 +146,52 @@ {/if} + +
+ +
+
+ {$i18n.t('Context Compaction Threshold')} +
+ +
+
+ + {#if (params?.compact_token_threshold ?? null) !== null} +
+
+ +
+
+ {/if} +
{/if}