Skip to content
AlexLecuPublic

About

Knowledge graph-augmented retrieval for evidence-based AMD information: LLM-built causal KG, Graph-RAG chatbot, LLM comparison (IEEE Access 2025)

Topics

Resources

Stars

37 stars

Watchers

2 watching

Forks

Latest commit

Β 

History

114 Commits

Folders and files

Repository files navigation

πŸ‘οΈ LLMKGraph

A Knowledge Graph-Augmented Retrieval System for Evidence-Based Age-Related Macular Degeneration Information

DOI Python 3.10–3.13 Docker CI License: MIT

🌐 Live Demo Β· πŸ’¬ Chatbot Β· πŸ“Š LLM Comparison Β· πŸŽ₯ Paper Overview Β· 🎬 Platform Demo Β· πŸ“„ Paper Β· πŸ“š Citation Β· πŸš€ Deploy

Official code for the paper "Reducing Hallucinations in Medical AI: A Knowledge Graph-Augmented Retrieval System for Evidence-Based Age-Related Macular Degeneration Information", IEEE Access 13, 210624–210639, 2025.

LLMKGraph system architecture

Try it at llmkgraph.utcluj.ro, hosted by the Technical University of Cluj-Napoca: browse the knowledge graph, ask the research assistant, compare the extraction LLMs, or extract relations from an abstract of your own into the Playground graph.

Three layers: LLM-based knowledge graph construction over AMD literature, a Graph-RAG chatbot grounded in that graph, and the web interfaces and evaluation suite built on top.


πŸ“– Overview

LLMKGraph turns the biomedical literature on Age-related Macular Degeneration (AMD) into a causal knowledge graph and uses that graph to ground a medical chatbot, so answers come with evidence instead of hallucinations. Large language models extract typed entities and relations from abstracts and clinical trials; an OWL ontology and a reasoner keep the graph consistent; a Graph-RAG retriever feeds the relevant sub-graph to the chat model at question time.

Component Description
🧠 Knowledge Graph Causal relations extracted from 5,000 AMD research abstracts (Dimensions.ai) and 100 clinical trials, stored in GraphDB (RDF) under the CausalAMD ontology, every relation linked to its source publication via prov:wasDerivedFrom
πŸ”¬ LLM Relation Extraction Fine-tuned GPT-3.5 Turbo and Mistral Nemo, GPT-4o-mini and DeepSeek extract 12 entity types and 8 relation types; entity disambiguation and HermiT reasoning clean the result
πŸ’¬ Graph-RAG Chatbot Research assistant over Weaviate: the question is analysed with spaCy, matching entities are found with BM25, the relations between and around them are retrieved, summarised, and handed to the LLM as grounded context

πŸ“ˆ Key Results

Open models from 1B to 70B parameters plus GPT-5-mini were evaluated on 3,000 questions (1-hop and 2-hop; multiple-choice, true/false and open-ended; 500 each) with and without knowledge-graph context. Accuracy on the 1-hop sets, computed from the result files in backend/evaluation/:

Model True/False, KG True/False, no KG MCQ, KG MCQ, no KG
SmolLM3-3B 95.6% 78.2% 89.2% 83.8%
Llama 3.2-3B 91.2% 80.4% 86.4% 66.2%
Mistral-7B 91.8% 90.6% 62.8% 44.6%
Qwen3-4B 90.6% 83.8% 91.2% 85.4%
Llama 3.1-8B 95.2% 93.4% 90.0% 86.2%
Llama 3.3-70B 97.6% 97.8% 94.0% 95.0%
GPT-5-mini 98.0% 97.2% 92.0% 95.6%

Graph context gives the largest gains to small models (up to +20 points on MCQ for Llama 3.2-3B), while 70B-class and frontier models gain little because their baseline is already high. Open-ended answers are scored with RAGAS (mean faithfulness 0.85–0.97 across models and hop depths); the chatbot was also run through Giskard LLM vulnerability scans.


🧱 Components at a Glance

Knowledge graph explorer

The graph explorer: browse and search the graphs, extract relations from an abstract, edit relations and run the reasoner.

Graph-grounded research assistant

The research assistant: every answer is written from relations retrieved from the knowledge graph.

