Update main.py
Browse files
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 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
"
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
|
|
|
| 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
|
| 486 |
self._state = "NORMAL"
|
| 487 |
return ""
|
| 488 |
|
| 489 |
|
| 490 |
def _strip_thinking(text: str) -> str:
|
| 491 |
-
if not text or
|
| 492 |
return text
|
| 493 |
res = _THINK_PAIR.sub("", text)
|
| 494 |
-
idx = res.lower().find(
|
| 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[ูุธุงู
]: ุฅุฐุง ูุชุญุช ูุชูุฉ
|
| 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 |
|