# auvalab/itext2kg architecture

> Architecture diagram of the GitHub repository auvalab/itext2kg, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/auvalab/itext2kg
- Repository: https://github.com/auvalab/itext2kg

## Overview

##### Main Components
- Document Distiller: This module takes raw documents and processes them into semantic blocks based on a user-defined schema. It is responsible for parsing information from scientific papers, CVs, company websites, and other inputs.
- Incremental Entity Extractor: It extracts and disambiguates entities from the refined document sections. It uses cosine similarity-based matching and applies configurable weights.
- Incremental Relation Extractor: This component identifies relationships between these entities, working on both local and global context. It also handles LLM-specific issues such as hallucination or missing relations.
- Graph Integrator & Visualization: Responsible for integrating the extracted components into a Neo4j database, providing a graph-based visualization for exploration and interactive analysis.
- Utility and Supporting Modules: Includes utility functions such as the LLM output parser, matcher, and schema definitions that support the main modules.
- External Integrations: Notice that the system utilizes external LLMs and embedding models (for instance, OpenAI, Mistral, and LangChain integrations) which are critical for the extraction processes.

##### Representing Relationships and Interactions
- Raw Documents (inputs) → Document Distiller.
- Distilled semantic blocks → Incremental Entity Extractor.
- Distilled semantic blocks (or extracted entities) → Incremental Relation Extractor.
- Both the extracted entities and relations feed into the core iText2KG engine, which orchestrates these operations.
- The results, in the form of a knowledge graph, are then passed to the Graph Integrator for visualization and further interaction via Neo4j.
- External dependencies:
- Data flow from the Knowledge Graph (or constructed semantic representations) into the Neo4j database.

##### Architectural Patterns and Design Principles
- Modular Separation – Each key functionality is encapsulated in its submodule (distiller, extraction modules, integrator). Indicate these modules as distinct blocks.
- Incremental Processing – The graph is built in an incremental manner where new documents can trigger updates to the existing knowledge graph.
- External Services Integration – The design is extensible by integrating multiple external model services. Represent these as “External LLM Service”, “External Embedding Service”, and “Neo4j Graph Database”.
- Input Transformation – Begin with raw text and apply a transformation pipeline (distillation → extraction → graph integration).

##### Including Relevant Technologies and Frameworks
- Python Environment with dependencies indicated in pyproject.toml, requirements.txt, and setup.cfg.
- Neo4j for graph database storage and visualization.
- LangChain integration for LLM and embedding models.
- pydantic (as seen from schema definitions) for data modeling.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Internal Modules
    A["Raw Documents"]:::internal
    B["Document Distiller"]:::internal
    C["Semantic Blocks"]:::internal
    D["Incremental Entity Extractor"]:::internal
    E["Incremental Relation Extractor"]:::internal

    %% Utility Modules Subgraph
    subgraph "Utility Modules"
        F1["LLM Output Parser"]:::utility
        F2["Matcher"]:::utility
        F3["Schema Definitions"]:::utility
    end

    %% Core iText2KG Subgraph
    subgraph "Core iText2KG Logic & Graph Construction"
        G1["iText2KG Engine"]:::internal
        G2["Knowledge Graph Model"]:::internal
    end

    H["Graph Integrator & Visualization"]:::internal

    %% External Components
    I["External LLM Service"]:::external
    J["External Embedding Service"]:::external
    K["Neo4j Graph Database"]:::external

    %% Data Flow
    A -->|"parse"| B
    B -->|"generate"| C
    C -->|"entityExtraction"| D
    C -->|"relationExtraction"| E

    %% Utility support to Extraction Modules
    F1 -->|"LLMParse"| D
    F2 -->|"Matcher"| D
    F3 -->|"SchemaSupport"| E

    %% Extraction Modules feed into Core Engine
    D -->|"entities"| G1
    E -->|"relations"| G1

    %% Internal Core Operations
    G1 -->|"buildsKG"| G2
    G1 -->|"callsLLM"| I
    G1 -->|"callsEmbedding"| J

    %% Graph Integration
    G1 -->|"constructKG"| H
    H -->|"feedData"| K

    %% Click Events for Component Mappings
    click B "https://github.com/auvalab/itext2kg/blob/main/itext2kg/documents_distiller/documents_distiller.py"
    click D "https://github.com/auvalab/itext2kg/blob/main/itext2kg/ientities_extraction/ientities_extractor.py"
    click E "https://github.com/auvalab/itext2kg/blob/main/itext2kg/irelations_extraction/irelations_extractor.py"
    click H "https://github.com/auvalab/itext2kg/blob/main/itext2kg/graph_integration/graph_integrator.py"
    click G1 "https://github.com/auvalab/itext2kg/blob/main/itext2kg/itext2kg.py"
    click G2 "https://github.com/auvalab/itext2kg/blob/main/itext2kg/models/knowledge_graph.py"
    click F1 "https://github.com/auvalab/itext2kg/blob/main/itext2kg/utils/llm_output_parser.py"
    click F2 "https://github.com/auvalab/itext2kg/blob/main/itext2kg/utils/matcher.py"
    click F3 "https://github.com/auvalab/itext2kg/blob/main/itext2kg/utils/schemas.py"

    %% Styles
    classDef internal fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef utility fill:#FAD7A0,stroke:#E67E22,stroke-width:2px;
    classDef external fill:#F9E79F,stroke:#D4AC0D,stroke-width:2px,stroke-dasharray: 5 5;
    
    %% Ensure vertical flow by ordering nodes (the TD direction by default is vertical)
    %% End of Diagram
```
