Spaces:
Runtime error
Runtime error
Update chartdiagnosischecker.py
Browse files- chartdiagnosischecker.py +67 -0
chartdiagnosischecker.py
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from crewai import Agent, Task, Crew, Process
|
| 2 |
+
from langchain_openai import ChatOpenAI
|
| 3 |
+
|
| 4 |
+
class ChartDiagnosisChecker:
|
| 5 |
+
def __init__(self, pdf_dir: str, model: str = "gpt-4o"):
|
| 6 |
+
self.embed_manager = DirectoryEmbeddingManager(pdf_dir)
|
| 7 |
+
self.llm = ChatOpenAI(model=model, temperature=0)
|
| 8 |
+
|
| 9 |
+
self.agent = Agent(
|
| 10 |
+
role="Patient Chart Diagnosis Checker",
|
| 11 |
+
goal="Verify whether specific diagnoses are documented in the patient chart.",
|
| 12 |
+
backstory="You are a medical chart reviewer specialized in confirming diagnoses for HCC coding validation.",
|
| 13 |
+
verbose=True,
|
| 14 |
+
memory=False,
|
| 15 |
+
llm=self.llm,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
def check_one(self, diagnosis_entry: dict) -> dict:
|
| 19 |
+
"""Check if the patient has the given diagnosis (Yes/No)."""
|
| 20 |
+
diagnosis = diagnosis_entry["diagnosis"]
|
| 21 |
+
icd10 = diagnosis_entry["icd10"]
|
| 22 |
+
ref = diagnosis_entry.get("reference", "N/A")
|
| 23 |
+
|
| 24 |
+
# Build question
|
| 25 |
+
question = f"Does the patient have {diagnosis} (ICD-10: {icd10})?"
|
| 26 |
+
|
| 27 |
+
# Query patient chart embeddings
|
| 28 |
+
context = self.embed_manager.query(diagnosis, top_k=15)
|
| 29 |
+
|
| 30 |
+
task = Task(
|
| 31 |
+
description=(
|
| 32 |
+
f"Diagnosis to validate: {diagnosis} (ICD-10: {icd10})\n\n"
|
| 33 |
+
f"Reference: {ref}\n\n"
|
| 34 |
+
f"Patient chart excerpts:\n{context}\n\n"
|
| 35 |
+
f"Does the patient have {diagnosis} or anything related to this diagnosis"
|
| 36 |
+
"Answer Yes or No, with a short justification using ONLY the provided chart text.\n"
|
| 37 |
+
"Output must be valid JSON in the form:\n"
|
| 38 |
+
"{'answer': 'yes/no', 'rationale': 'one-line rationale'}"
|
| 39 |
+
),
|
| 40 |
+
expected_output="JSON with keys answer and rationale",
|
| 41 |
+
agent=self.agent,
|
| 42 |
+
json_mode=True,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
crew = Crew(agents=[self.agent], tasks=[task], process=Process.sequential, verbose=True)
|
| 46 |
+
result = crew.kickoff()
|
| 47 |
+
result = eval(repair_json(result))
|
| 48 |
+
|
| 49 |
+
# Enrich output
|
| 50 |
+
return {
|
| 51 |
+
"diagnosis": diagnosis,
|
| 52 |
+
"icd10": icd10,
|
| 53 |
+
"reference": ref,
|
| 54 |
+
"answer": result.get("answer", "unknown"),
|
| 55 |
+
"rationale": result.get("rationale", ""),
|
| 56 |
+
"context": context
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
def run(self, diagnoses: list[dict]) -> list[dict]:
|
| 60 |
+
"""Loop over all diagnoses and check them in the chart."""
|
| 61 |
+
results = []
|
| 62 |
+
for entry in diagnoses:
|
| 63 |
+
print(f"\n[INFO] Checking: {entry['diagnosis']} ({entry['icd10']})")
|
| 64 |
+
result = self.check_one(entry)
|
| 65 |
+
results.append(result)
|
| 66 |
+
print(f"[ANSWER] {result}")
|
| 67 |
+
return results
|