Component Description Location
πŸ–₯️ Web Interface Vue 3 app: graph visualisation (vis-network), search and filtering, relation editing, reasoner, abstract upload, the chat page and the LLM-comparison page frontend/
🐍 Backend API Flask + Gunicorn REST API over GraphDB (search, add/delete relations, reasoning, extraction uploads, graph comparison); a read-only public mode with a writable Playground backend/app.py
πŸ”¬ Relation Extraction Prompted and fine-tuned LLM extractors (GPT, Mistral, DeepSeek) producing typed (entity1, relation, entity2) triples with provenance backend/bulk_relations.py, backend/prompts.py
🧹 Disambiguation & Reasoning Majority-vote entity typing, name normalisation, and OWL reasoning with HermiT (owlready2) pushed back into GraphDB backend/disambiguation/
πŸ“ CausalAMD Ontology OWL ontology with 12 entity classes and the causal object properties used by the graph data/CausalAMD_ont.rdf
πŸ’¬ Graph-RAG Chatbot spaCy query analysis, Weaviate BM25 entity search, pairwise and per-entity relation retrieval, LLM summarisation; served as a streaming HTTP API for the web UI or as a Chainlit app; Ollama or any OpenAI-compatible API backend/weaviate_rag/
πŸ” Vector / Graph Store Weaviate Entity, Relation, Publication collections (text2vec-transformers), ingested from GraphDB via SPARQL backend/weaviate_rag/create_schema.py, ingest_data.py
πŸ“Š Graph Comparison Compares the graphs built by different LLMs (size, degree distribution, clustering, relation and entity types, triple agreement): a page of the web UI backed by /api/evaluation, and the original Streamlit app backend/evaluation_stats.py, backend/streamlit/
πŸ“ Evaluation 3,000-question benchmark (Grok-generated, verified), RAGAS scoring, Giskard scans, per-model notebooks and results backend/evaluation/

πŸš€ Quick Start

Prerequisites

  • Docker and Docker Compose
  • An OpenAI API key (relation extraction); optional Mistral / DeepSeek / HuggingFace keys for the other extractors

Knowledge graph + web interface

# 1. Clone
git clone https://github.com/AlexLecu/LLMKGraph.git
cd LLMKGraph

# 2. Configure (.env is git-ignored; never commit real keys)
cp .env.example .env
# edit OPENAI_API_KEY and any other provider keys you want to use

# 3. Launch GraphDB + Flask API + Streamlit + Vue frontend
docker compose up --build
Service URL
Vue frontend http://localhost:8080
Flask API http://localhost:5555
GraphDB Workbench http://localhost:7200
Streamlit graph comparison http://localhost:8502

Then create a GraphDB repository named amd_repo (Workbench β†’ Setup β†’ Repositories) and populate it either by importing data/amd_repo.rdf (Workbench β†’ Import) or by uploading one of the extracted relation files from data/Extracted_Relations/ through the frontend's Relation Extraction and Upload panel.

Graph-RAG chatbot

The chatbot has its own stack: Weaviate for retrieval and an LLM for generation (local Ollama by default, or any OpenAI-compatible API such as Groq, Gemini, OpenRouter or Together).

cd backend/weaviate_rag

# 1. Weaviate + text2vec-transformers
docker compose -f docker-compose-weaviate.yml up -d

# 2. Python environment (3.10–3.13)
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt && python -m spacy download en_core_web_sm

# 3. Build the Weaviate collections from GraphDB
python create_schema.py && python ingest_data.py

# 4. Chat β€” http://localhost:8000
chainlit run chatbot.py -w
# or the HTTP API used by the web UI's Chat page (set VITE_CHAT_API_URL=http://localhost:8000)
uvicorn chat_api:app --port 8000

To use a hosted model instead of Ollama, set in .env:

CHAT_PROVIDER=openai-compatible
CHAT_API_BASE_URL=https://api.groq.com/openai/v1   # or Gemini / OpenRouter / Together
CHAT_API_KEY=...
CHAT_MODELS=openai/gpt-oss-120b,openai/gpt-oss-20b

πŸ’‘ All URLs and keys are read from environment variables; see .env.example (backend) and frontend/.env.example (frontend). The production setup behind llmkgraph.utcluj.ro is described in πŸ“„ DEPLOYMENT.md.

Manual setup (without Docker)

# Backend API (needs a running GraphDB and Java for the reasoner)
cd backend && python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
flask run -p 5555

# Graph comparison app
streamlit run streamlit/streamlit_app_evaluation.py --server.port 8502

# Frontend β€” http://localhost:5173
cd ../frontend && npm install && npm run dev

πŸ—‚οΈ Project Structure

