# openspg/kag architecture

> Architecture diagram of the GitHub repository openspg/kag, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openspg/kag
- Repository: https://github.com/openspg/kag

## Overview

##### Main Components Overview
The system should be divided into three major layers:

a. Core Framework Layer:
- kg-builder: Knowledge representation and construction
- kg-solver: Logical reasoning and Q&A processing
- kag-model (future component)

b. Infrastructure Layer:
- OpenSPG Engine
- LLM Integration
- Vector Store
- Graph Store (Neo4j)

c. Interface Layer:
- REST APIs
- Client Interfaces
- Builder/Solver Interfaces

##### Key Architectural Elements

a. Knowledge Processing Pipeline:
**Raw Data → Reader Components → Extractors → Aligners → Knowledge Graph**

b. Solver Architecture:
**Query → Logic Form Planner → Hybrid Reasoning Engine → Response Generator**

##### Component Details

a. Builder Components (Rectangle, Blue):
- Readers (CSV, JSON, PDF, etc.)
- Extractors
- Aligners
- Vectorizers
- Writers

b. Solver Components (Hexagon, Green):
- Logic Form Planner
- Reasoning Engine
- Retrieval System
- Response Generator

- Graph Store
- Vector Store
- Document Store

##### Data Flow Representation

- Knowledge Construction Flow
- Query Processing Flow
- Reasoning Paths
- Data Integration Points

##### Integration Points

Connections with:
- LLM Services
- OpenSPG Engine
- External APIs
- Storage Systems

##### Detailed Component Breakdown

For each major component, show:
**Component Name**

- Interfaces
- Core Logic
- Dependencies
- Output Handlers

##### Cross-Cutting Concerns

- Configuration Management
- Error Handling
- Logging
- Monitoring

##### The schema-constrained knowledge construction process

Use a hierarchical layout with:
- Top: Interface/API Layer
- Middle: Core Processing Layer
- Bottom: Storage/Infrastructure Layer

Clear labels and annotations explaining key processes and interactions, particularly focusing on the knowledge augmentation and reasoning capabilities that distinguish KAG from traditional RAG systems.

Remember to highlight the system's unique features:
- Knowledge and Chunk Mutual Indexing
- Conceptual Semantic Reasoning
- Schema-constrained Knowledge Construction
- Logical Form-guided Hybrid Reasoning

This architecture should clearly show how KAG overcomes traditional RAG limitations and implements its advanced features for professional domain applications.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Interface
        API[REST APIs]
        BI[Builder Interface]:::interface
        SI[Solver Interface]:::interface
        RI[Retriever Interface]:::interface
    end

    subgraph "Core Framework"
        subgraph "Knowledge Builder"
            RD[Readers]:::builder
            EX[Extractors]:::builder
            AL[Aligners]:::builder
            VE[Vectorizers]:::builder
            WR[Writers]:::builder
            KM[Knowledge Model]:::builder
            KO[Knowledge Operators]:::builder
            BP[Builder Prompts]:::builder
        end

        subgraph "Solver Components"
            LFP[Logic Form Planner]:::solver
            RE[Reasoning Engine]:::solver
            RG[Response Generator]:::solver
            LC[Logic Core]:::solver
            LE[Logic Executors]:::solver
            SP[Solver Prompts]:::solver
        end
    end

    subgraph Infrastructure
        LLMI[LLM Integration]:::infra
        VS[Vector Store]:::storage
        GS[Graph Store]:::storage
        OSPG[OpenSPG Engine]:::infra
    end

    %% Data Flow
    API --> BI & SI & RI
    BI --> RD
    RD --> EX
    EX --> AL
    AL --> VE
    VE --> WR
    WR --> GS
    
    KM --> KO
    KO --> WR
    BP --> EX & AL

    SI --> LFP
    LFP --> RE
    RE --> RG
    LC --> LE
    LE --> RE
    SP --> RG

    RE --> VS & GS
    LLMI --> RE & EX
    OSPG --> KO & LE

    %% Styling
    classDef builder fill:#a8d1f0
    classDef solver fill:#90EE90
    classDef storage fill:#D3D3D3
    classDef infra fill:#FFE4B5
    classDef interface fill:#DDA0DD

    %% Click Events
    click RD "https://github.com/OpenSPG/KAG/tree/master/kag/builder/component/reader/"
    click EX "https://github.com/OpenSPG/KAG/tree/master/kag/builder/component/extractor/"
    click AL "https://github.com/OpenSPG/KAG/tree/master/kag/builder/component/aligner/"
    click VE "https://github.com/OpenSPG/KAG/tree/master/kag/builder/component/vectorizer/"
    click WR "https://github.com/OpenSPG/KAG/tree/master/kag/builder/component/writer/"
    click LFP "https://github.com/OpenSPG/KAG/blob/master/kag/solver/implementation/default_lf_planner.py"
    click RE "https://github.com/OpenSPG/KAG/blob/master/kag/solver/implementation/default_reasoner.py"
    click RG "https://github.com/OpenSPG/KAG/blob/master/kag/solver/prompt/default/resp_generator.py"
    click LLMI "https://github.com/OpenSPG/KAG/tree/master/kag/common/llm/"
    click VS "https://github.com/OpenSPG/KAG/tree/master/kag/common/vectorizer/"
    click GS "https://github.com/OpenSPG/KAG/blob/master/kag/common/graphstore/neo4j_graph_store.py"
    click BI "https://github.com/OpenSPG/KAG/tree/master/kag/interface/builder/"
    click SI "https://github.com/OpenSPG/KAG/tree/master/kag/interface/solver/"
    click RI "https://github.com/OpenSPG/KAG/tree/master/kag/interface/retriever/"
    click LC "https://github.com/OpenSPG/KAG/tree/master/kag/solver/logic/core_modules/"
    click LE "https://github.com/OpenSPG/KAG/tree/master/kag/solver/logic/core_modules/op_executor/"
    click KM "https://github.com/OpenSPG/KAG/tree/master/kag/builder/model/"
    click KO "https://github.com/OpenSPG/KAG/tree/master/kag/builder/operator/"
    click BP "https://github.com/OpenSPG/KAG/tree/master/kag/builder/prompt/"
    click SP "https://github.com/OpenSPG/KAG/tree/master/kag/solver/prompt/"
```
