# robert-mcdermott/ai-knowledge-graph architecture

> Architecture diagram of the GitHub repository robert-mcdermott/ai-knowledge-graph, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/robert-mcdermott/ai-knowledge-graph
- Repository: https://github.com/robert-mcdermott/ai-knowledge-graph

## Overview

#### 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: [`src/knowledge_graph/main.py`](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/main.py) (SPO extraction), [`src/knowledge_graph/entity_standardization.py`](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/entity_standardization.py) (Entity standardization), [`src/knowledge_graph/visualization.py`](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/visualization.py) (Graph analytics & renderer), [`src/knowledge_graph/llm.py`](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/llm.py) (OpenAI-compatible LLM client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["CLI & Runtime"]
  node_script_wrapper["Direct-script wrapper<br/>Python entry point<br/>[generate-graph.py]"]
  node_module_wrapper["Installable CLI wrapper<br/>Python entry point<br/>[generate_graph.py]"]
  node_main["Pipeline orchestrator<br/>CLI orchestration<br/>[main.py]"]
  node_config["Config loader<br/>TOML validation<br/>[config.py]"]
  node_default_config["Default configuration<br/>TOML<br/>[config.toml]"]
  node_package["Package metadata<br/>Python packaging<br/>[pyproject.toml]"]
end

subgraph group_pipeline["Knowledge Graph Pipeline"]
  node_input["Input text document<br/>plaintext corpus"]
  node_chunking["Text chunking<br/>overlapping word chunks<br/>[text_utils.py]"]
  node_extraction_prompts["Extraction prompts<br/>prompt contract<br/>[main_prompts.py]"]
  node_llm{{"OpenAI-compatible LLM client<br/>chat-completions transport<br/>[llm.py]"}}
  node_extraction["SPO extraction<br/>per-chunk extraction<br/>[main.py]"]
  node_standardization["Entity standardization<br/>canonical entity rewrite"]
  node_entity_prompts["Entity-resolution prompts<br/>prompt contract<br/>[entity_prompts.py]"]
  node_inference["Relationship inference<br/>graph enrichment<br/>[main.py]"]
  node_inference_prompts["Inference prompts<br/>prompt contract"]
end

subgraph group_delivery["Graph Delivery"]
  node_visualization["Graph analytics & renderer<br/>NetworkX / PyVis<br/>[visualization.py]"]
  node_template["Visualization template<br/>HTML template"]
  node_graph_json["Raw graph JSON<br/>graph export"]
  node_html_output["Interactive HTML output<br/>self-contained visualization"]
  node_demo["Published demo artifact<br/>HTML artifact<br/>[index.html]"]
end

node_script_wrapper -->|"launches"| node_main
node_module_wrapper -->|"launches"| node_main
node_package -->|"installs"| node_module_wrapper
node_default_config -->|"loads"| node_config
node_config -->|"runtime settings"| node_main
node_input -->|"text"| node_chunking
node_main -->|"coordinates"| node_chunking
node_chunking -->|"overlapping chunks"| node_extraction
node_extraction_prompts -->|"instructions"| node_extraction
node_extraction -->|"chat requests"| node_llm
node_llm -->|"structured responses"| node_extraction
node_extraction -->|"initial triples"| node_standardization
node_entity_prompts -->|"alias resolution"| node_standardization
node_standardization -.->|"ambiguous aliases"| node_llm
node_standardization -->|"canonical triples"| node_inference
node_inference_prompts -->|"link proposals"| node_inference
node_inference -.->|"optional proposals"| node_llm
node_inference -->|"final triples"| node_visualization
node_template -->|"HTML base"| node_visualization
node_visualization -->|"exports"| node_graph_json
node_visualization -->|"renders"| node_html_output
node_html_output -.->|"published example"| node_demo

click node_script_wrapper "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/generate-graph.py"
click node_module_wrapper "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/generate_graph.py"
click node_main "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/main.py"
click node_config "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/config.py"
click node_default_config "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/config.toml"
click node_package "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/pyproject.toml"
click node_input "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/data/industrial-revolution.txt"
click node_chunking "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/text_utils.py"
click node_extraction_prompts "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/prompts/main_prompts.py"
click node_llm "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/llm.py"
click node_extraction "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/main.py"
click node_standardization "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/entity_standardization.py"
click node_entity_prompts "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/prompts/entity_prompts.py"
click node_inference "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/main.py"
click node_inference_prompts "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/prompts/inference_prompts.py"
click node_visualization "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/visualization.py"
click node_template "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/templates/graph_template.html"
click node_demo "https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/docs/index.html"

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_script_wrapper,node_module_wrapper,node_main,node_config,node_default_config,node_package toneBlue
class node_input,node_chunking,node_extraction_prompts,node_llm,node_extraction,node_standardization,node_entity_prompts,node_inference,node_inference_prompts toneAmber
class node_visualization,node_template,node_graph_json,node_html_output,node_demo toneMint
```

## Components

### CLI & Runtime

- [Direct-script wrapper](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/generate-graph.py): Python entry point · `generate-graph.py`
- [Installable CLI wrapper](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/generate_graph.py): Python entry point · `src/generate_graph.py`
- [Pipeline orchestrator](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/main.py): CLI orchestration · Parses arguments and coordinates all pipeline stages. · `src/knowledge_graph/main.py`
- [Config loader](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/config.py): TOML validation · `src/knowledge_graph/config.py`
- [Default configuration](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/config.toml): TOML · Supplies LLM, chunking, and stage settings. · `config.toml`
- [Package metadata](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/pyproject.toml): Python packaging · Defines console-script installation. · `pyproject.toml`

### Knowledge Graph Pipeline

- [Input text document](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/data/industrial-revolution.txt): plaintext corpus · `data/industrial-revolution.txt`
- [Text chunking](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/text_utils.py): overlapping word chunks · Bounds LLM context while preserving local context. · `src/knowledge_graph/text_utils.py`
- [Extraction prompts](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/prompts/main_prompts.py): prompt contract · `src/knowledge_graph/prompts/main_prompts.py`
- [OpenAI-compatible LLM client](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/llm.py): chat-completions transport · Handles structured responses and JSON extraction. · `src/knowledge_graph/llm.py`
- [SPO extraction](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/main.py): per-chunk extraction · Normalizes LLM output into subject–predicate–object triples. · `src/knowledge_graph/main.py`
- [Entity standardization](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/entity_standardization.py): canonical entity rewrite · Optional alias resolution consolidates entities across chunks. · `src/knowledge_graph/entity_standardization.py`
- [Entity-resolution prompts](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/prompts/entity_prompts.py): prompt contract · `src/knowledge_graph/prompts/entity_prompts.py`
- [Relationship inference](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/main.py): graph enrichment · Optionally adds inferred links and deduplicates triples. · `src/knowledge_graph/main.py`
- [Inference prompts](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/prompts/inference_prompts.py): prompt contract · `src/knowledge_graph/prompts/inference_prompts.py`

### Graph Delivery

- [Graph analytics & renderer](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/visualization.py): NetworkX / PyVis · Computes importance and communities, then emits interactive HTML. · `src/knowledge_graph/visualization.py`
- [Visualization template](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/src/knowledge_graph/templates/graph_template.html): HTML template · `src/knowledge_graph/templates/graph_template.html`
- **Raw graph JSON**: graph export
- **Interactive HTML output**: self-contained visualization · Browser-consumable without an application server.
- [Published demo artifact](https://github.com/robert-mcdermott/ai-knowledge-graph/blob/main/docs/index.html): HTML artifact · `docs/index.html`

## Connections

- 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
