How robert-mcdermott/ai-knowledge-graph works
Purpose and entry points
This is a Python 3.11+ CLI pipeline that converts an unstructured text file into an LLM-extracted subject–predicate–object (SPO) knowledge graph, then emits raw graph JSON and an interactive HTML visualization.
Primary launch surfaces:
• Direct-script wrapper: generate-graph.py
• Installable/module CLI wrapper: src/generate_graph.py
• Orchestration and argument handling: src/knowledge_graph/main.py
Configuration and execution boundary
Runtime behavior is driven by TOML configuration plus CLI overrides, including LLM endpoint/model settings, chunking, entity standardization, and inference switches.
• Default repository configuration: config.toml
• Loading/validation boundary: src/knowledge_graph/config.py
• Package metadata and console-script packaging: pyproject.toml
Input is a local plaintext document; output is a local HTML file plus a JSON graph export. The included example corpus is data/industrial-revolution.txt.
Phase 1: chunking and SPO extraction
The orchestrator reads the input document, splits it into overlapping word-based chunks, and processes each chunk independently. This controls LLM context size while retaining nearby context across chunk boundaries.
• Text preparation/chunking: src/knowledge_graph/text_utils.py
• Extraction orchestration: src/knowledge_graph/main.py
• LLM client and response/JSON extraction: src/knowledge_graph/llm.py
Each extraction result is normalized into triples that form the initial graph. Debug mode exposes raw LLM responses and extracted JSON.
LLM integration and prompt contract
The system targets any OpenAI-compatible chat-completions endpoint, including local or hosted providers such as Ollama, LM Studio, OpenAI, vLLM, and LiteLLM-backed services. The endpoint URL, API key, model, token limit, and temperature are runtime configuration rather than hardwired infrastructure.
Prompt responsibilities are separated by stage:
• Extraction prompts: src/knowledge_graph/prompts/main_prompts.py
• Entity-resolution prompts: src/knowledge_graph/prompts/entity_prompts.py
• Relationship-inference prompts: src/knowledge_graph/prompts/inference_prompts.py
Phase 2: entity standardization
Optional standardization consolidates equivalent entity labels across independently extracted chunks. It performs basic text normalization and can invoke the LLM to identify ambiguous aliases or concept groups, then rewrites triples to their selected canonical entity forms.
• Standardization algorithms and LLM-assisted resolution: src/knowledge_graph/entity_standardization.py
• Stage control and graph rewrite coordination: src/knowledge_graph/main.py
This stage is a semantic cleanup boundary before graph-level inference; --no-standardize bypasses it.
Phase 3: relationship inference
Optional inference enriches the standardized graph through multiple mechanisms: graph-community analysis, LLM-proposed links between disconnected or intra-community entities, transitive rules, lexical similarity, and triple deduplication.
• Inference coordination: src/knowledge_graph/main.py
• LLM transport/structured-response handling: src/knowledge_graph/llm.py
• Inference prompt definitions: src/knowledge_graph/prompts/inference_prompts.py
It is independently disabled by configuration or --no-inference, allowing extraction-only graphs and reduced LLM usage.
Phase 4: graph analytics and HTML rendering
The final triple set is converted to a NetworkX/PyVis-oriented graph representation. Rendering calculates centrality-derived node importance and Louvain communities, distinguishes original from inferred edges, and writes a self-contained interactive visualization with theme and controls.
• Graph analytics, PyVis construction, HTML output: src/knowledge_graph/visualization.py
• Visualization HTML base: src/knowledge_graph/templates/graph_template.html
• Final invocation/output statistics: src/knowledge_graph/main.py
The generated HTML is browser-consumable without an application server; docs/index.html represents the published demo artifact.
Main entry points: main.py SPO extraction · entity_standardization.py Entity standardization · visualization.py Graph analytics & renderer · llm.py OpenAI-compatible LLM client
Components 20
CLI & Runtime
- Direct-script wrapper
generate-graph.py - Installable CLI wrapper
src/generate_graph.py - Pipeline orchestrator · Parses arguments and coordinates all pipeline stages.
src/knowledge_graph/main.py - Config loader
src/knowledge_graph/config.py - Default configuration · Supplies LLM, chunking, and stage settings.
config.toml - Package metadata · Defines console-script installation.
pyproject.toml
Knowledge Graph Pipeline
- Input text document
data/industrial-revolution.txt - Text chunking · Bounds LLM context while preserving local context.
src/knowledge_graph/text_utils.py - Extraction prompts
src/knowledge_graph/prompts/main_prompts.py - OpenAI-compatible LLM client · Handles structured responses and JSON extraction.
src/knowledge_graph/llm.py - SPO extraction · Normalizes LLM output into subject–predicate–object triples.
src/knowledge_graph/main.py - Entity standardization · Optional alias resolution consolidates entities across chunks.
src/knowledge_graph/entity_standardization.py - Entity-resolution prompts
src/knowledge_graph/prompts/entity_prompts.py - Relationship inference · Optionally adds inferred links and deduplicates triples.
src/knowledge_graph/main.py - Inference prompts
src/knowledge_graph/prompts/inference_prompts.py
Graph Delivery
- Graph analytics & renderer · Computes importance and communities, then emits interactive HTML.
src/knowledge_graph/visualization.py - Visualization template
src/knowledge_graph/templates/graph_template.html - Raw graph JSON
- Interactive HTML output · Browser-consumable without an application server.
- Published demo artifact
docs/index.html
Connections 22
- Direct-script wrapper → Pipeline orchestrator: launches
- Installable CLI wrapper → Pipeline orchestrator: launches
- Package metadata → Installable CLI wrapper: installs
- Default configuration → Config loader: loads
- Config loader → Pipeline orchestrator: runtime settings
- Input text document → Text chunking: text
- Pipeline orchestrator → Text chunking: coordinates
- Text chunking → SPO extraction: overlapping chunks
- Extraction prompts → SPO extraction: instructions
- SPO extraction → OpenAI-compatible LLM client: chat requests
- OpenAI-compatible LLM client → SPO extraction: structured responses
- SPO extraction → Entity standardization: initial triples
- Entity-resolution prompts → Entity standardization: alias resolution
- Entity standardization → OpenAI-compatible LLM client: ambiguous aliases
- Entity standardization → Relationship inference: canonical triples
- Inference prompts → Relationship inference: link proposals
- Relationship inference → OpenAI-compatible LLM client: optional proposals
- Relationship inference → Graph analytics & renderer: final triples
- Visualization template → Graph analytics & renderer: HTML base
- Graph analytics & renderer → Raw graph JSON: exports
- Graph analytics & renderer → Interactive HTML output: renders
- Interactive HTML output → Published demo artifact: published example