How mrrezaeiuoft/amg-rag works
Purpose and Entry Points
AMG-RAG is a Python medical QA framework that builds and updates an in-memory medical knowledge graph while grounding answers in retrieved evidence. The primary implementation is AMG-with-KG.py, exposing the AMG_RAG_System class and its question-answering flow. Simple_AMG_RAG.py is a reduced alternative pipeline. create_VDB.py provides vector-database creation utilities.
System Orchestration
AMG-with-KG.py is the architectural center: it initializes model providers, retrieval clients, graph state, and the agentic QA workflow. Input is a medical question with multiple-choice options; output includes an answer, confidence, and explanation. The README’s import name differs from the hyphenated filename, so module-loading/renaming may require attention.
Paths: AMG-with-KG.py, Simple_AMG_RAG.py
Entity Extraction and Enrichment
The pipeline extracts medically relevant entities from the question, assigns relevance/confidence scores, and produces contextual descriptions. Entities are enriched with current external evidence before or during graph incorporation, making the graph question-driven rather than a fixed curated artifact.
Paths: AMG-with-KG.py
Knowledge Graph Construction and Updates
The system maintains a medical knowledge graph containing entities and typed, confidence-scored relationships. It analyzes relationships in both directions and supports graph traversal for downstream reasoning. New evidence and extracted concepts can update the graph at query time, forming the primary mutable-state boundary.
NetworkX is the stated graph runtime.
Paths: AMG-with-KG.py
Evidence Retrieval Boundary
Retrieval combines three evidence sources: PubMed for current literature, Wikipedia for supplementary context/fallback, and semantic retrieval against a vector database. PubMed is an external HTTP/API dependency and may use an optional API key. Vector search depends on embeddings from Hugging Face Transformers and optional LangChain Chroma integrations; database preparation is separated into create_VDB.py.
Paths: AMG-with-KG.py, create_VDB.py
Reasoning and Answer Synthesis
A graph-aware reasoning stage explores relevant entity/relation paths, integrates retrieved literature and vector context, and propagates confidence through the evidence chain. The final generation stage selects an answer option and returns a human-readable explanation. LangChain and LangGraph provide the agent/workflow substrate.
Paths: AMG-with-KG.py
Model and Runtime Boundaries
The repository targets Python 3.8+. LLM inference is selectable: OpenAI via API key or local Ollama. Environment-provided credentials include OPENAI_API_KEY and optionally pubmed_api; secrets are expected outside source control. Core external dependencies are LangChain/LangGraph, OpenAI or Ollama, Hugging Face embeddings, PubMed, Wikipedia, and NetworkX.
Paths: AMG-with-KG.py, README.md
Operational Data Flow
Question JSONL-style records enter the system; entity extraction and evidence retrieval generate contextual facts; these facts populate/update the knowledge graph; graph and retrieval evidence feed structured reasoning; answer synthesis emits answer, confidence, and explanation. Batch dataset/result directories are described in documentation but are not present in the supplied repository tree, so scripts should not assume they exist.
Paths: AMG-with-KG.py, create_VDB.py, Simple_AMG_RAG.py
Main entry points: AMG-with-KG.py Entity enrichment · create_VDB.py Vector DB creation · Simple_AMG_RAG.py Simple AMG-RAG
Components 16
QA Workflow
- Question records · Medical questions with answer options, including JSONL-style batch records.
AMG-with-KG.py - AMG_RAG_System · Primary agentic medical QA implementation.
AMG-with-KG.py - Entity extraction · Finds medical concepts and assigns relevance and confidence.
AMG-with-KG.py - Entity enrichment · Adds current evidence and contextual descriptions to extracted entities.
AMG-with-KG.py - Graph-aware reasoning · Traverses relation paths and combines evidence with confidence propagation.
AMG-with-KG.py - Answer synthesis · Selects an answer option and produces confidence and explanation.
AMG-with-KG.py - Answer result · Answer, confidence, and human-readable explanation.
AMG-with-KG.py - Simple AMG-RAG · Reduced QA pipeline implementation.
Simple_AMG_RAG.py
Evidence Sources
- PubMed · Current medical literature retrieval; API key is optional.
AMG-with-KG.py - Wikipedia · Supplementary context and retrieval fallback.
AMG-with-KG.py - Vector search · Retrieves embedded medical context from the vector database.
AMG-with-KG.py - Vector DB creation · Prepares the vector database separately from QA execution.
create_VDB.py
Knowledge State
- Medical knowledge graph · Mutable entities and typed, confidence-scored relationships.
AMG-with-KG.py
Model Runtime
- Hugging Face embeddings
AMG-with-KG.py - OpenAI or Ollama LLM · Selectable cloud or local answer-generation runtime.
AMG-with-KG.py - LangChain and LangGraph · Agent and workflow substrate for the primary pipeline.
AMG-with-KG.py
Connections 21
- Question records → AMG_RAG_System: questions
- AMG_RAG_System → LangChain and LangGraph: runs workflow
- AMG_RAG_System → Entity extraction: extracts
- Entity extraction → Entity enrichment: concepts
- Entity enrichment → PubMed: literature lookup
- Entity enrichment → Wikipedia: context lookup
- Entity enrichment → Vector search: semantic context
- Entity enrichment → Medical knowledge graph: updates entities and facts
- PubMed → Medical knowledge graph: evidence
- Wikipedia → Medical knowledge graph: supplementary facts
- Vector search → Medical knowledge graph: retrieved context
- Medical knowledge graph → Graph-aware reasoning: entity-relation paths
- Vector search → Graph-aware reasoning: semantic evidence
- PubMed → Graph-aware reasoning: literature evidence
- Graph-aware reasoning → Answer synthesis: grounded reasoning
- Answer synthesis → Answer result: answer and explanation
- OpenAI or Ollama LLM → Answer synthesis: generates
- Hugging Face embeddings → Vector search: query embeddings
- Hugging Face embeddings → Vector DB creation: indexes documents
- Vector DB creation → Vector search: builds index
- Simple AMG-RAG → Answer result: alternative QA flow