One Open Source Project a Day (Part 61): knowledge_graph - Turn Any Text into a Knowledge Graph

Deep dive into knowledge_graph — extract concepts and relations from text with LLM, build knowledge graphs for GRAG (Graph Retrieval Augmented Generation), centrality analysis, community detection; local Mistral 7B + Ollama, zero API cost

·4 min read·AI & Machine Learning

Introduction

"Convert any text to a graph of knowledge. Graph Retrieval Augmented Generation (GRAG) — a new and improved version of RAG."

This is Part 61 of the "One Open Source Project a Day" series. Today's project is knowledge_graph (GitHub).

Want to turn documents and PDFs into queryable, visualizable knowledge graphs? knowledge_graph uses an LLM to extract concepts (not entities), build nodes and edges, and supports GRAG (Graph Retrieval Augmented Generation), centrality analysis, and community detection. No-GPT approach: local Mistral 7B + Ollama, zero API cost, Docker one-click run.

What You'll Learn

  • Knowledge graph construction pipeline
  • Concepts vs entities
  • Dual weights: W1 (semantic) and W2 (contextual proximity)
  • GRAG vs RAG
  • Local Ollama + Docker quick start

Prerequisites

  • Basic understanding of RAG and knowledge graphs
  • Docker or local Ollama (optional)

Project Background

Project Overview

knowledge_graph is rahulnyk's open-source text-to-knowledge-graph project. From PDFs and other text corpora, it uses an LLM to extract concepts (not entities): e.g., "Bangalore" is an entity, "Pleasant weather in Bangalore" is a concept. Concepts co-occurring in the same text chunk are treated as related. The resulting graph supports GRAG, centrality, community clustering, and more.

Author

  • Author: rahulnyk
  • Philosophy: Concepts are more meaningful than entities; local LLM = zero cost; extensible to graph databases

Project Data


Core Features

Pipeline

  1. Chunking: Split text into chunks, assign chunk_id
  2. Extract concepts & relations: LLM extracts concepts and semantic relations per chunk, weight W1
  3. Contextual proximity: Concepts in the same chunk are related, weight W2
  4. Merge edges: Merge weights and concatenate relations for same concept pairs
  5. Degree & communities: Node degree (sizing), communities (coloring)

Concepts vs Entities

TypeExampleDescription
EntityBangalore, doctorConcrete objects, names, places
ConceptPleasant weather in Bangalore, doctor–patient relationshipSituations, relations, abstractions
This projectExtracts conceptsAuthor: concepts yield more meaningful KG

Use Cases

  1. GRAG: Use graph as retriever for deeper document Q&A
  2. Centrality: Identify most important concepts in text
  3. Community detection: Cluster concepts, analyze topic structure
  4. Visualization: Pyvis for web-hostable interactive graphs

Quick Start

Docker (recommended):

git clone https://github.com/rahulnyk/knowledge_graph.git
cd knowledge_graph
docker build -t knowledge-graph .
docker run -p 8888:8888 knowledge-graph

Access Jupyter on port 8888.

Local Ollama:

  1. Install Ollama
  2. Run ollama run zephyr (or Mistral 7B OpenOrca)
  3. Edit extract_graph.ipynb with your text/PDF path and run

Core notebook: extract_graph.ipynb

Tech Stack

ComponentDescription
Mistral 7B OpenOrcaLLM for concept extraction, via Ollama
OllamaLocal model hosting, zero API cost
PandasGraph schema dataframes (can switch to graph DB later)
NetworkXGraph structure and algorithms
PyvisWeb-hostable JS graph visualization

Project Advantages

Comparisonknowledge_graphTraditional NER + REPure vector RAG
ExtractionConceptsEntitiesNo explicit structure
RelationsSemantic + contextualPredefined typesNone
CostLocal LLM, zero APIVariesEmbedding cost
RetrievalGraph (GRAG)Graph queryVector similarity
InterpretabilityHigh, traceable edgesMediumLow

Deep Dive

Dual Weight Design

  • W1: Semantic relations from LLM; multiple relations per concept pair
  • W2: Contextual proximity (same chunk)
  • Merge: Same concept pair → sum weights, concatenate relations

Suggested Improvements (community)

Backend:

  • Embeddings to deduplicate similar concepts ("doctor" vs "doctors")
  • Filter redundant/outlier concepts
  • Better contextual proximity to avoid overweighting frequent concepts

Frontend:

  • Expand by interest/topic
  • More useful graph browsing and navigation

Directory Structure

knowledge_graph/
├── extract_graph.ipynb   # Core extraction notebook
├── ner.ipynb            # NER related
├── data_input/           # Input
├── data_output/          # Output
├── helpers/              # Helpers
├── ollama/               # Ollama
├── dockerfile
└── pyproject.toml

Official Resources

Target Audience

  • Developers building knowledge graphs from documents
  • Teams exploring GRAG and graph retrieval
  • Users wanting zero-API-cost, local RAG/GRAG
  • Researchers interested in concept extraction and graph visualization

Visit my homepage for more useful knowledge and interesting products