refac
This commit is contained in:
@@ -9,6 +9,7 @@ from open_webui.models.config import Config
|
||||
from open_webui.utils.chat_id import is_saved_chat_id
|
||||
from open_webui.utils.json_codec import JSONCodec
|
||||
from open_webui.utils.misc import get_content_from_message, get_last_user_message, get_message_list
|
||||
from open_webui.utils.payload import apply_params_to_form_data
|
||||
from open_webui.utils.task import (
|
||||
get_task_model_id,
|
||||
prompt_template,
|
||||
@@ -367,6 +368,7 @@ async def _generate_summary(
|
||||
task_config = await Config.get_many(
|
||||
'task.model.default',
|
||||
'task.model.external',
|
||||
'task.model.params',
|
||||
'chat.context_compaction.model',
|
||||
)
|
||||
context_compaction_model = task_config.get('chat.context_compaction.model')
|
||||
@@ -394,22 +396,25 @@ async def _generate_summary(
|
||||
prompt = prompt_variables_template(prompt, {'{{PREVIOUS_SUMMARY}}': previous_summary or ''})
|
||||
prompt = await prompt_template(prompt, user)
|
||||
|
||||
max_tokens = models[task_model_id].get('info', {}).get('params', {}).get('max_tokens', 1000)
|
||||
task_model_params = task_config.get('task.model.params') or {}
|
||||
if not isinstance(task_model_params, dict):
|
||||
task_model_params = {}
|
||||
task_model_params = {key: value for key, value in task_model_params.items() if value is not None and value != ''}
|
||||
task_model_params = task_model_params or {
|
||||
'max_tokens': models[task_model_id].get('info', {}).get('params', {}).get('max_tokens', 1000)
|
||||
}
|
||||
|
||||
payload = {
|
||||
'model': task_model_id,
|
||||
'messages': [{'role': 'user', 'content': prompt}],
|
||||
'stream': False,
|
||||
**(
|
||||
{'max_tokens': max_tokens}
|
||||
if models[task_model_id].get('owned_by') == 'ollama'
|
||||
else {'max_completion_tokens': max_tokens}
|
||||
),
|
||||
'metadata': {
|
||||
**(request.state.metadata if hasattr(request.state, 'metadata') else {}),
|
||||
'task': 'context_compaction',
|
||||
},
|
||||
}
|
||||
|
||||
payload = apply_params_to_form_data(payload, models[task_model_id], task_model_params)
|
||||
response = await generate_chat_completion(request, form_data=payload, user=user)
|
||||
summary = _response_text(response).strip()
|
||||
if summary:
|
||||
|
||||
@@ -100,9 +100,7 @@ from open_webui.utils.memory import add_memory_context, review_memory_after_turn
|
||||
from open_webui.utils.misc import (
|
||||
add_or_update_system_message,
|
||||
add_or_update_user_message,
|
||||
convert_logit_bias_input_to_json,
|
||||
convert_output_to_messages,
|
||||
deep_update,
|
||||
extract_urls,
|
||||
get_content_from_message,
|
||||
get_last_assistant_message,
|
||||
@@ -118,7 +116,7 @@ from open_webui.utils.misc import (
|
||||
set_last_user_message_content,
|
||||
strip_empty_content_blocks,
|
||||
)
|
||||
from open_webui.utils.payload import apply_system_prompt_to_body, resolve_system_prompt
|
||||
from open_webui.utils.payload import apply_params_to_form_data, apply_system_prompt_to_body, resolve_system_prompt
|
||||
from open_webui.utils.plugin import load_function_module_by_id
|
||||
from open_webui.utils.response import merge_usage, normalize_usage
|
||||
from open_webui.utils.sanitize import sanitize_code
|
||||
@@ -1942,59 +1940,6 @@ async def chat_completion_files_handler(
|
||||
return body, {'sources': sources}
|
||||
|
||||
|
||||
def apply_params_to_form_data(form_data, model):
|
||||
params = form_data.pop('params', {})
|
||||
custom_params = params.pop('custom_params', {})
|
||||
|
||||
open_webui_params = {
|
||||
'stream_response': bool,
|
||||
'stream_delta_chunk_size': int,
|
||||
'function_calling': str,
|
||||
'reasoning_tags': list,
|
||||
'compact_token_threshold': int,
|
||||
'system': str,
|
||||
'note_id': str,
|
||||
}
|
||||
|
||||
for key in list(params.keys()):
|
||||
if key in open_webui_params:
|
||||
del params[key]
|
||||
|
||||
if custom_params:
|
||||
# Attempt to parse custom_params if they are strings
|
||||
for key, value in custom_params.items():
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
# Attempt to parse the string as JSON
|
||||
custom_params[key] = JSONCodec.loads(value)
|
||||
except JSONCodec.JSONDecodeError:
|
||||
# If it fails, keep the original string
|
||||
pass
|
||||
|
||||
# If custom_params are provided, merge them into params
|
||||
params = deep_update(params, custom_params)
|
||||
|
||||
if model.get('owned_by') == 'ollama':
|
||||
# Ollama specific parameters
|
||||
form_data['options'] = {**params, **(form_data.get('options') or {})}
|
||||
else:
|
||||
if isinstance(params, dict):
|
||||
for key, value in params.items():
|
||||
if value is not None and key not in form_data:
|
||||
form_data[key] = value
|
||||
|
||||
if 'logit_bias' in params and params['logit_bias'] is not None and 'logit_bias' not in form_data:
|
||||
try:
|
||||
logit_bias = convert_logit_bias_input_to_json(params['logit_bias'])
|
||||
|
||||
if logit_bias:
|
||||
form_data['logit_bias'] = JSONCodec.loads(logit_bias)
|
||||
except Exception as e:
|
||||
log.exception(f'Error parsing logit_bias: {e}')
|
||||
|
||||
return form_data
|
||||
|
||||
|
||||
async def convert_url_images_to_base64(form_data, user=None):
|
||||
messages = form_data.get('messages', [])
|
||||
|
||||
|
||||
@@ -78,6 +78,17 @@ def apply_model_params_to_body(params: dict, form_data: dict, mappings: dict[str
|
||||
return form_data
|
||||
|
||||
|
||||
def apply_params_to_form_data(form_data: dict, model: dict, params: dict | None = None) -> dict:
|
||||
payload_params = form_data.pop('params', {}) or {}
|
||||
params = dict(payload_params if params is None else params)
|
||||
if not params:
|
||||
return form_data
|
||||
|
||||
if model.get('owned_by') == 'ollama':
|
||||
return apply_model_params_to_body_ollama(params, form_data)
|
||||
return apply_model_params_to_body_openai(params, form_data)
|
||||
|
||||
|
||||
def remove_open_webui_params(params: dict) -> dict:
|
||||
"""
|
||||
Removes OpenWebUI specific parameters from the provided dictionary.
|
||||
@@ -95,6 +106,7 @@ def remove_open_webui_params(params: dict) -> dict:
|
||||
'reasoning_tags': list,
|
||||
'compact_token_threshold': int,
|
||||
'system': str,
|
||||
'note_id': str,
|
||||
}
|
||||
|
||||
for key in list(params.keys()):
|
||||
|
||||
Reference in New Issue
Block a user