How supermedintel/medical-graph-rag works
Project Type and Purpose
• This is a specialized graph Retrieval-Augmented Generation (RAG) system for the medical domain.
• It leverages a multi-agent pipeline (inherited largely from the CAMEL framework) to integrate language models (LLMs) and graph data – specifically, it constructs and queries graphs built on datasets such as MIMIC, MedC-K, and UMLS.
• The project supports two modes: a simple baseline RAG implementation and a full-scale “build from scratch” Graph RAG pipeline that constructs the hierarchy of graphs and augments LLM inference through retrieval.
Analyzing the File Structure
• At a high level, the repository contains several standalone scripts (run.py, creat_graph.py, cleangraph.py) and configuration files (medgraphrag.yml, Dockerfile, etc.) used for deployment.
• A very large “camel” directory indicates that the system relies on a robust modular framework. This directory is organized with submodules for agents, configurations, embeddings, interpreters, loaders, memories, models, retrievers, storages, toolkits, and a workforce that orchestrates multi-agent behavior.
• There is also a smaller “nano_graphrag” module that likely provides a simplified or alternative pipeline for graph tasks.
• Other files (MedicalBook.xlsx, git_push.sh, etc.) indicate data sources and deployment automation scripts.
Architectural Components to Capture
a. Data Ingestion and Pre-Processing:
• Components: dataloader.py, loaders (in camel/loaders) and data_chunk.py.
• Purpose: Read and pre-process heterogeneous data sources including patient records (MIMIC), medical textbooks, and UMLS–like dictionaries.
b. Graph Construction:
• Components: creat_graph.py, cleangraph.py, nanographrag modules; and parts from storages (especially camel/storages/graph_storages and key_value_storages).
• Purpose: Convert raw text data into structured graph representations, supporting multi-grained levels (top-level, medium-level, and bottom-level).
c. Graph Storage and Database Integration:
• Components: Modules interacting with Neo4j are indicated in the README’s environment variables (NEO4J_URL, etc.), as well as relevant code under storages (e.g., neo4j_graph.py).
• Purpose: Persist and manage the knowledge graphs so they can be queried during inference.
d. Retrieval and Querying:
• Components: Camel retrievers (auto_retriever.py, bm25_retriever.py, vector_retriever.py) and memory blocks (chat_history_block.py, vectordb_block.py).
• Purpose: Efficiently search and extract relevant information from the stored graphs using various retrieval strategies.
e. LLM and Model Inference:
• Components: Modules in camel/models (e.g., openai_model.py, mistral_model.py) and prompt handling via camel/prompts.
• Purpose: Process natural language queries, generate responses, and augment answers using LLMs. The integration with external APIs (OpenAI, ChatGPT, etc.) is also evident.
f. Agent and Multi-Agent Orchestration:
• Components: Many files under camel/agents and workforce (agentic_chunker.py, workforce modules, task_agent.py etc.).
• Purpose: Coordinate different agents that handle task execution, role assignment, search, deduction, and tools orchestration, making the system a dynamic multi-agent pipeline.
g. Toolkits and External Integrations:
• Components: A variety of toolkits (google_maps_toolkit, github_toolkit, search_toolkit, etc.) and open API specs.
• Purpose: Extend the system with external functionalities (e.g., leveraging public APIs or additional tools), which is especially useful when processing or accessing non-local data.