# dgg32/helms architecture

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

- Interactive diagram: https://gitdiagram.com/dgg32/helms
- Repository: https://github.com/dgg32/helms

## Overview

#### Purpose and entry points
HELMS builds schema-driven, typed knowledge graphs from PDF/Markdown documents. It extracts candidate triples with an LLM, validates and reviews them, persists approved facts, and exposes graph/document retrieval to AI clients.
- CLI pipeline: `pipeline.py`, `convert_pdf.py`, `extract.py`, `apply_graph.py`
- Web workflow: `htmx_app/main.py`
- MCP read/query and ingest endpoints: `mcp_server.py`, `mcp_ingest_server.py`

#### Schema and project boundary
A project supplies the human-authored graph contract and document scope; runtime output is organized into project run folders. The schema determines node types, relationship types, primary keys, extraction prompts, and resolver constraints, avoiding domain-specific code changes.
- `projects/drug/schema.yaml`, `projects/drug/meta.yaml`
- `projects/supplychain/schema.yaml`, `projects/supplychain/meta.yaml`
- `schema_raw.py`, `pipeline_meta.py`

#### Document-to-candidate extraction
PDF conversion produces Markdown sidecars, then extraction semantically chunks Markdown and makes structured LLM calls per chunk. It retains immutable raw triples and document text, while quote grounding re-anchors LLM-provided evidence to exact source spans. Extraction is cacheable and supports multiple LLM providers through one structured-output interface.
- `convert_pdf.py`, `extract.py`, `grounding.py`
- `llm_client.py`, `prompts.yaml`

Flow: PDF → Markdown → chunks → LLM triples/evidence → grounded `<stem>_raw.json`. Metadata instructions and page filters cross the conversion/extraction boundary.

#### Safeguards and human review
External entity resolution occurs after extraction: UMLS for medical concepts and GLEIF for organizations. A semantic-check agent is the sole authority for green/yellow/red verdicts, applying LLM relation-support checks plus deterministic schema, type, duplicate, and fabricated-quote validators. Human actions remain separate as an event log; materialization overlays edits onto immutable raw output before graph writes.
- `agents/node_agent.py`, `agents/umls_resolver.py`, `agents/gleif_resolver.py`
- `agents/semantic_check_agent.py`, `review_layer.py`
- `agents/extraction_agent.py`

The targeted agentic retry path can add/re-resolve one relationship type, then re-runs semantic checking.

#### Learning loop and orchestration
Approved, overridden, agent-retry, and rejected review outcomes are harvested into per-relationship examples. Future extraction injects positive examples and document-specific rejection reminders. The orchestration layer supports CLI, UI-runner, and background ingestion paths while preserving run artifacts and statistics.
- `agents/harvest.py`, `pipeline.py`, `pipeline_runner.py`
- `mcp_ingest_server.py`, `kg_logging.py`

#### Graph persistence and query serving
`apply_graph.py` materializes reviews, filters by verdict, and writes nodes/edges through a backend abstraction. Node identity is primary-key based; edges are source-document-scoped, enabling incremental multi-document provenance and same-document replacement.
- `apply_graph.py`, `backends/base.py`
- `backends/ladybug_backend.py`, `backends/neo4j_backend.py`

LadybugDB is the default embedded openCypher store; Neo4j is supported over Bolt. `mcp_server.py` serves read-only Cypher plus just-in-time retrieval from raw document text, with FastMCP as the integration runtime.

#### External services and runtimes
The Python application uses LiteLLM for Azure OpenAI, OpenAI-compatible, Anthropic, Ollama, and Gemini structured calls; UMLS and GLEIF REST APIs for authority grounding; pymupdf4llm or LlamaParse for conversion; FastAPI/HTMX for the optional UI; SQLite caching for registry lookups.
- `lookup_cache.py`, `lookups.py`
- `htmx_app/main.py`, `mcp_server.py`

