Upload 6 files
Browse files
app.py
CHANGED
|
@@ -205,6 +205,123 @@ def _recompute_out_and_state_from_tokens(model_name: str, model_tokens: List[int
|
|
| 205 |
return out, model_state
|
| 206 |
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
# Move ChatCompletionRequest definition above fallback apply_model_tags_to_request
|
| 210 |
|
|
@@ -335,12 +452,16 @@ class ChatCompletionRequest(BaseModel):
|
|
| 335 |
include_usage: Optional[bool] = Field(default=False)
|
| 336 |
stop: Optional[list[str]] = Field(["\n\n"])
|
| 337 |
stop_tokens: Optional[list[int]] = Field([0])
|
| 338 |
-
|
| 339 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
auto_web_search: Optional[bool] = Field(default=True, description="Whether to enable web_search based on auto-detected intent")
|
| 341 |
enable_tools: Optional[bool] = Field(default=None, description="Explicitly enable tools (overrides auto detection)")
|
| 342 |
auto_tools: Optional[bool] = Field(default=True, description="Whether to enable tools based on auto-detected intent")
|
| 343 |
-
enable_reasoning: Optional[bool] = Field(default=
|
| 344 |
auto_reasoning: Optional[bool] = Field(default=True, description="Whether to enable reasoning based on auto detection")
|
| 345 |
enable_universal: Optional[bool] = Field(default=None, description="Explicitly enable the universal tool execution")
|
| 346 |
auto_universal: Optional[bool] = Field(default=True, description="Whether to auto enable universal tool execution")
|
|
@@ -544,112 +665,18 @@ async def chatResponse(
|
|
| 544 |
|
| 545 |
createTimestamp = time.time()
|
| 546 |
|
| 547 |
-
|
| 548 |
-
web_search_enabled = (
|
| 549 |
-
True
|
| 550 |
-
if (request.enable_web_search is not None and request.enable_web_search)
|
| 551 |
-
else (
|
| 552 |
-
request.web_search
|
| 553 |
-
or (request.auto_web_search if request.auto_web_search is not None else CONFIG.AUTO_ENABLE_WEB_SEARCH and detection.get('need_web_search'))
|
| 554 |
-
)
|
| 555 |
-
)
|
| 556 |
-
if not getattr(CONFIG, 'ENABLE_WEB_SEARCH_BY_DEFAULT', True) and request.enable_web_search is None and not request.web_search:
|
| 557 |
-
web_search_enabled = False
|
| 558 |
-
try:
|
| 559 |
-
if request.sampler and getattr(request.sampler, 'ALLOW_WEB_SEARCH', None) is not None:
|
| 560 |
-
web_search_enabled = bool(request.sampler.ALLOW_WEB_SEARCH)
|
| 561 |
-
elif hasattr(request, 'sampler_allow_web_search') and request.sampler_allow_web_search is not None:
|
| 562 |
-
web_search_enabled = bool(request.sampler_allow_web_search)
|
| 563 |
-
else:
|
| 564 |
-
ms = MODEL_STORAGE.get(request.model)
|
| 565 |
-
if ms and ms.MODEL_CONFIG:
|
| 566 |
-
if hasattr(ms.MODEL_CONFIG, 'DEFAULT_SAMPLER') and getattr(ms.MODEL_CONFIG.DEFAULT_SAMPLER, 'ALLOW_WEB_SEARCH', None) is not None:
|
| 567 |
-
web_search_enabled = bool(ms.MODEL_CONFIG.DEFAULT_SAMPLER.ALLOW_WEB_SEARCH)
|
| 568 |
-
elif hasattr(ms.MODEL_CONFIG, 'ALLOW_WEB_SEARCH') and not ms.MODEL_CONFIG.ALLOW_WEB_SEARCH:
|
| 569 |
-
web_search_enabled = False
|
| 570 |
-
except Exception:
|
| 571 |
-
pass
|
| 572 |
-
return web_search_enabled
|
| 573 |
|
| 574 |
def decide_file_tool_enabled(request):
|
| 575 |
-
|
| 576 |
-
file_tool_enabled = bool(request.enable_file_tool)
|
| 577 |
-
else:
|
| 578 |
-
auto_file_flag = request.auto_file_tool if request.auto_file_tool is not None else CONFIG.AUTO_ENABLE_TOOLS
|
| 579 |
-
file_tool_enabled = bool((request.file_ids and len(request.file_ids) > 0) or (auto_file_flag and request.file_ids))
|
| 580 |
-
if not getattr(CONFIG, 'ALLOW_FILE_TOOL_BY_DEFAULT', True) and request.enable_file_tool is None:
|
| 581 |
-
file_tool_enabled = False
|
| 582 |
-
try:
|
| 583 |
-
if request.sampler and getattr(request.sampler, 'ALLOW_FILE_TOOL', None) is not None:
|
| 584 |
-
file_tool_enabled = bool(request.sampler.ALLOW_FILE_TOOL)
|
| 585 |
-
elif hasattr(request, 'sampler_allow_file_tool') and request.sampler_allow_file_tool is not None:
|
| 586 |
-
file_tool_enabled = bool(request.sampler_allow_file_tool)
|
| 587 |
-
else:
|
| 588 |
-
ms = MODEL_STORAGE.get(request.model)
|
| 589 |
-
if ms and ms.MODEL_CONFIG:
|
| 590 |
-
if hasattr(ms.MODEL_CONFIG, 'DEFAULT_SAMPLER') and getattr(ms.MODEL_CONFIG.DEFAULT_SAMPLER, 'ALLOW_FILE_TOOL', None) is not None:
|
| 591 |
-
file_tool_enabled = bool(ms.MODEL_CONFIG.DEFAULT_SAMPLER.ALLOW_FILE_TOOL)
|
| 592 |
-
elif hasattr(ms.MODEL_CONFIG, 'ALLOW_FILE_TOOL') and not ms.MODEL_CONFIG.ALLOW_FILE_TOOL:
|
| 593 |
-
file_tool_enabled = False
|
| 594 |
-
except Exception:
|
| 595 |
-
pass
|
| 596 |
-
return file_tool_enabled
|
| 597 |
|
| 598 |
def decide_tools_enabled(request, detection):
|
| 599 |
-
|
| 600 |
-
tools_enabled = bool(request.enable_tools)
|
| 601 |
-
else:
|
| 602 |
-
auto_tools_flag = request.auto_tools if request.auto_tools is not None else CONFIG.AUTO_ENABLE_TOOLS
|
| 603 |
-
tools_enabled = bool(request.tools) or CONFIG.ENABLE_TOOLS_BY_DEFAULT or (auto_tools_flag and (detection.get('need_calc') or detection.get('need_web_search')))
|
| 604 |
-
try:
|
| 605 |
-
if request.sampler and getattr(request.sampler, 'ALLOW_TOOLS', None) is not None:
|
| 606 |
-
tools_enabled = bool(request.sampler.ALLOW_TOOLS)
|
| 607 |
-
elif hasattr(request, 'sampler_allow_tools') and request.sampler_allow_tools is not None:
|
| 608 |
-
tools_enabled = bool(request.sampler_allow_tools)
|
| 609 |
-
else:
|
| 610 |
-
ms = MODEL_STORAGE.get(request.model)
|
| 611 |
-
if ms and ms.MODEL_CONFIG:
|
| 612 |
-
if hasattr(ms.MODEL_CONFIG, 'DEFAULT_SAMPLER') and getattr(ms.MODEL_CONFIG.DEFAULT_SAMPLER, 'ALLOW_TOOLS', None) is not None:
|
| 613 |
-
if not ms.MODEL_CONFIG.DEFAULT_SAMPLER.ALLOW_TOOLS:
|
| 614 |
-
tools_enabled = False
|
| 615 |
-
elif hasattr(ms.MODEL_CONFIG, 'ALLOW_TOOLS') and not ms.MODEL_CONFIG.ALLOW_TOOLS:
|
| 616 |
-
tools_enabled = False
|
| 617 |
-
except Exception:
|
| 618 |
-
pass
|
| 619 |
-
return tools_enabled
|
| 620 |
|
| 621 |
def decide_reasoning_enabled(request, detection, enableReasoning):
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
else (
|
| 626 |
-
bool(enableReasoning) or bool(request.auto_reasoning if request.auto_reasoning is not None else (CONFIG.AUTO_ENABLE_REASONING and bool(detection.get('need_reasoning'))))
|
| 627 |
-
)
|
| 628 |
-
)
|
| 629 |
-
if not getattr(CONFIG, 'ENABLE_REASONING_BY_DEFAULT', True) and request.enable_reasoning is None:
|
| 630 |
-
reasoning_enabled = False
|
| 631 |
-
try:
|
| 632 |
-
if request.sampler and getattr(request.sampler, 'ALLOW_REASONING', None) is not None:
|
| 633 |
-
reasoning_enabled = bool(request.sampler.ALLOW_REASONING)
|
| 634 |
-
elif hasattr(request, 'sampler_allow_reasoning') and request.sampler_allow_reasoning is not None:
|
| 635 |
-
reasoning_enabled = bool(request.sampler_allow_reasoning)
|
| 636 |
-
else:
|
| 637 |
-
ms = MODEL_STORAGE.get(request.model)
|
| 638 |
-
if ms and ms.MODEL_CONFIG:
|
| 639 |
-
if hasattr(ms.MODEL_CONFIG, 'DEFAULT_SAMPLER') and getattr(ms.MODEL_CONFIG.DEFAULT_SAMPLER, 'ALLOW_REASONING', None) is not None:
|
| 640 |
-
if not ms.MODEL_CONFIG.DEFAULT_SAMPLER.ALLOW_REASONING:
|
| 641 |
-
reasoning_enabled = False
|
| 642 |
-
elif hasattr(ms.MODEL_CONFIG, 'ALLOW_REASONING') and not ms.MODEL_CONFIG.ALLOW_REASONING:
|
| 643 |
-
reasoning_enabled = False
|
| 644 |
-
except Exception:
|
| 645 |
-
pass
|
| 646 |
-
try:
|
| 647 |
-
ms = MODEL_STORAGE.get(request.model)
|
| 648 |
-
if ms and ms.MODEL_CONFIG and hasattr(ms.MODEL_CONFIG, 'ALLOW_REASONING') and not ms.MODEL_CONFIG.ALLOW_REASONING:
|
| 649 |
-
reasoning_enabled = False
|
| 650 |
-
except Exception:
|
| 651 |
-
pass
|
| 652 |
-
return reasoning_enabled
|
| 653 |
|
| 654 |
def execute_tools(request, detection, prompt, executed_tool_calls, web_search_enabled, tools_enabled, file_tool_enabled, raw_prompt):
|
| 655 |
"""Helper to execute tools and update prompt and executed_tool_calls."""
|
|
@@ -730,6 +757,33 @@ async def chatResponse(
|
|
| 730 |
detection = detect_tools_and_reasoning(raw_prompt)
|
| 731 |
prompt = raw_prompt if request.prompt is not None else f"{cleanMessages(request.messages or [])}\n\nAssistant:{' <think' if enableReasoning else ''}"
|
| 732 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 733 |
web_search_enabled = decide_web_search_enabled(request, detection)
|
| 734 |
file_tool_enabled = decide_file_tool_enabled(request)
|
| 735 |
tools_enabled = decide_tools_enabled(request, detection)
|
|
@@ -964,28 +1018,12 @@ async def chatResponseStream(
|
|
| 964 |
# Intent detection and defaults: check whether to auto-enable tools, web_search, reasoning
|
| 965 |
detection = detect_tools_and_reasoning(raw_prompt)
|
| 966 |
|
| 967 |
-
|
| 968 |
-
|
| 969 |
-
|
| 970 |
-
|
| 971 |
-
|
| 972 |
-
|
| 973 |
-
)
|
| 974 |
-
)
|
| 975 |
-
|
| 976 |
-
if request.enable_tools is not None:
|
| 977 |
-
tools_enabled = bool(request.enable_tools)
|
| 978 |
-
else:
|
| 979 |
-
auto_tools_flag = request.auto_tools if request.auto_tools is not None else CONFIG.AUTO_ENABLE_TOOLS
|
| 980 |
-
tools_enabled = bool(request.tools) or CONFIG.ENABLE_TOOLS_BY_DEFAULT or (auto_tools_flag and (detection.get('need_calc') or detection.get('need_web_search')))
|
| 981 |
-
|
| 982 |
-
reasoning_enabled = bool(
|
| 983 |
-
True
|
| 984 |
-
if (request.enable_reasoning is not None and request.enable_reasoning)
|
| 985 |
-
else (
|
| 986 |
-
bool(enableReasoning) or bool(request.auto_reasoning if request.auto_reasoning is not None else (CONFIG.AUTO_ENABLE_REASONING and bool(detection.get('need_reasoning'))))
|
| 987 |
-
)
|
| 988 |
-
)
|
| 989 |
enableReasoning = reasoning_enabled
|
| 990 |
try:
|
| 991 |
ms_cfg = MODEL_STORAGE.get(request.model)
|
|
@@ -993,28 +1031,7 @@ async def chatResponseStream(
|
|
| 993 |
enableReasoning = False
|
| 994 |
except Exception:
|
| 995 |
pass
|
| 996 |
-
#
|
| 997 |
-
if request.enable_file_tool is not None:
|
| 998 |
-
file_tool_enabled = bool(request.enable_file_tool)
|
| 999 |
-
else:
|
| 1000 |
-
auto_file_flag = request.auto_file_tool if request.auto_file_tool is not None else CONFIG.AUTO_ENABLE_TOOLS
|
| 1001 |
-
file_tool_enabled = bool((request.file_ids and len(request.file_ids) > 0) or (auto_file_flag and request.file_ids))
|
| 1002 |
-
if not getattr(CONFIG, 'ALLOW_FILE_TOOL_BY_DEFAULT', True) and request.enable_file_tool is None:
|
| 1003 |
-
file_tool_enabled = False
|
| 1004 |
-
try:
|
| 1005 |
-
if request.sampler and getattr(request.sampler, 'ALLOW_FILE_TOOL', None) is not None:
|
| 1006 |
-
file_tool_enabled = bool(request.sampler.ALLOW_FILE_TOOL)
|
| 1007 |
-
elif hasattr(request, 'sampler_allow_file_tool') and request.sampler_allow_file_tool is not None:
|
| 1008 |
-
file_tool_enabled = bool(request.sampler_allow_file_tool)
|
| 1009 |
-
else:
|
| 1010 |
-
ms2 = MODEL_STORAGE.get(request.model)
|
| 1011 |
-
if ms2 and ms2.MODEL_CONFIG:
|
| 1012 |
-
if hasattr(ms2.MODEL_CONFIG, 'DEFAULT_SAMPLER') and getattr(ms2.MODEL_CONFIG.DEFAULT_SAMPLER, 'ALLOW_FILE_TOOL', None) is not None:
|
| 1013 |
-
file_tool_enabled = bool(ms2.MODEL_CONFIG.DEFAULT_SAMPLER.ALLOW_FILE_TOOL)
|
| 1014 |
-
elif hasattr(ms2.MODEL_CONFIG, 'ALLOW_FILE_TOOL') and not ms2.MODEL_CONFIG.ALLOW_FILE_TOOL:
|
| 1015 |
-
file_tool_enabled = False
|
| 1016 |
-
except Exception:
|
| 1017 |
-
pass
|
| 1018 |
# Build final prompt after deciding enableReasoning
|
| 1019 |
prompt = raw_prompt if request.prompt is not None else f"{cleanMessages(request.messages or [], enableReasoning)}\n\nAssistant:{' <think' if enableReasoning else ''}"
|
| 1020 |
|
|
|
|
| 205 |
return out, model_state
|
| 206 |
|
| 207 |
|
| 208 |
+
def resolve_request_flags(request, detection):
|
| 209 |
+
"""Resolve effective booleans for web_search, file_tool, tools, and reasoning
|
| 210 |
+
based on request flags (explicit), sampler overrides, model defaults and detection.
|
| 211 |
+
Returns dict with keys: web_search_enabled, file_tool_enabled, tools_enabled, reasoning_enabled.
|
| 212 |
+
"""
|
| 213 |
+
# Web search
|
| 214 |
+
web_search_enabled = (
|
| 215 |
+
True
|
| 216 |
+
if (request.enable_web_search is not None and request.enable_web_search)
|
| 217 |
+
else (
|
| 218 |
+
request.web_search
|
| 219 |
+
or (request.auto_web_search if request.auto_web_search is not None else (getattr(CONFIG, 'AUTO_ENABLE_WEB_SEARCH', True) and detection.get('need_web_search')))
|
| 220 |
+
)
|
| 221 |
+
)
|
| 222 |
+
if not getattr(CONFIG, 'ENABLE_WEB_SEARCH_BY_DEFAULT', True) and request.enable_web_search is None and not (request.web_search or False):
|
| 223 |
+
web_search_enabled = False
|
| 224 |
+
try:
|
| 225 |
+
if request.sampler and getattr(request.sampler, 'ALLOW_WEB_SEARCH', None) is not None:
|
| 226 |
+
web_search_enabled = bool(request.sampler.ALLOW_WEB_SEARCH)
|
| 227 |
+
elif hasattr(request, 'sampler_allow_web_search') and request.sampler_allow_web_search is not None:
|
| 228 |
+
web_search_enabled = bool(request.sampler_allow_web_search)
|
| 229 |
+
else:
|
| 230 |
+
ms = MODEL_STORAGE.get(request.model)
|
| 231 |
+
if ms and ms.MODEL_CONFIG:
|
| 232 |
+
if hasattr(ms.MODEL_CONFIG, 'DEFAULT_SAMPLER') and getattr(ms.MODEL_CONFIG.DEFAULT_SAMPLER, 'ALLOW_WEB_SEARCH', None) is not None:
|
| 233 |
+
web_search_enabled = bool(ms.MODEL_CONFIG.DEFAULT_SAMPLER.ALLOW_WEB_SEARCH)
|
| 234 |
+
elif hasattr(ms.MODEL_CONFIG, 'ALLOW_WEB_SEARCH') and not ms.MODEL_CONFIG.ALLOW_WEB_SEARCH:
|
| 235 |
+
web_search_enabled = False
|
| 236 |
+
except Exception:
|
| 237 |
+
pass
|
| 238 |
+
|
| 239 |
+
# File tool decision
|
| 240 |
+
if request.enable_file_tool is not None:
|
| 241 |
+
file_tool_enabled = bool(request.enable_file_tool)
|
| 242 |
+
else:
|
| 243 |
+
auto_file_flag = request.auto_file_tool if request.auto_file_tool is not None else getattr(CONFIG, 'AUTO_ENABLE_TOOLS', True)
|
| 244 |
+
file_tool_enabled = bool((request.file_ids and len(request.file_ids) > 0) or (auto_file_flag and request.file_ids))
|
| 245 |
+
if not getattr(CONFIG, 'ALLOW_FILE_TOOL_BY_DEFAULT', True) and request.enable_file_tool is None:
|
| 246 |
+
file_tool_enabled = False
|
| 247 |
+
try:
|
| 248 |
+
if request.sampler and getattr(request.sampler, 'ALLOW_FILE_TOOL', None) is not None:
|
| 249 |
+
file_tool_enabled = bool(request.sampler.ALLOW_FILE_TOOL)
|
| 250 |
+
elif hasattr(request, 'sampler_allow_file_tool') and request.sampler_allow_file_tool is not None:
|
| 251 |
+
file_tool_enabled = bool(request.sampler_allow_file_tool)
|
| 252 |
+
else:
|
| 253 |
+
ms = MODEL_STORAGE.get(request.model)
|
| 254 |
+
if ms and ms.MODEL_CONFIG:
|
| 255 |
+
if hasattr(ms.MODEL_CONFIG, 'DEFAULT_SAMPLER') and getattr(ms.MODEL_CONFIG.DEFAULT_SAMPLER, 'ALLOW_FILE_TOOL', None) is not None:
|
| 256 |
+
file_tool_enabled = bool(ms.MODEL_CONFIG.DEFAULT_SAMPLER.ALLOW_FILE_TOOL)
|
| 257 |
+
elif hasattr(ms.MODEL_CONFIG, 'ALLOW_FILE_TOOL') and not ms.MODEL_CONFIG.ALLOW_FILE_TOOL:
|
| 258 |
+
file_tool_enabled = False
|
| 259 |
+
except Exception:
|
| 260 |
+
pass
|
| 261 |
+
|
| 262 |
+
# Tools decision
|
| 263 |
+
if request.enable_tools is not None:
|
| 264 |
+
tools_enabled = bool(request.enable_tools)
|
| 265 |
+
else:
|
| 266 |
+
auto_tools_flag = request.auto_tools if request.auto_tools is not None else getattr(CONFIG, 'AUTO_ENABLE_TOOLS', True)
|
| 267 |
+
tools_enabled = bool(request.tools) or getattr(CONFIG, 'ENABLE_TOOLS_BY_DEFAULT', False) or (auto_tools_flag and (detection.get('need_calc') or detection.get('need_web_search')))
|
| 268 |
+
try:
|
| 269 |
+
if request.sampler and getattr(request.sampler, 'ALLOW_TOOLS', None) is not None:
|
| 270 |
+
tools_enabled = bool(request.sampler.ALLOW_TOOLS)
|
| 271 |
+
elif hasattr(request, 'sampler_allow_tools') and request.sampler_allow_tools is not None:
|
| 272 |
+
tools_enabled = bool(request.sampler_allow_tools)
|
| 273 |
+
else:
|
| 274 |
+
ms = MODEL_STORAGE.get(request.model)
|
| 275 |
+
if ms and ms.MODEL_CONFIG:
|
| 276 |
+
if hasattr(ms.MODEL_CONFIG, 'DEFAULT_SAMPLER') and getattr(ms.MODEL_CONFIG.DEFAULT_SAMPLER, 'ALLOW_TOOLS', None) is not None:
|
| 277 |
+
if not ms.MODEL_CONFIG.DEFAULT_SAMPLER.ALLOW_TOOLS:
|
| 278 |
+
tools_enabled = False
|
| 279 |
+
elif hasattr(ms.MODEL_CONFIG, 'ALLOW_TOOLS') and not ms.MODEL_CONFIG.ALLOW_TOOLS:
|
| 280 |
+
tools_enabled = False
|
| 281 |
+
except Exception:
|
| 282 |
+
pass
|
| 283 |
+
|
| 284 |
+
# Reasoning decision
|
| 285 |
+
reasoning_enabled = bool(
|
| 286 |
+
True
|
| 287 |
+
if (request.enable_reasoning is not None and request.enable_reasoning)
|
| 288 |
+
else (
|
| 289 |
+
bool(False) or bool(request.auto_reasoning if request.auto_reasoning is not None else (getattr(CONFIG, 'AUTO_ENABLE_REASONING', True) and bool(detection.get('need_reasoning'))))
|
| 290 |
+
)
|
| 291 |
+
)
|
| 292 |
+
if not getattr(CONFIG, 'ENABLE_REASONING_BY_DEFAULT', True) and request.enable_reasoning is None:
|
| 293 |
+
reasoning_enabled = False
|
| 294 |
+
try:
|
| 295 |
+
if request.sampler and getattr(request.sampler, 'ALLOW_REASONING', None) is not None:
|
| 296 |
+
reasoning_enabled = bool(request.sampler.ALLOW_REASONING)
|
| 297 |
+
elif hasattr(request, 'sampler_allow_reasoning') and request.sampler_allow_reasoning is not None:
|
| 298 |
+
reasoning_enabled = bool(request.sampler_allow_reasoning)
|
| 299 |
+
else:
|
| 300 |
+
ms = MODEL_STORAGE.get(request.model)
|
| 301 |
+
if ms and ms.MODEL_CONFIG:
|
| 302 |
+
if hasattr(ms.MODEL_CONFIG, 'DEFAULT_SAMPLER') and getattr(ms.MODEL_CONFIG.DEFAULT_SAMPLER, 'ALLOW_REASONING', None) is not None:
|
| 303 |
+
if not ms.MODEL_CONFIG.DEFAULT_SAMPLER.ALLOW_REASONING:
|
| 304 |
+
reasoning_enabled = False
|
| 305 |
+
elif hasattr(ms.MODEL_CONFIG, 'ALLOW_REASONING') and not ms.MODEL_CONFIG.ALLOW_REASONING:
|
| 306 |
+
reasoning_enabled = False
|
| 307 |
+
except Exception:
|
| 308 |
+
pass
|
| 309 |
+
# Also apply model-level disable
|
| 310 |
+
try:
|
| 311 |
+
ms = MODEL_STORAGE.get(request.model)
|
| 312 |
+
if ms and ms.MODEL_CONFIG and hasattr(ms.MODEL_CONFIG, 'ALLOW_REASONING') and not ms.MODEL_CONFIG.ALLOW_REASONING:
|
| 313 |
+
reasoning_enabled = False
|
| 314 |
+
except Exception:
|
| 315 |
+
pass
|
| 316 |
+
|
| 317 |
+
return {
|
| 318 |
+
'web_search_enabled': web_search_enabled,
|
| 319 |
+
'file_tool_enabled': file_tool_enabled,
|
| 320 |
+
'tools_enabled': tools_enabled,
|
| 321 |
+
'reasoning_enabled': reasoning_enabled,
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
|
| 325 |
|
| 326 |
# Move ChatCompletionRequest definition above fallback apply_model_tags_to_request
|
| 327 |
|
|
|
|
| 452 |
include_usage: Optional[bool] = Field(default=False)
|
| 453 |
stop: Optional[list[str]] = Field(["\n\n"])
|
| 454 |
stop_tokens: Optional[list[int]] = Field([0])
|
| 455 |
+
# Note: these defaults are intentionally None so the model may decide
|
| 456 |
+
# autonomously whether to use web search based on prompt detection unless
|
| 457 |
+
# the client explicitly sets flags. `auto_web_search` will be consulted
|
| 458 |
+
# when `enable_web_search` and `web_search` are None.
|
| 459 |
+
web_search: Optional[bool] = Field(default=None, description="Whether to perform a web search and append results to the prompt; if None, auto-detection is used")
|
| 460 |
+
enable_web_search: Optional[bool] = Field(default=None, description="Explicitly enable web search (overrides auto/web_search) if set; if None, auto-detection controls it")
|
| 461 |
auto_web_search: Optional[bool] = Field(default=True, description="Whether to enable web_search based on auto-detected intent")
|
| 462 |
enable_tools: Optional[bool] = Field(default=None, description="Explicitly enable tools (overrides auto detection)")
|
| 463 |
auto_tools: Optional[bool] = Field(default=True, description="Whether to enable tools based on auto-detected intent")
|
| 464 |
+
enable_reasoning: Optional[bool] = Field(default=None, description="Explicitly override reasoning enablement; if None, auto-detection controls it")
|
| 465 |
auto_reasoning: Optional[bool] = Field(default=True, description="Whether to enable reasoning based on auto detection")
|
| 466 |
enable_universal: Optional[bool] = Field(default=None, description="Explicitly enable the universal tool execution")
|
| 467 |
auto_universal: Optional[bool] = Field(default=True, description="Whether to auto enable universal tool execution")
|
|
|
|
| 665 |
|
| 666 |
createTimestamp = time.time()
|
| 667 |
|
| 668 |
+
# use global resolve_request_flags
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 669 |
|
| 670 |
def decide_file_tool_enabled(request):
|
| 671 |
+
return resolve_request_flags(request, detection)['file_tool_enabled']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
|
| 673 |
def decide_tools_enabled(request, detection):
|
| 674 |
+
return resolve_request_flags(request, detection)['tools_enabled']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 675 |
|
| 676 |
def decide_reasoning_enabled(request, detection, enableReasoning):
|
| 677 |
+
# resolve_request_flags ignores the enableReasoning baseline, so compute flags then
|
| 678 |
+
flags = resolve_request_flags(request, detection)
|
| 679 |
+
return flags['reasoning_enabled']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 680 |
|
| 681 |
def execute_tools(request, detection, prompt, executed_tool_calls, web_search_enabled, tools_enabled, file_tool_enabled, raw_prompt):
|
| 682 |
"""Helper to execute tools and update prompt and executed_tool_calls."""
|
|
|
|
| 757 |
detection = detect_tools_and_reasoning(raw_prompt)
|
| 758 |
prompt = raw_prompt if request.prompt is not None else f"{cleanMessages(request.messages or [])}\n\nAssistant:{' <think' if enableReasoning else ''}"
|
| 759 |
|
| 760 |
+
def decide_web_search_enabled(request, detection):
|
| 761 |
+
web_search_enabled = (
|
| 762 |
+
True
|
| 763 |
+
if (request.enable_web_search is not None and request.enable_web_search)
|
| 764 |
+
else (
|
| 765 |
+
request.web_search
|
| 766 |
+
or (request.auto_web_search if request.auto_web_search is not None else (getattr(CONFIG, 'AUTO_ENABLE_WEB_SEARCH', True) and detection.get('need_web_search')))
|
| 767 |
+
)
|
| 768 |
+
)
|
| 769 |
+
if not getattr(CONFIG, 'ENABLE_WEB_SEARCH_BY_DEFAULT', True) and request.enable_web_search is None and not (request.web_search or False):
|
| 770 |
+
web_search_enabled = False
|
| 771 |
+
try:
|
| 772 |
+
if request.sampler and getattr(request.sampler, 'ALLOW_WEB_SEARCH', None) is not None:
|
| 773 |
+
web_search_enabled = bool(request.sampler.ALLOW_WEB_SEARCH)
|
| 774 |
+
elif hasattr(request, 'sampler_allow_web_search') and request.sampler_allow_web_search is not None:
|
| 775 |
+
web_search_enabled = bool(request.sampler_allow_web_search)
|
| 776 |
+
else:
|
| 777 |
+
ms = MODEL_STORAGE.get(request.model)
|
| 778 |
+
if ms and ms.MODEL_CONFIG:
|
| 779 |
+
if hasattr(ms.MODEL_CONFIG, 'DEFAULT_SAMPLER') and getattr(ms.MODEL_CONFIG.DEFAULT_SAMPLER, 'ALLOW_WEB_SEARCH', None) is not None:
|
| 780 |
+
web_search_enabled = bool(ms.MODEL_CONFIG.DEFAULT_SAMPLER.ALLOW_WEB_SEARCH)
|
| 781 |
+
elif hasattr(ms.MODEL_CONFIG, 'ALLOW_WEB_SEARCH') and not ms.MODEL_CONFIG.ALLOW_WEB_SEARCH:
|
| 782 |
+
web_search_enabled = False
|
| 783 |
+
except Exception:
|
| 784 |
+
pass
|
| 785 |
+
return web_search_enabled
|
| 786 |
+
|
| 787 |
web_search_enabled = decide_web_search_enabled(request, detection)
|
| 788 |
file_tool_enabled = decide_file_tool_enabled(request)
|
| 789 |
tools_enabled = decide_tools_enabled(request, detection)
|
|
|
|
| 1018 |
# Intent detection and defaults: check whether to auto-enable tools, web_search, reasoning
|
| 1019 |
detection = detect_tools_and_reasoning(raw_prompt)
|
| 1020 |
|
| 1021 |
+
# Resolve flags via central helper (request defaults to None -> auto-detect)
|
| 1022 |
+
flags = resolve_request_flags(request, detection)
|
| 1023 |
+
web_search_enabled = flags['web_search_enabled']
|
| 1024 |
+
tools_enabled = flags['tools_enabled']
|
| 1025 |
+
file_tool_enabled = flags['file_tool_enabled']
|
| 1026 |
+
reasoning_enabled = flags['reasoning_enabled']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1027 |
enableReasoning = reasoning_enabled
|
| 1028 |
try:
|
| 1029 |
ms_cfg = MODEL_STORAGE.get(request.model)
|
|
|
|
| 1031 |
enableReasoning = False
|
| 1032 |
except Exception:
|
| 1033 |
pass
|
| 1034 |
+
# file_tool_enabled is derived from resolve_request_flags as well
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1035 |
# Build final prompt after deciding enableReasoning
|
| 1036 |
prompt = raw_prompt if request.prompt is not None else f"{cleanMessages(request.messages or [], enableReasoning)}\n\nAssistant:{' <think' if enableReasoning else ''}"
|
| 1037 |
|