Skip to content

Commit 7f08376

Browse files
committed
refac
1 parent 15c7e37 commit 7f08376

5 files changed

Lines changed: 71 additions & 1 deletion

File tree

backend/open_webui/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,7 @@ async def chat_completion(
10651065
# Chat Params
10661066
stream_delta_chunk_size = form_data.get('params', {}).get('stream_delta_chunk_size')
10671067
reasoning_tags = form_data.get('params', {}).get('reasoning_tags')
1068+
compact_token_threshold = form_data.get('params', {}).get('compact_token_threshold')
10681069

10691070
# Model Params
10701071
if model_info_params.get('stream_response') is not None:
@@ -1076,6 +1077,9 @@ async def chat_completion(
10761077
if model_info_params.get('reasoning_tags') is not None:
10771078
reasoning_tags = model_info_params.get('reasoning_tags')
10781079

1080+
if model_info_params.get('compact_token_threshold') is not None:
1081+
compact_token_threshold = model_info_params.get('compact_token_threshold')
1082+
10791083
# parent_id signals intent:
10801084
# null → new chat (root message, no parent)
10811085
# value → follow-up (user message's parentId = prev assistant)
@@ -1133,6 +1137,7 @@ async def chat_completion(
11331137
'params': {
11341138
'stream_delta_chunk_size': stream_delta_chunk_size,
11351139
'reasoning_tags': reasoning_tags,
1140+
'compact_token_threshold': compact_token_threshold,
11361141
'function_calling': (
11371142
form_data.get('params', {}).get('function_calling')
11381143
or model_info_params.get('function_calling')

backend/open_webui/utils/context_compaction.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ async def compact_messages_for_request(
5353
return messages, None, False
5454

5555
messages, previous_summary = _apply_latest_summary_checkpoint(messages)
56+
token_threshold = _resolve_token_threshold(config['token_threshold'], metadata)
5657
if (
57-
not _exceeds_token_threshold(messages, system_prompt, previous_summary, config['token_threshold'])
58+
not _exceeds_token_threshold(messages, system_prompt, previous_summary, token_threshold)
5859
or len(messages) <= 3
5960
):
6061
return messages, previous_summary, False
@@ -147,6 +148,21 @@ async def _load_config() -> dict:
147148
}
148149

149150

151+
def _parse_positive_int(value: Any) -> int | None:
152+
try:
153+
parsed = int(value)
154+
except (TypeError, ValueError):
155+
return None
156+
return parsed if parsed > 0 else None
157+
158+
159+
def _resolve_token_threshold(global_threshold: int, metadata: dict) -> int:
160+
configured_threshold = _parse_positive_int((metadata.get('params') or {}).get('compact_token_threshold'))
161+
if configured_threshold is None:
162+
return global_threshold
163+
return min(configured_threshold, global_threshold)
164+
165+
150166
def _apply_latest_summary_checkpoint(messages: list[dict]) -> tuple[list[dict], str | None]:
151167
summary = None
152168
summary_idx = None

backend/open_webui/utils/middleware.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,7 @@ def apply_params_to_form_data(form_data, model):
20762076
'stream_delta_chunk_size': int,
20772077
'function_calling': str,
20782078
'reasoning_tags': list,
2079+
'compact_token_threshold': int,
20792080
'system': str,
20802081
}
20812082

backend/open_webui/utils/payload.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def remove_open_webui_params(params: dict) -> dict:
7272
'stream_delta_chunk_size': int,
7373
'function_calling': str,
7474
'reasoning_tags': list,
75+
'compact_token_threshold': int,
7576
'system': str,
7677
}
7778

src/lib/components/chat/Settings/Advanced/AdvancedParams.svelte

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// Advanced
1717
stream_response: null, // Set stream responses for this model individually
1818
stream_delta_chunk_size: null, // Set the chunk size for streaming responses
19+
compact_token_threshold: null,
1920
function_calling: null,
2021
reasoning_tags: null,
2122
seed: null,
@@ -145,6 +146,52 @@
145146
</div>
146147
{/if}
147148
</div>
149+
150+
<div>
151+
<Tooltip
152+
content={$i18n.t(
153+
'Lower the context compaction token threshold for this model. The global context compaction threshold remains the maximum.'
154+
)}
155+
placement="top-start"
156+
className="inline-tooltip"
157+
>
158+
<div class="flex w-full justify-between">
159+
<div class=" self-center text-xs">
160+
{$i18n.t('Context Compaction Threshold')}
161+
</div>
162+
<button
163+
class="p-1 px-3 text-xs flex rounded-sm transition shrink-0 outline-hidden"
164+
type="button"
165+
on:click={() => {
166+
params.compact_token_threshold =
167+
(params?.compact_token_threshold ?? null) === null ? 80000 : null;
168+
}}
169+
>
170+
{#if (params?.compact_token_threshold ?? null) === null}
171+
<span class="ml-2 self-center"> {$i18n.t('Default')} </span>
172+
{:else}
173+
<span class="ml-2 self-center"> {$i18n.t('Custom')} </span>
174+
{/if}
175+
</button>
176+
</div>
177+
</Tooltip>
178+
179+
{#if (params?.compact_token_threshold ?? null) !== null}
180+
<div class="flex mt-0.5 space-x-2">
181+
<div class=" flex-1">
182+
<input
183+
class="text-sm w-full bg-transparent outline-hidden outline-none"
184+
type="number"
185+
placeholder={$i18n.t('Enter token threshold')}
186+
bind:value={params.compact_token_threshold}
187+
autocomplete="off"
188+
min="1"
189+
step="1"
190+
/>
191+
</div>
192+
</div>
193+
{/if}
194+
</div>
148195
{/if}
149196

150197
<div>

0 commit comments

Comments
 (0)