Main entry points: [`pipeline_meta.py`](https://github.com/dgg32/helms/blob/main/pipeline_meta.py) (Schema loader), [`agents/semantic_check_agent.py`](https://github.com/dgg32/helms/blob/main/agents/semantic_check_agent.py) (Semantic check agent), [`extract.py`](https://github.com/dgg32/helms/blob/main/extract.py) (Candidate extraction), [`apply_graph.py`](https://github.com/dgg32/helms/blob/main/apply_graph.py) (Graph materialization)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_project["Project contract"]
  node_project_schema["Project schemas<br/>YAML contracts<br/>[schema.yaml]"]
  node_project_metadata["Project metadata<br/>YAML scope<br/>[meta.yaml]"]
  node_pipeline_meta["Schema loader<br/>project metadata<br/>[pipeline_meta.py]"]
end

subgraph group_extract["Extraction pipeline"]
  node_pdf_conversion["PDF conversion<br/>document converter<br/>[convert_pdf.py]"]
  node_extraction["Candidate extraction<br/>semantic chunking<br/>[extract.py]"]
  node_llm_client["Structured LLM client<br/>provider abstraction<br/>[llm_client.py]"]
  node_grounding["Quote grounding<br/>evidence validator<br/>[grounding.py]"]
  node_raw_artifacts["Raw run artifacts<br/>immutable JSON"]
end

subgraph group_review["Resolution and review"]
  node_entity_resolution["Entity resolution<br/>authority resolvers<br/>[node_agent.py]"]
  node_lookup_cache[("Lookup cache<br/>SQLite cache<br/>[lookup_cache.py]")]
  node_semantic_check["Semantic check agent<br/>verdict authority"]
  node_review_layer["Review event log<br/>human review<br/>[review_layer.py]"]
  node_learning_harvest["Example harvesting<br/>learning loop<br/>[harvest.py]"]
end

subgraph group_graph["Graph serving"]
  node_materialize["Graph materialization<br/>approved fact writer<br/>[apply_graph.py]"]
  node_graph_backend[("Graph backend abstraction<br/>storage interface<br/>[base.py]")]
  node_mcp_query["MCP query server<br/>FastMCP read API<br/>[mcp_server.py]"]
end

subgraph group_interfaces["Runtimes and integrations"]
  node_orchestrator["Pipeline orchestrator<br/>run coordinator<br/>[pipeline_runner.py]"]
  node_cli_pipeline["CLI pipeline<br/>command entry point<br/>[pipeline.py]"]
  node_web_workflow["HTMX web workflow<br/>FastAPI UI<br/>[main.py]"]
  node_mcp_ingest["MCP ingest server<br/>background ingest API"]
end

node_project_schema -->|"loaded as contract"| node_pipeline_meta
node_project_metadata -->|"loads scope"| node_pipeline_meta
node_pipeline_meta -->|"instructions and filters"| node_pdf_conversion
node_pipeline_meta -->|"types and prompts"| node_extraction
node_pdf_conversion -->|"Markdown sidecars"| node_extraction
node_extraction -->|"structured calls"| node_llm_client
node_llm_client -->|"triples and evidence"| node_grounding
node_grounding -->|"grounded raw output"| node_raw_artifacts
node_raw_artifacts -->|"candidate entities"| node_entity_resolution
node_entity_resolution -->|"caches registry results"| node_lookup_cache
node_entity_resolution -->|"resolved candidates"| node_semantic_check
node_raw_artifacts -->|"triples and source evidence"| node_semantic_check
node_semantic_check -->|"verdicts for review"| node_review_layer
node_review_layer -->|"edits and overrides"| node_materialize
node_raw_artifacts -->|"immutable source facts"| node_materialize
node_semantic_check -->|"approved verdicts"| node_materialize
node_review_layer -->|"outcomes"| node_learning_harvest
node_learning_harvest -.->|"examples and reminders"| node_extraction
node_materialize -->|"nodes and scoped edges"| node_graph_backend
node_graph_backend -->|"Cypher reads"| node_mcp_query
node_raw_artifacts -->|"document retrieval"| node_mcp_query
node_cli_pipeline -->|"invokes"| node_orchestrator
node_web_workflow -->|"runs workflow"| node_orchestrator
node_mcp_ingest -->|"background ingestion"| node_orchestrator
node_orchestrator -->|"coordinates pipeline"| node_pdf_conversion

click node_project_schema "https://github.com/dgg32/helms/blob/main/projects/drug/schema.yaml"
click node_project_metadata "https://github.com/dgg32/helms/blob/main/projects/drug/meta.yaml"
click node_pipeline_meta "https://github.com/dgg32/helms/blob/main/pipeline_meta.py"
click node_pdf_conversion "https://github.com/dgg32/helms/blob/main/convert_pdf.py"
click node_extraction "https://github.com/dgg32/helms/blob/main/extract.py"
click node_llm_client "https://github.com/dgg32/helms/blob/main/llm_client.py"
click node_grounding "https://github.com/dgg32/helms/blob/main/grounding.py"
click node_entity_resolution "https://github.com/dgg32/helms/blob/main/agents/node_agent.py"
click node_lookup_cache "https://github.com/dgg32/helms/blob/main/lookup_cache.py"
click node_semantic_check "https://github.com/dgg32/helms/blob/main/agents/semantic_check_agent.py"
click node_review_layer "https://github.com/dgg32/helms/blob/main/review_layer.py"
click node_learning_harvest "https://github.com/dgg32/helms/blob/main/agents/harvest.py"
click node_materialize "https://github.com/dgg32/helms/blob/main/apply_graph.py"
click node_graph_backend "https://github.com/dgg32/helms/blob/main/backends/base.py"
click node_mcp_query "https://github.com/dgg32/helms/blob/main/mcp_server.py"
click node_orchestrator "https://github.com/dgg32/helms/blob/main/pipeline_runner.py"
click node_cli_pipeline "https://github.com/dgg32/helms/blob/main/pipeline.py"
click node_web_workflow "https://github.com/dgg32/helms/blob/main/htmx_app/main.py"
click node_mcp_ingest "https://github.com/dgg32/helms/blob/main/mcp_ingest_server.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_project_schema,node_project_metadata,node_pipeline_meta toneBlue
class node_pdf_conversion,node_extraction,node_llm_client,node_grounding,node_raw_artifacts toneAmber
class node_entity_resolution,node_lookup_cache,node_semantic_check,node_review_layer,node_learning_harvest toneMint
class node_materialize,node_graph_backend,node_mcp_query toneRose
class node_orchestrator,node_cli_pipeline,node_web_workflow,node_mcp_ingest toneIndigo
```

## Components

### Project contract

Schema and metadata define each graph domain and document scope.

- [Project schemas](https://github.com/dgg32/helms/blob/main/projects/drug/schema.yaml): YAML contracts · Defines types, keys, relations, prompts, and resolver constraints. · `projects/drug/schema.yaml`
- [Project metadata](https://github.com/dgg32/helms/blob/main/projects/drug/meta.yaml): YAML scope · Supplies document metadata and pipeline instructions. · `projects/drug/meta.yaml`
- [Schema loader](https://github.com/dgg32/helms/blob/main/pipeline_meta.py): project metadata · `pipeline_meta.py`

### Extraction pipeline

Documents become grounded immutable candidate facts.

- [PDF conversion](https://github.com/dgg32/helms/blob/main/convert_pdf.py): document converter · Produces Markdown sidecars with metadata and page filtering. · `convert_pdf.py`
- [Candidate extraction](https://github.com/dgg32/helms/blob/main/extract.py): semantic chunking · Chunks Markdown and emits raw triples plus evidence. · `extract.py`
- [Structured LLM client](https://github.com/dgg32/helms/blob/main/llm_client.py): provider abstraction · Supports multiple providers through one structured-output interface. · `llm_client.py`
- [Quote grounding](https://github.com/dgg32/helms/blob/main/grounding.py): evidence validator · Re-anchors evidence to exact document spans. · `grounding.py`
- **Raw run artifacts**: immutable JSON · Stores source text and grounded raw triples per document run.

### Resolution and review

Candidates are resolved, checked, and edited before approval.

- [Entity resolution](https://github.com/dgg32/helms/blob/main/agents/node_agent.py): authority resolvers · Resolves medical concepts and organizations through external registries. · `agents/node_agent.py`
- [Lookup cache](https://github.com/dgg32/helms/blob/main/lookup_cache.py): SQLite cache · Caches registry lookups. · `lookup_cache.py`
- [Semantic check agent](https://github.com/dgg32/helms/blob/main/agents/semantic_check_agent.py): verdict authority · Assigns green, yellow, or red using LLM and deterministic validators. · `agents/semantic_check_agent.py`
- [Review event log](https://github.com/dgg32/helms/blob/main/review_layer.py): human review · Keeps human actions separate from immutable extraction output. · `review_layer.py`
- [Example harvesting](https://github.com/dgg32/helms/blob/main/agents/harvest.py): learning loop · Builds relationship examples and rejection reminders from outcomes. · `agents/harvest.py`

### Graph serving

Approved facts are materialized into queryable graph storage.

- [Graph materialization](https://github.com/dgg32/helms/blob/main/apply_graph.py): approved fact writer · Overlays review edits, filters verdicts, and writes graph changes. · `apply_graph.py`
- [Graph backend abstraction](https://github.com/dgg32/helms/blob/main/backends/base.py): storage interface · Supports embedded LadybugDB and Neo4j graph persistence. · `backends/base.py`
- [MCP query server](https://github.com/dgg32/helms/blob/main/mcp_server.py): FastMCP read API · Serves read-only Cypher and just-in-time document retrieval. · `mcp_server.py`

### Runtimes and integrations

CLI, web, and MCP paths invoke the shared workflow.

- [Pipeline orchestrator](https://github.com/dgg32/helms/blob/main/pipeline_runner.py): run coordinator · Preserves run artifacts and statistics across execution modes. · `pipeline_runner.py`
- [CLI pipeline](https://github.com/dgg32/helms/blob/main/pipeline.py): command entry point · `pipeline.py`
- [HTMX web workflow](https://github.com/dgg32/helms/blob/main/htmx_app/main.py): FastAPI UI · `htmx_app/main.py`
- [MCP ingest server](https://github.com/dgg32/helms/blob/main/mcp_ingest_server.py): background ingest API · `mcp_ingest_server.py`

## Connections

- Project schemas → Schema loader: loaded as contract
- Project metadata → Schema loader: loads scope
- Schema loader → PDF conversion: instructions and filters
- Schema loader → Candidate extraction: types and prompts
- PDF conversion → Candidate extraction: Markdown sidecars
- Candidate extraction → Structured LLM client: structured calls
- Structured LLM client → Quote grounding: triples and evidence
- Quote grounding → Raw run artifacts: grounded raw output
- Raw run artifacts → Entity resolution: candidate entities
- Entity resolution → Lookup cache: caches registry results
- Entity resolution → Semantic check agent: resolved candidates
- Raw run artifacts → Semantic check agent: triples and source evidence
- Semantic check agent → Review event log: verdicts for review
- Review event log → Graph materialization: edits and overrides
- Raw run artifacts → Graph materialization: immutable source facts
- Semantic check agent → Graph materialization: approved verdicts
- Review event log → Example harvesting: outcomes
- Example harvesting → Candidate extraction: examples and reminders
- Graph materialization → Graph backend abstraction: nodes and scoped edges
- Graph backend abstraction → MCP query server: Cypher reads
- Raw run artifacts → MCP query server: document retrieval
- CLI pipeline → Pipeline orchestrator: invokes
- HTMX web workflow → Pipeline orchestrator: runs workflow
- MCP ingest server → Pipeline orchestrator: background ingestion
- Pipeline orchestrator → PDF conversion: coordinates pipeline
