Drug-Safety Agent on Databricks · End to end · The rules in order
The rules, in order
The order of the rules is the design. Refusals come first because they are the cases where a mistake costs most. A request to file something is checked before a request for advice, which is checked before a question about cause, which is checked before a request for a person.
An example of why. "Send the lisinopril angioedema report to the FDA" mentions a drug and an event and looks like an investigation. Because the filing rule is first, it is refused as a filing request and never reaches a model.
Tested as a table. Each rule has rows in a test table: the phrasings from the specification first, then tricky ones. A new rule means a new row, not a new test function.
The real code
Cut from the repository each time this site is built, so it always matches what runs. Each header names the file and the line it was cut from.
src/drugsafety/router.py:47The patterns, most dangerous first_SUBMISSION = re.compile(
r"\b(send|submit|file|forward|email|report)\b.*\b(fda|ema|regulator|regulatory|mah|marketing authori[sz]ation|"
r"company|manufacturer|patient)\b"
)
_ADVICE = re.compile(
r"\b(should|can|could|may)\s+(my|i|we|he|she|they|the patient)\b.*\b(stop|start|take|taking|add|change|switch|skip)\b"
r"|\b(stop|keep|start)\s+taking\b|\bis it safe (to|for)\b|\bsafe to (add|take|combine)\b"
)
_CAUSALITY = re.compile(
r"\b(does|do|did|can|could|will|would)\b.*\bcaus(e|es|ed|ing)\b"
r"|\bis\b.*\bcaused by\b|\bcan we say\b.*\bsafe\b|\bis\b.*\bsafe for\b|\bproven?\b.*\b(cause|link)\b"
)
_HUMAN = re.compile(src/drugsafety/router.py:84The ordered rule table_RULES: tuple[tuple[re.Pattern[str], str, str, str | None], ...] = (
(_SUBMISSION, "submission_request", "refusal", "R-SUBMISSION"),
(_ADVICE, "advice_request", "refusal", "R-ADVICE"),
(_CAUSALITY, "causality_question", "refusal", "R-CAUSALITY"),
(_HUMAN, "human", "handoff", None),
)tests/test_router.py:6The test table: request, intent, who answers, refusal, drug, eventROUTES = [
# (request, intent, answered_by, refusal, drug, event)
("Is there a signal for lisinopril and angioedema?", "investigate", "loop", None, "lisinopril", "ANGIOEDEMA"),
("Assess amiodarone and pulmonary toxicity", "investigate", "loop", None, "amiodarone", "PULMONARY TOXICITY"),
("Investigate warfarin with gastrointestinal haemorrhage", "investigate", "loop", None, "warfarin", "GASTROINTESTINAL HAEMORRHAGE"),
("How many angioedema reports for lisinopril?", "counts_only", "direct", None, "lisinopril", "ANGIOEDEMA"),
("Show the counts for metformin and lactic acidosis", "counts_only", "direct", None, "metformin", "LACTIC ACIDOSIS"),
("Is angioedema on the lisinopril label?", "label_only", "direct", None, "lisinopril", "ANGIOEDEMA"),
("Does the metformin label warn about lactic acidosis?", "label_only", "direct", None, "metformin", "LACTIC ACIDOSIS"),
("Does the lisinopril label already mention angioedema?", "label_only", "direct", None, "lisinopril", "ANGIOEDEMA"),
("I want to talk to a person", "human", "handoff", None, None, None),
("Get the safety lead", "human", "handoff", None, None, None),
("Does lisinopril cause angioedema?", "causality_question", "refusal", "R-CAUSALITY", "lisinopril", "ANGIOEDEMA"),
("Can we say metformin is safe for the kidneys?", "causality_question", "refusal", "R-CAUSALITY", "metformin", None),