Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(partners): ignore negated and quoted authoring phrases

Bingxi Zhao (Frank)21205dac3a84
diff --git a/deeptutor/capabilities/partner_authoring/binding.py b/deeptutor/capabilities/partner_authoring/binding.py
index bad8c87a1..9f8c8d34c 100644
--- a/deeptutor/capabilities/partner_authoring/binding.py
+++ b/deeptutor/capabilities/partner_authoring/binding.py
@@ -30,6 +30,18 @@ _AUTHORING_REQUEST = re.compile(
rf"{_ZH_ACTION}{_ZH_GAP}{_ZH_OBJECT}|{_EN_ACTION}{_EN_DETERMINER}{_EN_GAP}{_EN_OBJECT}",
re.IGNORECASE,
)
+_QUOTED_OR_CODE = re.compile(
+ r"```[\s\S]*?```|~~~[\s\S]*?~~~"
+ r"|(?m:^>[^\n]*)|`[^`\n]*`"
+ r'|"[^"\n]*"|“[^”\n]*”|「[^」\n]*」|『[^』\n]*』'
+ r"|(?<!\w)'[^'\n]+'(?!\w)"
+)
+_CLAUSE_END = re.compile(r"[。!?!?;;,,\n]")
+_NEGATED_ACTION = re.compile(
+ r"(?:\b(?:don't|do\s+not|never|not|can't|cannot)\s*(?:want|need)?(?:\s+(?:you\s+)?to)?\s*"
+ r"|(?:不|别|不要|不用|无需|不必|不想(?:让你)?|不需要)\s*)$",
+ re.IGNORECASE,
+)
def is_partner_authoring_turn(context: UnifiedContext) -> bool:
@@ -41,7 +53,20 @@ def is_partner_authoring_turn(context: UnifiedContext) -> bool:
if context.active_capability == PARTNER_AUTHORING_CAPABILITY_NAME:
return True
text = str(context.user_message or "")
- return _AUTHORING_REQUEST.search(text) is not None
+ quoted = [match.span() for match in _QUOTED_OR_CODE.finditer(text)]
+ for match in _AUTHORING_REQUEST.finditer(text):
+ # Mentioning a request inside a quotation or code sample is not an
+ # instruction. The action may still be outside quotes around a name.
+ if any(start <= match.start() and match.end() <= end for start, end in quoted):
+ continue
+ clause_start = max(
+ (boundary.end() for boundary in _CLAUSE_END.finditer(text, 0, match.start())),
+ default=0,
+ )
+ if _NEGATED_ACTION.search(text[clause_start : match.start()]):
+ continue
+ return True
+ return False
__all__ = ["PARTNER_AUTHORING_CAPABILITY_NAME", "is_partner_authoring_turn"]
diff --git a/tests/capabilities/test_partner_authoring_capability.py b/tests/capabilities/test_partner_authoring_capability.py
index c25ed42ef..fc7c74fa4 100644
--- a/tests/capabilities/test_partner_authoring_capability.py
+++ b/tests/capabilities/test_partner_authoring_capability.py
@@ -35,6 +35,9 @@ def test_partner_authoring_activation_requires_action_and_partner_object() -> No
"Set up a new study buddy for physics",
"我想要一个数学导师",
"来个学习搭子",
+ 'Create a tutor named "Ada"',
+ "I don't want a coach; create a tutor instead",
+ "请不要创建一个伙伴,请帮我创建一个数学导师",
],
)
def test_partner_authoring_activates_on_a_creation_request(message: str) -> None:
@@ -52,12 +55,30 @@ def test_partner_authoring_activates_on_a_creation_request(message: str) -> None
"我想提交一个关于伙伴功能的 bug",
"生成的回答里提到了伙伴",
"我需要解释一下,为什么会触发伙伴创建",
+ "I don't want a tutor",
+ "I do not need a partner",
+ "Please don't create a partner",
+ "I don't want you to create a tutor",
+ "我不想要一个伙伴",
+ "请不要创建一个伙伴",
+ "我不想让你创建一个伙伴",
+ 'Why does "create a Partner" trigger here?',
+ "The issue title is 'create a Partner'",
+ "请解释“创建一个伙伴”为什么会误触发",
+ "```text\ncreate a Partner\n```",
+ "> create a Partner\nWhy did this trigger?",
],
)
def test_partner_authoring_ignores_turns_that_only_mention_a_partner(message: str) -> None:
assert not is_partner_authoring_turn(_context(message))
+def test_explicit_partner_authoring_selection_still_routes_the_turn() -> None:
+ context = _context("I don't want a tutor")
+ context.active_capability = "partner_authoring"
+ assert is_partner_authoring_turn(context)
+
+
def test_capability_forces_a_draft_before_finishing() -> None:
capability = PartnerAuthoringCapability()
context = _context("创建一个伙伴")