LLMKGraph/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app.py                     # Flask REST API (search, relations, reasoning, uploads)
β”‚   β”œβ”€β”€ evaluation_stats.py        # Graph comparison behind /api/evaluation
β”‚   β”œβ”€β”€ prompts.py                 # Extraction + chat prompts (entity / relation vocabulary)
β”‚   β”œβ”€β”€ bulk_relations.py          # LLM relation extraction (GPT, Mistral, DeepSeek) + bulk KG load
β”‚   β”œβ”€β”€ enrich_kg_gpt.py           # Single-abstract extraction used by the UI
β”‚   β”œβ”€β”€ search_kg.py               # SPARQL search / delete helpers
β”‚   β”œβ”€β”€ disambiguation/            # Entity disambiguation + HermiT reasoning (owlready2)
β”‚   β”œβ”€β”€ weaviate_rag/              # Graph-RAG chatbot
β”‚   β”‚   β”œβ”€β”€ chat_core.py           # Shared chat logic (LLM backends, sanitising, retrieval)
β”‚   β”‚   β”œβ”€β”€ chat_api.py            # Streaming HTTP chat API used by the web UI
β”‚   β”‚   β”œβ”€β”€ chatbot.py             # Chainlit app (Ollama or OpenAI-compatible backends)
β”‚   β”‚   β”œβ”€β”€ rag_system.py          # BM25 entity retrieval, relation retrieval, LLM summary
β”‚   β”‚   β”œβ”€β”€ query_analyzer.py      # spaCy noun-phrase / keyword extraction
β”‚   β”‚   β”œβ”€β”€ create_schema.py       # Weaviate collections (Entity, Relation, Publication)
β”‚   β”‚   └── ingest_data.py         # GraphDB β†’ Weaviate ETL
β”‚   β”œβ”€β”€ streamlit/                 # Graph comparison app (+ legacy LangChain chat)
β”‚   └── evaluation/                # Benchmark datasets, notebooks, RAGAS / Giskard results
β”œβ”€β”€ frontend/                      # Vue 3 + Vite web interface
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ CausalAMD_ont.rdf          # OWL ontology
β”‚   β”œβ”€β”€ amd_repo.rdf               # Ready-to-import GraphDB repository
β”‚   β”œβ”€β”€ abstracts_with_id.json     # Sample of 100 abstracts (full corpus not redistributed)
β”‚   β”œβ”€β”€ Extracted_Relations/       # Relations extracted per model and date
β”‚   └── Annotators/                # Hand-annotated relation triples (two annotators)
β”œβ”€β”€ deploy/
β”‚   β”œβ”€β”€ server/                    # Production compose project + nginx (llmkgraph.utcluj.ro)
β”‚   └── chatbot/                   # Self-contained chatbot image (Weaviate + chat API)
β”œβ”€β”€ docker-compose.yml             # Local: GraphDB + API + Streamlit + frontend
└── DEPLOYMENT.md                  # How the public instance is deployed

πŸ“ Ontology and Extraction Schema

The CausalAMD ontology (data/CausalAMD_ont.rdf) models AMD knowledge as typed entities connected by causal and clinical relations. The extraction prompt restricts models to this vocabulary:

  • 🏷️ 12 entity types β€” disease, symptom, treatment, risk_factor, test, gene, biomarker, complication, prognosis, comorbidity, progression, body_part
  • πŸ”— 8 relation types β€” cause, treat, present, diagnose, aggravate, prevent, improve, affect
  • πŸ“Ž Provenance β€” every relation is a reified ont:RELATION node linked to its source abstract (Dimensions.ai publication id) or clinical trial (NCT number) via prov:wasDerivedFrom; the Weaviate Relation objects keep the same link through hasPublication
  • πŸ”„ Reasoning β€” the graph is exported from GraphDB, classified with HermiT through owlready2, and the inferred triples are written back (Reason KG in the UI)

πŸ§ͺ Evaluation Summary

πŸ“ Benchmark

  • 3,000 questions in backend/evaluation/data/grok_evaluation_datasets/: 1-hop and 2-hop Γ— multiple-choice, true/false and open-ended, 500 each; verified versions of the MCQ and true/false sets are included under verified/
  • Each question is answered with and without the retrieved graph context, by the same model, so the effect of grounding is isolated; the notebooks call the Graph-RAG pipeline (GraphRAGSystem) directly

πŸ€– Models

