Spaces:
Running
Running
github-actions[bot] commited on
Commit ·
bafd639
1
Parent(s): 468b0c0
🚀 Auto-deploy backend from GitHub (a4680e0)
Browse files
main.py
CHANGED
|
@@ -1563,6 +1563,12 @@ _MATH_SCOPE_KEYWORDS: Set[str] = {
|
|
| 1563 |
"evaluate",
|
| 1564 |
"compute",
|
| 1565 |
"calculate",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1566 |
}
|
| 1567 |
|
| 1568 |
_MATH_SCOPE_PATTERNS: Tuple[re.Pattern[str], ...] = (
|
|
@@ -1687,7 +1693,19 @@ def _is_contextual_continuation_followup(message: str, history: Optional[Sequenc
|
|
| 1687 |
return any(pattern.search(latest_assistant_message) for pattern in _CONTINUATION_INVITE_PATTERNS)
|
| 1688 |
|
| 1689 |
|
| 1690 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1691 |
normalized = (message or "").strip().lower()
|
| 1692 |
if not normalized:
|
| 1693 |
return random.choice(_NON_MATH_REDIRECT_RESPONSES)
|
|
@@ -1695,6 +1713,10 @@ def _scope_boundary_response_without_continuation(message: str) -> Optional[str]
|
|
| 1695 |
if is_math_related_query(normalized):
|
| 1696 |
return None
|
| 1697 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1698 |
if _GREETING_PATTERN.search(normalized):
|
| 1699 |
return random.choice(_GREETING_RESPONSES)
|
| 1700 |
|
|
@@ -1705,7 +1727,7 @@ def _scope_boundary_response_without_continuation(message: str) -> Optional[str]
|
|
| 1705 |
|
| 1706 |
|
| 1707 |
def get_scope_boundary_response(message: str, history: Optional[Sequence[Any]] = None) -> Optional[str]:
|
| 1708 |
-
boundary_response = _scope_boundary_response_without_continuation(message)
|
| 1709 |
if boundary_response is None:
|
| 1710 |
return None
|
| 1711 |
|
|
@@ -1719,7 +1741,7 @@ def get_scope_boundary_response(message: str, history: Optional[Sequence[Any]] =
|
|
| 1719 |
if not reconstructed_intent:
|
| 1720 |
return _CONTINUATION_CONTEXT_CLARIFY_RESPONSE
|
| 1721 |
|
| 1722 |
-
reconstructed_boundary_response = _scope_boundary_response_without_continuation(reconstructed_intent)
|
| 1723 |
if reconstructed_boundary_response is None:
|
| 1724 |
return None
|
| 1725 |
|
|
@@ -1936,6 +1958,23 @@ async def health_check():
|
|
| 1936 |
}
|
| 1937 |
|
| 1938 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1939 |
@app.get("/")
|
| 1940 |
async def root():
|
| 1941 |
return {
|
|
|
|
| 1563 |
"evaluate",
|
| 1564 |
"compute",
|
| 1565 |
"calculate",
|
| 1566 |
+
"step by step",
|
| 1567 |
+
"real life",
|
| 1568 |
+
"example",
|
| 1569 |
+
"why",
|
| 1570 |
+
"how",
|
| 1571 |
+
"remember",
|
| 1572 |
}
|
| 1573 |
|
| 1574 |
_MATH_SCOPE_PATTERNS: Tuple[re.Pattern[str], ...] = (
|
|
|
|
| 1693 |
return any(pattern.search(latest_assistant_message) for pattern in _CONTINUATION_INVITE_PATTERNS)
|
| 1694 |
|
| 1695 |
|
| 1696 |
+
def _has_math_context_in_history(history: Optional[Sequence[Any]]) -> bool:
|
| 1697 |
+
if not history:
|
| 1698 |
+
return False
|
| 1699 |
+
latest_intent = _extract_latest_user_intent_message(history)
|
| 1700 |
+
if not latest_intent:
|
| 1701 |
+
return False
|
| 1702 |
+
return is_math_related_query(latest_intent)
|
| 1703 |
+
|
| 1704 |
+
|
| 1705 |
+
def _scope_boundary_response_without_continuation(
|
| 1706 |
+
message: str,
|
| 1707 |
+
history: Optional[Sequence[Any]] = None,
|
| 1708 |
+
) -> Optional[str]:
|
| 1709 |
normalized = (message or "").strip().lower()
|
| 1710 |
if not normalized:
|
| 1711 |
return random.choice(_NON_MATH_REDIRECT_RESPONSES)
|
|
|
|
| 1713 |
if is_math_related_query(normalized):
|
| 1714 |
return None
|
| 1715 |
|
| 1716 |
+
# Allow conversational follow-ups when recent history shows math context
|
| 1717 |
+
if _has_math_context_in_history(history):
|
| 1718 |
+
return None
|
| 1719 |
+
|
| 1720 |
if _GREETING_PATTERN.search(normalized):
|
| 1721 |
return random.choice(_GREETING_RESPONSES)
|
| 1722 |
|
|
|
|
| 1727 |
|
| 1728 |
|
| 1729 |
def get_scope_boundary_response(message: str, history: Optional[Sequence[Any]] = None) -> Optional[str]:
|
| 1730 |
+
boundary_response = _scope_boundary_response_without_continuation(message, history)
|
| 1731 |
if boundary_response is None:
|
| 1732 |
return None
|
| 1733 |
|
|
|
|
| 1741 |
if not reconstructed_intent:
|
| 1742 |
return _CONTINUATION_CONTEXT_CLARIFY_RESPONSE
|
| 1743 |
|
| 1744 |
+
reconstructed_boundary_response = _scope_boundary_response_without_continuation(reconstructed_intent, history)
|
| 1745 |
if reconstructed_boundary_response is None:
|
| 1746 |
return None
|
| 1747 |
|
|
|
|
| 1958 |
}
|
| 1959 |
|
| 1960 |
|
| 1961 |
+
@app.get("/debug/scope-info")
|
| 1962 |
+
async def debug_scope_info():
|
| 1963 |
+
"""Reveal what scope check code is deployed and its current state."""
|
| 1964 |
+
import subprocess
|
| 1965 |
+
try:
|
| 1966 |
+
git_hash = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=os.path.dirname(os.path.abspath(__file__))).decode().strip()
|
| 1967 |
+
except Exception:
|
| 1968 |
+
git_hash = "unknown"
|
| 1969 |
+
return {
|
| 1970 |
+
"git_commit": git_hash,
|
| 1971 |
+
"math_keywords": list(_MATH_SCOPE_KEYWORDS),
|
| 1972 |
+
"pattern_names": list(_MATH_SCOPE_PATTERNS.keys()),
|
| 1973 |
+
"has_history_check": "_has_math_context_in_history" in globals() or "_has_math_context_in_history" in dir(),
|
| 1974 |
+
"thanks_pattern": _THANKS_PATTERN.pattern if hasattr(_THANKS_PATTERN, 'pattern') else str(_THANKS_PATTERN),
|
| 1975 |
+
}
|
| 1976 |
+
|
| 1977 |
+
|
| 1978 |
@app.get("/")
|
| 1979 |
async def root():
|
| 1980 |
return {
|