File size: 3,090 Bytes
8f9cecc
 
 
 
 
97dc066
 
8f9cecc
 
 
 
 
 
 
 
1124c64
8f9cecc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97dc066
8f9cecc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# summarizer.py
from typing import Callable
from crewai import Agent, Task, Crew, Process
from crewai_tools import tool
from langchain_community.vectorstores import Chroma
from langchain_openai import ChatOpenAI


class PatientChartSummarizer:
    """
    Builds a single manager-style agent that queries the patient chart
    (backed by Chroma) and produces a comprehensive markdown summary.
    """
    def __init__(self, vectordb: Chroma):
        self.vectordb = vectordb
        self.model = ChatOpenAI(model="gpt-4o", temperature=0)

        @tool("patient_chart_search")
        def patient_chart_search(query: str) -> str:
            """Search the patient chart embeddings for the given query."""
            results = self.vectordb.similarity_search(query, k=15)
            return "\n".join([res.page_content for res in results])

        self.summary_agent = Agent(
            role="Clinical Documentation Manager",
            goal=("Create a comprehensive, well-organized markdown summary of the entire patient chart."),
            verbose=True,
            memory=False,
            tools=[patient_chart_search],
            backstory=(
                "You coordinate information extraction for diagnoses, procedures, labs, vitals, and medications, "
                "then synthesize a clear, clinically relevant chart summary."
            ),
            llm=self.model
        )

        self.summary_task = Task(
            description=(
                "Search and compile a full patient chart summary in markdown.\n\n"
                "# Patient Chart Summary\n\n"
                "## Patient Demographics\n"
                "Include patient name, age, DOB, gender, MRN (if available)\n\n"
                "## Chief Complaint & History\n"
                "Extract reason for visit, chief complaint, relevant history\n\n"
                "## Diagnoses\n"
                "Organize as Primary/Secondary/Chronic/Past Medical History\n\n"
                "## Procedures & Interventions\n"
                "Chronological list with outcomes\n\n"
                "## Laboratory Results\n"
                "Organize by category; highlight abnormal findings\n\n"
                "## Vital Signs & Measurements\n"
                "Present trends and significant findings\n\n"
                "## Medications\n"
                "Current, discontinued, allergies & adverse reactions\n\n"
                "## Clinical Assessment & Plan\n"
                "Summarize assessment and plan sections\n\n"
                "## Key Clinical Findings\n"
                "Bullet key takeaways and recommendations\n"
            ),
            expected_output="Comprehensive patient chart summary in well-formatted markdown.",
            agent=self.summary_agent,
        )

        self.crew = Crew(
            agents=[self.summary_agent],
            tasks=[self.summary_task],
            process=Process.sequential,  # simple, deterministic flow
            verbose=True,
        )

    def summarize_chart(self) -> str:
        result = self.crew.kickoff()
        return str(result)