refac
This commit is contained in:
1 parent
f0bfcd4097
commit
865c80c160
1 file changed
+47
-5
@@ -1,14 +1,18 @@
|
|||||||
|
import logging
|
||||||
from typing import Callable, Optional
|
from typing import Callable, Optional
|
||||||
|
|
||||||
from open_webui.utils.chat_variables import render_chat_variables, render_user_variables
|
from open_webui.utils.chat_variables import render_chat_variables, render_user_variables
|
||||||
from open_webui.utils.json_codec import JSONCodec
|
from open_webui.utils.json_codec import JSONCodec
|
||||||
from open_webui.utils.misc import (
|
from open_webui.utils.misc import (
|
||||||
add_or_update_system_message,
|
add_or_update_system_message,
|
||||||
|
convert_logit_bias_input_to_json,
|
||||||
deep_update,
|
deep_update,
|
||||||
replace_system_message_content,
|
replace_system_message_content,
|
||||||
)
|
)
|
||||||
from open_webui.utils.task import prompt_template, prompt_variables_template
|
from open_webui.utils.task import prompt_template, prompt_variables_template
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def resolve_system_prompt(
|
async def resolve_system_prompt(
|
||||||
system: Optional[str],
|
system: Optional[str],
|
||||||
@@ -80,13 +84,51 @@ def apply_model_params_to_body(params: dict, form_data: dict, mappings: dict[str
|
|||||||
|
|
||||||
def apply_params_to_form_data(form_data: dict, model: dict, params: dict | None = None) -> dict:
|
def apply_params_to_form_data(form_data: dict, model: dict, params: dict | None = None) -> dict:
|
||||||
payload_params = form_data.pop('params', {}) or {}
|
payload_params = form_data.pop('params', {}) or {}
|
||||||
params = dict(payload_params if params is None else params)
|
params = payload_params if params is None else dict(params)
|
||||||
if not params:
|
custom_params = params.pop('custom_params', {})
|
||||||
return form_data
|
|
||||||
|
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:
|
||||||
|
for key, value in custom_params.items():
|
||||||
|
if isinstance(value, str):
|
||||||
|
try:
|
||||||
|
custom_params[key] = JSONCodec.loads(value)
|
||||||
|
except JSONCodec.JSONDecodeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
params = deep_update(params, custom_params)
|
||||||
|
|
||||||
if model.get('owned_by') == 'ollama':
|
if model.get('owned_by') == 'ollama':
|
||||||
return apply_model_params_to_body_ollama(params, form_data)
|
form_data['options'] = {**params, **(form_data.get('options') or {})}
|
||||||
return apply_model_params_to_body_openai(params, form_data)
|
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
|
||||||
|
|
||||||
|
|
||||||
def remove_open_webui_params(params: dict) -> dict:
|
def remove_open_webui_params(params: dict) -> dict:
|
||||||
|
|||||||
Reference in new issue
Block a user