Result files in the repo cover Gemma 3 (1B, 4B, 12B), Llama 3.2-3B, Llama 3.1-8B, Llama 3.3-70B, Mistral-7B, Mixtral-8x7B, Qwen3-4B, SmolLM3-3B and GPT-5-mini. Per-model notebooks and CSV results live in backend/evaluation/evaluation_notebooks/evaluation_results/.

πŸ“Š Metrics

  • Accuracy for MCQ and true/false (context impact per question is also recorded)
  • RAGAS for open-ended answers: answer correctness, answer relevancy, faithfulness, context precision/recall
  • Giskard LLM vulnerability scans of the chatbot (Llama 3.2, Gemma 3, DeepSeek-R1); HTML reports in backend/evaluation/data/giskard_scan_results/
  • Graph-level comparison of KGs built by different extractors (node/edge counts, degree distribution, clustering coefficient, relation and entity types, triple agreement) on the LLM comparison page

πŸ“¦ Data

Resource Description Location
πŸ“š Abstracts A 100-abstract sample with Dimensions.ai publication ids; the full 5,000-abstract corpus used in the paper is not redistributed data/abstracts_with_id.json
πŸ”— Extracted relations Relations per extractor and run (GPT-3.5 FT, GPT-4o-mini, Mistral FT, DeepSeek V3 on abstracts and clinical trials) data/Extracted_Relations/
✍️ Annotations 67 abstracts annotated with entity spans and relations (Prodigy), relation triples from two independent annotators (163 each), and a 20-abstract evaluation set data/Annotators/, data/Evaluation/
❓ Benchmark 3,000 evaluation questions with verified answers backend/evaluation/data/
πŸ—„οΈ Knowledge graph Ready-to-import GraphDB repository + ontology data/amd_repo.rdf, data/CausalAMD_ont.rdf

πŸ› οΈ Technologies

Category Stack
🐍 Backend Python, Flask, Gunicorn, SPARQLWrapper, rdflib, owlready2 + HermiT, spaCy
πŸ’¬ Chat / UI apps Streaming chat API (FastAPI) and Chainlit for the Graph-RAG chatbot, Streamlit (local graph comparison)
πŸ–₯️ Frontend Vue 3, TypeScript, Vite, vis-network
πŸ—„οΈ Databases GraphDB 10.8 (RDF / SPARQL), Weaviate 1.28 (text2vec-transformers)
πŸ€– LLMs Extraction: fine-tuned GPT-3.5 Turbo and Mistral Nemo, GPT-4o-mini, DeepSeek Β· Chat: Ollama (Llama 3.2, DeepSeek-R1, Gemma 3) or any OpenAI-compatible API (the public instance uses gpt-oss-120b on Groq)
πŸ“ Evaluation RAGAS, Giskard, Jupyter

πŸ“š Citation

If you use this repository, the knowledge graph or the benchmark, please cite the paper:

@article{lecu2025llmkgraph,
  author  = {Lecu, Alexandru and Groza, Adrian and Hawizy, Lezan},
  title   = {Reducing Hallucinations in Medical AI: A Knowledge Graph-Augmented Retrieval
             System for Evidence-Based Age-Related Macular Degeneration Information},
  journal = {IEEE Access},
  year    = {2025},
  volume  = {13},
  pages   = {210624--210639},
  issn    = {2169-3536},
  doi     = {10.1109/ACCESS.2025.3643370},
  url     = {https://ieeexplore.ieee.org/document/11298209}
}

Machine-readable metadata is in CITATION.cff.


πŸ”’ Security & Dependencies

Dependencies are pinned with minimum versions at the patched releases flagged by GitHub security advisories; Dependabot watches the Python, npm, Docker and GitHub Actions manifests, and CI fails the frontend build on high-severity npm audit findings. Keep FLASK_DEBUG=0 and set CORS_ORIGINS to your frontend origin before exposing the API; the public instance runs with READ_ONLY=1 (see DEPLOYMENT.md).

πŸ“„ License

Code is released under the MIT License. The extracted relations and evaluation datasets in this repository are shared for research use; the source abstracts and clinical-trial texts remain subject to their publishers' terms.

πŸ“¬ Contact

Alexandru Lecu Β· Technical University of Cluj-Napoca Β· users.utcluj.ro/~alexlecu

About

Knowledge graph-augmented retrieval for evidence-based AMD information: LLM-built causal KG, Graph-RAG chatbot, LLM comparison (IEEE Access 2025)

Topics

Resources

Stars

37 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages