Add read-only production runtime audit
This commit is contained in:
@@ -11,6 +11,7 @@ from rag.clinical import (
|
||||
ConditionNormalizer,
|
||||
ConditionQuery,
|
||||
ConditionRelation,
|
||||
HepaticContext,
|
||||
PatientContext,
|
||||
RenalContext,
|
||||
)
|
||||
@@ -20,6 +21,8 @@ from rag.understanding import (
|
||||
LlmQueryUnderstander,
|
||||
QueryFrame,
|
||||
_apply_condition_candidate_cue,
|
||||
_apply_contextual_candidate_safety,
|
||||
_apply_general_condition_scope,
|
||||
_apply_named_drug_cues,
|
||||
_apply_reverse_relation_cues,
|
||||
_merge_with_prior_frame,
|
||||
@@ -72,9 +75,104 @@ def test_condition_normalizer_handles_professional_aliases_without_drug_mapping(
|
||||
assert normalizer.normalize("THA dùng gì", "THA").normalized_condition == "tăng huyết áp"
|
||||
assert normalizer.normalize("cao huyết áp", "cao huyết áp").normalized_condition == "tăng huyết áp"
|
||||
assert normalizer.normalize("Gout", "gout").normalized_condition == "gút"
|
||||
assert normalizer.detect_known_alias("BN viêm phổi dùng thuốc gì?").normalized_condition == "viêm phổi"
|
||||
assert normalizer.normalize("bệnh lạ", "bệnh lạ").normalized_condition == "bệnh lạ"
|
||||
|
||||
|
||||
def test_explicit_indication_relation_is_a_condition_candidate_lookup():
|
||||
noisy = QueryFrame(
|
||||
turn_type="condition_relation",
|
||||
condition=ConditionNormalizer().normalize("bệnh gút", "gút"),
|
||||
needs_clarify=True,
|
||||
clarify_reason="Hỏi lại sai hướng",
|
||||
)
|
||||
|
||||
frame = _apply_condition_candidate_cue(
|
||||
noisy,
|
||||
"Thuốc nào có chỉ định liên quan bệnh gút?",
|
||||
ConditionNormalizer(),
|
||||
)
|
||||
|
||||
assert frame.turn_type == "condition_to_drug"
|
||||
assert frame.condition_relation == ConditionRelation.INDICATION
|
||||
assert frame.needs_clarify is False
|
||||
|
||||
|
||||
def test_general_condition_does_not_invent_patient_hepatic_context():
|
||||
frame = QueryFrame(
|
||||
turn_type="condition_to_drug",
|
||||
indication="viêm gan B mạn",
|
||||
condition=ConditionQuery(
|
||||
original_query="Viêm gan B mạn dùng thuốc gì?",
|
||||
normalized_condition="viêm gan B mạn",
|
||||
),
|
||||
patient_context=PatientContext(
|
||||
primary_condition="viêm gan B mạn",
|
||||
hepatic=HepaticContext(description="viêm gan B mạn"),
|
||||
),
|
||||
)
|
||||
|
||||
cleaned = _apply_general_condition_scope(
|
||||
frame, "Viêm gan B mạn dùng thuốc gì?"
|
||||
)
|
||||
|
||||
assert cleaned.patient_context.primary_condition == "viêm gan B mạn"
|
||||
assert cleaned.patient_context.requires_safety_review is False
|
||||
|
||||
|
||||
def test_patient_allergy_condition_lookup_keeps_safety_context():
|
||||
patient = PatientContext(
|
||||
primary_condition="viêm phổi", allergies=("penicillin",)
|
||||
)
|
||||
frame = QueryFrame(
|
||||
turn_type="condition_to_drug",
|
||||
condition=ConditionQuery(
|
||||
original_query="BN dị ứng penicillin, viêm phổi dùng thuốc gì?",
|
||||
normalized_condition="viêm phổi",
|
||||
),
|
||||
patient_context=patient,
|
||||
)
|
||||
|
||||
kept = _apply_general_condition_scope(
|
||||
frame, "BN dị ứng penicillin, viêm phổi dùng thuốc gì?"
|
||||
)
|
||||
|
||||
assert kept.patient_context == patient
|
||||
assert kept.patient_context.requires_safety_review is True
|
||||
|
||||
|
||||
def test_candidate_safety_followup_stays_on_prior_condition_lookup():
|
||||
prior = QueryFrame(
|
||||
turn_type="condition_to_drug",
|
||||
indication="tăng huyết áp",
|
||||
condition=ConditionQuery(
|
||||
original_query="BN bị tăng huyết áp",
|
||||
normalized_condition="tăng huyết áp",
|
||||
),
|
||||
patient_context=PatientContext(
|
||||
age_text="68 tuổi",
|
||||
renal=RenalContext(description="CKD", ckd_stage="G4"),
|
||||
),
|
||||
)
|
||||
noisy = QueryFrame(
|
||||
turn_type="condition_relation",
|
||||
condition_relation=ConditionRelation.CONTRAINDICATION,
|
||||
depends_on_previous_turn=True,
|
||||
patient_context=prior.patient_context,
|
||||
needs_clarify=False,
|
||||
)
|
||||
|
||||
corrected = _apply_contextual_candidate_safety(
|
||||
noisy,
|
||||
"Trong các thuốc trên cái nào cần lưu ý hơn với bệnh thận?",
|
||||
prior,
|
||||
)
|
||||
|
||||
assert corrected.turn_type == "condition_to_drug"
|
||||
assert corrected.condition == prior.condition
|
||||
assert corrected.condition_relation == ConditionRelation.INDICATION
|
||||
|
||||
|
||||
def test_broad_condition_is_clarified_but_specific_subtype_is_not():
|
||||
normalizer = ConditionNormalizer()
|
||||
broad = normalizer.normalize("Viêm gan dùng thuốc gì?", "viêm gan")
|
||||
|
||||
Reference in New Issue
Block a user