Community Summarization & Reports
Relevant source files
The following files were used as context for generating this wiki page:
Community Summarization & Reports is a critical module in the Agentic GraphRAG system designed to generate high-level, thematic views of clustered medical data. After hierarchical Leiden partitioning of the knowledge graph, this module walks the community tree bottom-up to generate structured LLM reports for each cluster. These reports synthesize atomic entities and relations into clinical insights, providing the reasoning agent with a hierarchical summary of the graph's contents.
Sources: README.md:92-95, am_diag/graph_construction/community/summarize.py:75-80
Architecture and Data Flow
The summarization process is structured as a bottom-up traversal of the community hierarchy. At each level, the system builds a text-based context of the community's internal graph structure and uses an LLM to generate a report. These reports include titles, summaries, and structured findings.
The diagram above illustrates the sequential flow where communities are processed by level to ensure lower-level summaries are available for higher-level roll-ups.
Sources: am_diag/graph_construction/community/summarize.py:82-115
Context Construction
The build_community_context function transforms graph data into a structured string for the LLM. It includes:
- Entities: Lists entity names and their clinical labels (e.g., Disease, Drug).
- Relationships: Represents edges as directed statements (e.g.,
Head --Type--> Tail). - Sub-reports: At higher levels, summaries of child communities are injected to allow for information aggregation across the hierarchy.
Sources: am_diag/graph_construction/community/context.py:11-64
Component Overview
CommunitySummarizer
The CommunitySummarizer is a pure component responsible for orchestrating the generation of reports across all detected communities. It manages concurrency through semaphores to prevent LLM rate-limiting.
| Parameter | Type | Default | Description |
|---|---|---|---|
max_input_length | int | 8000 | Maximum characters for the input context. |
max_report_length | int | 2000 | Token budget for the generated report. |
max_concurrent | int | 10 | Maximum simultaneous LLM calls. |
Sources: am_diag/graph_construction/community/summarize.py:61-73
LLM Interface (BAML)
The system uses BAML to ensure type-safe outputs from the LLM. The GenerateCommunityReport function is configured with a system prompt that directs the model to focus on medical concepts and clinical significance.
# From am_diag/llm/baml_client/inlinedbaml.py:108-118
class CommunityReportOutput(BaseModel):
title: string
summary: string
findings: Finding[]
rating: float
rating_explanation: string
Sources: am_diag/llm/baml_client/inlinedbaml.py:108-144
Data Models
CommunityReport
The CommunityReport model stores the synthesized information. Unlike entities or relations, reports are not deduplicated; every pipeline run generates a new unique report for its associated community.
| Field | Type | Description |
|---|---|---|
community_id | UUID | Reference to the graph community node. |
title | str | Descriptive name for the community. |
summary | str | 2-3 sentence overview (Embeddable for vector search). |
findings | list[Finding] | Structured headlines and supporting evidence. |
rating | float | Clinical importance score (0.0-10.0). |
Sources: am_diag/common/data_models/community_report.py:13-39
Storage and Persistence
Reports are persisted in Neo4j as :CommunityReport nodes. The Neo4jClient manages the creation of these nodes and links them to their respective communities using the :HAS_REPORT relationship.
The ER diagram shows the 1:1 relationship between a community and its generated report within the Neo4j storage layer.
Sources: am_diag/db/graph/neo4j.py:270-285, README.md:65-72
Summary
The Community Summarization system transforms a raw medical knowledge graph into a structured hierarchy of clinical reports. By leveraging bottom-up summarization and type-safe LLM integration via BAML, it provides the RAG pipeline with high-level thematic context that is essential for answering broad medical queries.