RareDx / src /agent.py
sarra2002's picture
Démo RareDx
613af80 verified
Raw History Blame Contribute Delete
3.88 kB
"""Agent v2: ask the doctor for the findings or lab results that best separate the top hypotheses."""
from __future__ import annotations
import numpy as np
from .system import DiagnosticSystem
class QuestionAgent:
def __init__(self, system: DiagnosticSystem, n_questions: int = 5, head: int = 10, window: int = 20,
confirm_weight: float = 1.0, exclude_weight: float = 0.5, lab_bonus: float = 1.5,
min_ic: float = 2.0):
self.system, self.kb, self.rag = system, system.kb, system.rag
self.n_questions, self.head, self.window = n_questions, head, window
self.confirm_weight, self.exclude_weight = confirm_weight, exclude_weight # λ and μ
self.lab_bonus, self.min_ic = lab_bonus, min_ic
self.choose_questions = system.tracer.wrap(self.choose_questions, "agent : choix des questions")
self.update = system.tracer.wrap(self.update, "agent : mise à jour après réponses")
def choose_questions(self, ranking: list[str], codes: list[str]) -> list[str]:
"""Findings with the largest rank-weighted variance across the top diseases (labs get a bonus)."""
top = ranking[:self.head]
if not top:
return []
p = np.array([1 / (1 + r) for r in range(1, len(top) + 1)])
p /= p.sum()
weights = [self.kb.extended_weights(g) for g in top]
known = self.kb.expand(codes)
notes = {}
for t in set().union(*weights) - known:
if self.rag.ic.get(t, 0) < self.min_ic: # too general
continue
v = np.array([w.get(t, 0) for w in weights])
note = (p * (v - (p * v).sum()) ** 2).sum()
notes[t] = note * (self.lab_bonus if self.kb.is_metabolic(t) else 1)
chosen: list[str] = []
for t in sorted(notes, key=lambda t: (-notes[t], t)): # ties broken by code: reproducible
if all(t not in self.kb.ancestors(c) and c not in self.kb.ancestors(t) for c in chosen):
chosen.append(t)
if len(chosen) == self.n_questions:
break
return chosen
def rerank(self, ranking: list[str], confirmed: list[str], excluded: list[str]) -> list[str]:
scored = []
for r, g in enumerate(ranking[:self.window], 1):
w, s = self.kb.extended_weights(g), 1 / (1 + r)
for c in confirmed:
s *= 1 + self.confirm_weight * w.get(c, 0)
for c in excluded:
s *= 1 - self.exclude_weight * w.get(c, 0)
scored.append((s, g))
scored.sort(key=lambda x: -x[0])
return [g for _, g in scored] + ranking[self.window:]
def update(self, codes: list[str], llm_sets: list[set[str]], confirmed: list[str], excluded: list[str]) -> list[str]:
"""Add the confirmed findings, re-run the RAG, fuse again (no new LLM call), then rerank."""
codes2 = list(dict.fromkeys(codes + confirmed))
ranking = self.system.fuse(self.system.search_rag(codes2), llm_sets, codes2)
return self.rerank(ranking, confirmed, excluded)
def read_answers(self, questions: list[str], answers: dict[int, str]) -> tuple[list[str], list[str]]:
"""Doctor's answers: "non" / "no", "?" (unknown), or a precise result in free text."""
confirmed, excluded = [], []
for i, q in enumerate(questions, 1):
a = str(answers.get(i, "?")).strip()
if a.lower() in ("?", "je ne sais pas", "unknown", ""):
continue
if a.lower() in ("non", "no"):
excluded.append(q)
continue
precise = [r[0] for p in self.system.llm.extract_symptoms(a) if (r := self.system.linker.to_hpo(p))]
confirmed += [c for c in (precise or [q]) if c not in confirmed]
return confirmed, excluded