Skip to content

Commit 8609aa0

Browse files
committed
fix: handle revenue queries without origin template
- add revenue detector and guard
1 parent d441d5a commit 8609aa0

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

backend/api/routers/chat_router.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4547,18 +4547,58 @@ async def chat_with_rag(request: Request, chat_request: ChatRequest):
45474547
from backend.core.stillme_detector import (
45484548
detect_stillme_query,
45494549
get_foundational_query_variants,
4550-
detect_origin_query
4550+
detect_origin_query,
4551+
detect_revenue_query
45514552
)
45524553
# CRITICAL: Detect origin queries FIRST, before any other processing
45534554
# This ensures Identity Truth Override works even when RAG is disabled
45544555
is_origin_query, origin_keywords = detect_origin_query(chat_request.message)
45554556
if is_origin_query:
45564557
logger.debug(f"Origin query detected! Matched keywords: {origin_keywords}")
4558+
# Detect revenue/monetization questions to avoid origin template misuse
4559+
is_revenue_query = False
4560+
revenue_keywords = []
4561+
is_revenue_query, revenue_keywords = detect_revenue_query(chat_request.message)
4562+
if is_revenue_query:
4563+
logger.info(f"💰 Revenue query detected - will avoid origin template (matched: {revenue_keywords})")
45574564
except ImportError:
45584565
logger.warning("StillMe detector not available, skipping origin detection")
45594566
except Exception as detector_error:
45604567
logger.warning(f"StillMe detector error: {detector_error}")
45614568

4569+
# CRITICAL: Revenue query guard - return verified "no source" response (no origin template)
4570+
if 'is_revenue_query' in locals() and is_revenue_query:
4571+
try:
4572+
detected_lang = detected_lang or detect_language(chat_request.message)
4573+
except Exception as lang_error:
4574+
logger.warning(f"Language detection failed: {lang_error}, defaulting to 'vi'")
4575+
detected_lang = "vi"
4576+
4577+
if detected_lang == "vi":
4578+
revenue_response = (
4579+
"Mình không có thông tin đã được xác minh về doanh thu hoặc kế hoạch doanh thu của StillMe "
4580+
"trong Knowledge Base hiện tại, nên mình không thể khẳng định điều này.\n\n"
4581+
"Nếu bạn có nguồn cụ thể, bạn có thể chia sẻ để mình kiểm tra hoặc cập nhật.\n\n"
4582+
"Bạn có muốn chia sẻ nguồn hoặc mình trả lời theo giả định phổ biến không? (Có/Không)"
4583+
)
4584+
else:
4585+
revenue_response = (
4586+
"I don't have verified information about StillMe's revenue or revenue plans in the current "
4587+
"Knowledge Base, so I can't confirm this.\n\n"
4588+
"If you have a specific source, you can share it and I'll verify or update it.\n\n"
4589+
"Do you want to share a source or have me answer using common assumptions? (Yes/No)"
4590+
)
4591+
4592+
from backend.core.epistemic_state import EpistemicState
4593+
return ChatResponse(
4594+
response=revenue_response,
4595+
confidence_score=0.2,
4596+
processing_steps=["💰 Revenue query - no verified sources in KB"],
4597+
validation_info={},
4598+
timing={},
4599+
epistemic_state=EpistemicState.UNKNOWN.value
4600+
)
4601+
45624602
# CRITICAL: Identity Truth Override - If origin query, return SYSTEM_ORIGIN answer directly
45634603
# This MUST happen BEFORE any other early returns (honesty, AI_SELF_MODEL, philosophy, FPS)
45644604
if is_origin_query:

backend/core/stillme_detector.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,42 @@ def get_foundational_query_variants(query: str) -> List[str]:
610610
}
611611

612612

613+
def detect_revenue_query(query: str) -> Tuple[bool, List[str]]:
614+
"""
615+
Detect if query is about revenue/monetization/business model.
616+
617+
Args:
618+
query: User query string
619+
620+
Returns:
621+
Tuple of (is_revenue_query, matched_keywords)
622+
"""
623+
if not query:
624+
return (False, [])
625+
query_lower = query.lower()
626+
matched_keywords = []
627+
628+
revenue_patterns = [
629+
r"\bdoanh\s+thu\b",
630+
r"\brevenue\b",
631+
r"\bmonetiz\w*\b",
632+
r"\bkinh\s+doanh\b",
633+
r"\bkiếm\s+tiền\b",
634+
r"\bthu\s+nhập\b",
635+
r"\bprofit\b",
636+
r"\bmô\s+hình\s+doanh\s+thu\b",
637+
r"\bbusiness\s+model\b",
638+
r"\bmô\s+hình\s+kinh\s+doanh\b",
639+
r"\bkế\s+hoạch\s+doanh\s+thu\b",
640+
]
641+
for pattern in revenue_patterns:
642+
if re.search(pattern, query_lower):
643+
matched_keywords.append(pattern)
644+
return (True, matched_keywords)
645+
646+
return (False, [])
647+
648+
613649
def detect_origin_query(query: str) -> Tuple[bool, List[str]]:
614650
"""
615651
Detect if query is about StillMe's origin/founder.
@@ -623,6 +659,26 @@ def detect_origin_query(query: str) -> Tuple[bool, List[str]]:
623659
"""
624660
query_lower = query.lower()
625661
matched_keywords = []
662+
663+
# CRITICAL: Exclude revenue/monetization questions from origin detection
664+
# These are business status questions, not origin/founder identity queries
665+
revenue_exclusion_patterns = [
666+
r"\bdoanh\s+thu\b",
667+
r"\brevenue\b",
668+
r"\bmonetiz\w*\b",
669+
r"\bkinh\s+doanh\b",
670+
r"\bkiếm\s+tiền\b",
671+
r"\bthu\s+nhập\b",
672+
r"\bprofit\b",
673+
r"\bmô\s+hình\s+doanh\s+thu\b",
674+
r"\bbusiness\s+model\b",
675+
r"\bmô\s+hình\s+kinh\s+doanh\b",
676+
r"\bkế\s+hoạch\s+doanh\s+thu\b",
677+
]
678+
for pattern in revenue_exclusion_patterns:
679+
if re.search(pattern, query_lower):
680+
logger.debug(f"Origin query excluded due to revenue pattern: {pattern}")
681+
return (False, [])
626682

627683
# CRITICAL: EXCLUDE philosophical/learning/evolution questions from origin detection FIRST
628684
# These questions are about StillMe's learning mechanism, self-reference, evolution, NOT about origin/founder

0 commit comments

Comments
 (0)