# mrrezaeiuoft/amg-rag architecture

> Architecture diagram of the GitHub repository mrrezaeiuoft/amg-rag, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mrrezaeiuoft/amg-rag
- Repository: https://github.com/mrrezaeiuoft/amg-rag

## Overview

#### 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`](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py) (Entity enrichment), [`create_VDB.py`](https://github.com/mrrezaeiuoft/amg-rag/blob/main/create_VDB.py) (Vector DB creation), [`Simple_AMG_RAG.py`](https://github.com/mrrezaeiuoft/amg-rag/blob/main/Simple_AMG_RAG.py) (Simple AMG-RAG)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_qa["QA Workflow"]
  node_input["Question records<br/>input<br/>[AMG-with-KG.py]"]
  node_amg["AMG_RAG_System<br/>orchestrator<br/>[AMG-with-KG.py]"]
  node_entities["Entity extraction<br/>pipeline stage<br/>[AMG-with-KG.py]"]
  node_enrichment["Entity enrichment<br/>pipeline stage<br/>[AMG-with-KG.py]"]
  node_reasoning["Graph-aware reasoning<br/>pipeline stage<br/>[AMG-with-KG.py]"]
  node_synthesis["Answer synthesis<br/>generation stage<br/>[AMG-with-KG.py]"]
  node_output["Answer result<br/>output<br/>[AMG-with-KG.py]"]
  node_simple_pipeline["Simple AMG-RAG<br/>alternative pipeline<br/>[Simple_AMG_RAG.py]"]
end

subgraph group_evidence["Evidence Sources"]
  node_pubmed{{"PubMed<br/>external API<br/>[AMG-with-KG.py]"}}
  node_wikipedia{{"Wikipedia<br/>external source<br/>[AMG-with-KG.py]"}}
  node_vector_search[("Vector search<br/>semantic retrieval<br/>[AMG-with-KG.py]")]
  node_vdb_builder["Vector DB creation<br/>index builder<br/>[create_VDB.py]"]
end

subgraph group_state["Knowledge State"]
  node_knowledge_graph[("Medical knowledge graph<br/>NetworkX graph<br/>[AMG-with-KG.py]")]
end

subgraph group_models["Model Runtime"]
  node_embeddings["Hugging Face embeddings<br/>embedding runtime<br/>[AMG-with-KG.py]"]
  node_llm{{"OpenAI or Ollama LLM<br/>inference provider<br/>[AMG-with-KG.py]"}}
  node_agent_runtime["LangChain and LangGraph<br/>workflow runtime<br/>[AMG-with-KG.py]"]
end

node_input -->|"questions"| node_amg
node_amg -->|"runs workflow"| node_agent_runtime
node_amg -->|"extracts"| node_entities
node_entities -->|"concepts"| node_enrichment
node_enrichment -->|"literature lookup"| node_pubmed
node_enrichment -.->|"context lookup"| node_wikipedia
node_enrichment -->|"semantic context"| node_vector_search
node_enrichment -->|"updates entities and facts"| node_knowledge_graph
node_pubmed -->|"evidence"| node_knowledge_graph
node_wikipedia -.->|"supplementary facts"| node_knowledge_graph
node_vector_search -->|"retrieved context"| node_knowledge_graph
node_knowledge_graph -->|"entity-relation paths"| node_reasoning
node_vector_search -->|"semantic evidence"| node_reasoning
node_pubmed -->|"literature evidence"| node_reasoning
node_reasoning -->|"grounded reasoning"| node_synthesis
node_synthesis -->|"answer and explanation"| node_output
node_llm -->|"generates"| node_synthesis
node_embeddings -->|"query embeddings"| node_vector_search
node_embeddings -->|"indexes documents"| node_vdb_builder
node_vdb_builder -->|"builds index"| node_vector_search
node_simple_pipeline -.->|"alternative QA flow"| node_output

click node_input "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_amg "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_entities "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_enrichment "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_reasoning "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_synthesis "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_output "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_knowledge_graph "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_pubmed "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_wikipedia "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_vector_search "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_vdb_builder "https://github.com/mrrezaeiuoft/amg-rag/blob/main/create_VDB.py"
click node_embeddings "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_llm "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_agent_runtime "https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py"
click node_simple_pipeline "https://github.com/mrrezaeiuoft/amg-rag/blob/main/Simple_AMG_RAG.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_input,node_amg,node_entities,node_enrichment,node_reasoning,node_synthesis,node_output,node_simple_pipeline toneBlue
class node_pubmed,node_wikipedia,node_vector_search,node_vdb_builder toneAmber
class node_knowledge_graph toneMint
class node_embeddings,node_llm,node_agent_runtime toneRose
```

## Components

### QA Workflow

- [Question records](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): input · Medical questions with answer options, including JSONL-style batch records. · `AMG-with-KG.py`
- [AMG_RAG_System](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): orchestrator · Primary agentic medical QA implementation. · `AMG-with-KG.py`
- [Entity extraction](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): pipeline stage · Finds medical concepts and assigns relevance and confidence. · `AMG-with-KG.py`
- [Entity enrichment](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): pipeline stage · Adds current evidence and contextual descriptions to extracted entities. · `AMG-with-KG.py`
- [Graph-aware reasoning](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): pipeline stage · Traverses relation paths and combines evidence with confidence propagation. · `AMG-with-KG.py`
- [Answer synthesis](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): generation stage · Selects an answer option and produces confidence and explanation. · `AMG-with-KG.py`
- [Answer result](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): output · Answer, confidence, and human-readable explanation. · `AMG-with-KG.py`
- [Simple AMG-RAG](https://github.com/mrrezaeiuoft/amg-rag/blob/main/Simple_AMG_RAG.py): alternative pipeline · Reduced QA pipeline implementation. · `Simple_AMG_RAG.py`

### Evidence Sources

- [PubMed](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): external API · Current medical literature retrieval; API key is optional. · `AMG-with-KG.py`
- [Wikipedia](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): external source · Supplementary context and retrieval fallback. · `AMG-with-KG.py`
- [Vector search](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): semantic retrieval · Retrieves embedded medical context from the vector database. · `AMG-with-KG.py`
- [Vector DB creation](https://github.com/mrrezaeiuoft/amg-rag/blob/main/create_VDB.py): index builder · Prepares the vector database separately from QA execution. · `create_VDB.py`

### Knowledge State

- [Medical knowledge graph](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): NetworkX graph · Mutable entities and typed, confidence-scored relationships. · `AMG-with-KG.py`

### Model Runtime

- [Hugging Face embeddings](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): embedding runtime · `AMG-with-KG.py`
- [OpenAI or Ollama LLM](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): inference provider · Selectable cloud or local answer-generation runtime. · `AMG-with-KG.py`
- [LangChain and LangGraph](https://github.com/mrrezaeiuoft/amg-rag/blob/main/AMG-with-KG.py): workflow runtime · Agent and workflow substrate for the primary pipeline. · `AMG-with-KG.py`

## Connections

- 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
