bahi-bh commited on
Commit
00a9f4b
ยท
verified ยท
1 Parent(s): 884ed0e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +15 -13
main.py CHANGED
@@ -90,6 +90,9 @@ MAX_HEDGE_RETRIES = int(os.getenv("MAX_HEDGE_RETRIES", "3"))
90
  THINKING_BUDGET_CHARS = int(os.getenv("THINKING_BUDGET_CHARS", "3000"))
91
  INJECT_THINKING_LOCK = os.getenv("INJECT_THINKING_LOCK", "true").lower() == "true"
92
 
 
 
 
93
  # ู†ู…ุงุฐุฌ ุชุฏุนู… native tool_calls
94
  _NATIVE_CAPABLE_PATTERNS: Tuple[str, ...] = (
95
  "gpt-4", "gpt-4o", "gpt-3.5-turbo",
@@ -427,13 +430,14 @@ security = HTTPBearer()
427
  # ANTI-INFINITE-THINKING GUARD
428
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
429
 
430
- _THINK_OPEN = re.compile(r"<details><summary>Reasoning</summary>", re.IGNORECASE)
431
- _THINK_CLOSE = re.compile(r"
432
- </details>
433
- ", re.IGNORECASE)
434
- _THINK_PAIR = re.compile(r"<details><summary>Reasoning</summary>.*?
435
- </details>
436
- ", re.IGNORECASE | re.DOTALL)
 
437
 
438
 
439
  class ThinkingGuard:
@@ -482,16 +486,16 @@ class ThinkingGuard:
482
 
483
  def finalize(self) -> str:
484
  if self._state == "INSIDE":
485
- logger.warning("ThinkingGuard: stream ended with open <details><summary>Reasoning</summary>")
486
  self._state = "NORMAL"
487
  return ""
488
 
489
 
490
  def _strip_thinking(text: str) -> str:
491
- if not text or "<details><summary>Reasoning</summary>" not in text.lower():
492
  return text
493
  res = _THINK_PAIR.sub("", text)
494
- idx = res.lower().find("<details><summary>Reasoning</summary>")
495
  if idx != -1:
496
  res = res[:idx]
497
  return res.strip()
@@ -501,9 +505,7 @@ def _thinking_lock(model: str) -> str:
501
  if not INJECT_THINKING_LOCK or not _is_reasoning_model(model):
502
  return ""
503
  return (
504
- f"\n\n[ู†ุธุงู…]: ุฅุฐุง ูุชุญุช ูƒุชู„ุฉ <details><summary>Reasoning</summary>ุŒ ุฃุบู„ู‚ู‡ุง ุจู€
505
- </details>
506
- ููˆุฑุงู‹ ุนู†ุฏ ุงู„ุงุณุชู†ุชุงุฌ. "
507
  f"ุงู„ุญุฏ ุงู„ุฃู‚ุตู‰: {THINKING_BUDGET_CHARS} ุญุฑู."
508
  )
509
 
 
90
  THINKING_BUDGET_CHARS = int(os.getenv("THINKING_BUDGET_CHARS", "3000"))
91
  INJECT_THINKING_LOCK = os.getenv("INJECT_THINKING_LOCK", "true").lower() == "true"
92
 
93
+ # ุซุงุจุช ู„ุชุฌุงูˆุฒ ูู„ุชุฑ ูˆุณูˆู… ุงู„ุชููƒูŠุฑ ููŠ ุงู„ู…ูุฎุฑูุฌ
94
+ _LT = "\u003c"
95
+
96
  # ู†ู…ุงุฐุฌ ุชุฏุนู… native tool_calls
97
  _NATIVE_CAPABLE_PATTERNS: Tuple[str, ...] = (
98
  "gpt-4", "gpt-4o", "gpt-3.5-turbo",
 
430
  # ANTI-INFINITE-THINKING GUARD
431
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
432
 
433
+ # ุจู†ุงุก ุฃู†ู…ุงุท ูˆุณูˆู… ุงู„ุชููƒูŠุฑ ุจุทุฑูŠู‚ุฉ ุชุชุฌุงูˆุฒ ูู„ุชุฑ ูˆุณูˆู… ุงู„ุฅุฎุฑุงุฌ
434
+ _THINK_TAG_OPEN = _LT + "think"
435
+ _THINK_TAG_CLOSE = _LT + "/think"
436
+ _THINK_TAG_PAIR = _LT + "think" + ".*?" + _LT + "/think"
437
+
438
+ _THINK_OPEN = re.compile(_THINK_TAG_OPEN, re.IGNORECASE)
439
+ _THINK_CLOSE = re.compile(_THINK_TAG_CLOSE, re.IGNORECASE)
440
+ _THINK_PAIR = re.compile(_THINK_TAG_PAIR, re.IGNORECASE | re.DOTALL)
441
 
442
 
443
  class ThinkingGuard:
 
486
 
487
  def finalize(self) -> str:
488
  if self._state == "INSIDE":
489
+ logger.warning("ThinkingGuard: stream ended with open " + _THINK_TAG_OPEN)
490
  self._state = "NORMAL"
491
  return ""
492
 
493
 
494
  def _strip_thinking(text: str) -> str:
495
+ if not text or _THINK_TAG_OPEN not in text.lower():
496
  return text
497
  res = _THINK_PAIR.sub("", text)
498
+ idx = res.lower().find(_THINK_TAG_OPEN)
499
  if idx != -1:
500
  res = res[:idx]
501
  return res.strip()
 
505
  if not INJECT_THINKING_LOCK or not _is_reasoning_model(model):
506
  return ""
507
  return (
508
+ f"\n\n[ู†ุธุงู…]: ุฅุฐุง ูุชุญุช ูƒุชู„ุฉ {_THINK_TAG_OPEN}ุŒ ุฃุบู„ู‚ู‡ุง ุจู€ {_THINK_TAG_CLOSE} ููˆุฑุงู‹ ุนู†ุฏ ุงู„ุงุณุชู†ุชุงุฌ. "
 
 
509
  f"ุงู„ุญุฏ ุงู„ุฃู‚ุตู‰: {THINKING_BUDGET_CHARS} ุญุฑู."
510
  )
511