mirror of
https://github.com/open-webui/open-webui.git
synced 2026-08-13 01:02:25 -06:00
refac
This commit is contained in:
@@ -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')
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
|
||||
@@ -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 @@
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Tooltip
|
||||
content={$i18n.t(
|
||||
'Lower the context compaction token threshold for this model. The global context compaction threshold remains the maximum.'
|
||||
)}
|
||||
placement="top-start"
|
||||
className="inline-tooltip"
|
||||
>
|
||||
<div class="flex w-full justify-between">
|
||||
<div class=" self-center text-xs">
|
||||
{$i18n.t('Context Compaction Threshold')}
|
||||
</div>
|
||||
<button
|
||||
class="p-1 px-3 text-xs flex rounded-sm transition shrink-0 outline-hidden"
|
||||
type="button"
|
||||
on:click={() => {
|
||||
params.compact_token_threshold =
|
||||
(params?.compact_token_threshold ?? null) === null ? 80000 : null;
|
||||
}}
|
||||
>
|
||||
{#if (params?.compact_token_threshold ?? null) === null}
|
||||
<span class="ml-2 self-center"> {$i18n.t('Default')} </span>
|
||||
{:else}
|
||||
<span class="ml-2 self-center"> {$i18n.t('Custom')} </span>
|
||||
{/if}
|
||||
</button>
|
||||
</div>
|
||||
</Tooltip>
|
||||
|
||||
{#if (params?.compact_token_threshold ?? null) !== null}
|
||||
<div class="flex mt-0.5 space-x-2">
|
||||
<div class=" flex-1">
|
||||
<input
|
||||
class="text-sm w-full bg-transparent outline-hidden outline-none"
|
||||
type="number"
|
||||
placeholder={$i18n.t('Enter token threshold')}
|
||||
bind:value={params.compact_token_threshold}
|
||||
autocomplete="off"
|
||||
min="1"
|
||||
step="1"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div>
|
||||
|
||||
Reference in New Issue
Block a user