# d3vmeh/llm-policy-generation architecture

> Architecture diagram of the GitHub repository d3vmeh/llm-policy-generation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/d3vmeh/llm-policy-generation
- Repository: https://github.com/d3vmeh/llm-policy-generation

## Overview

##### Main Components
- LLM Integration: The component where the language model processes user queries and generates responses.
- Neo4j Knowledge Graph: The database that stores data relevant to foreign policy such as treaties, historical events, and inter-nation relationships.
- GraphRAG Framework: The middleware that combines retrieval from the knowledge graph with the generation capabilities of the LLM.
- Data Pipeline Scripts:
- data_ingestion.py: Responsible for feeding and updating the Neo4j knowledge graph with fresh data.
- create_communities.py: Likely employed to segment or cluster related nodes/entities within the graph to enhance contextual understanding.
- querying.py: Handles the querying logic to interact with the knowledge graph using user prompts.
- main.py: Acts as the orchestrator that ties together the LLM, data querying, and overall business logic for the application.

##### Relationships and Interactions
- The user or an upstream application sends a query to main.py.
- Main.py routes the query into the GraphRAG framework, which involves:
- Calling querying.py to retrieve relevant contextual data from the Neo4j knowledge graph.
- Passing the retrieved data to the LLM for enhanced response generation.
- data_ingestion.py interacts with the Neo4j Knowledge Graph to ensure it is up-to-date.
- create_communities.py functions as a background process or an on-demand tool that organizes the graph data into meaningful clusters (helping the GraphRAG system provide more informed outputs).

##### Architectural Patterns and Design Principles
- Separation of Concerns: Each Python script handles a distinct aspect of the processing pipeline (data ingestion, community creation, querying, and orchestration).
- Layered Approach: Data ingestion and preprocessing are separate from the business logic (in main.py) and from the querying & generation layer.
- Integration of External Services: The system integrates with an external Neo4j database and leverages advanced LLM capabilities.
- Modular Design: Components (scripts) can be updated independently as long as the interfaces remain consistent.

##### Relevant Technologies and Frameworks
- Neo4j: The graph database for storing and retrieving structured data.
- A Large Language Model (LLM): Provides natural language generation and processing capabilities.
- Python: The language used for framework implementation and orchestration scripts.
- GraphRAG Techniques: An enhanced form of traditional RAG that incorporates graph-based relationships.

##### Detailed Breakdown
- Branch out the GraphRAG system to show:
- A path to the Query Engine (querying.py) which interacts bidirectionally with the Neo4j database.
- A path to the LLM Engine which processes the query along with additional context from the knowledge graph.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External User/Application
    U("User/Application"):::external

    %% GraphRAG Framework Subgraph
    subgraph "GraphRAG Framework"
        M("main.py (LLM Integration)"):::internal
        Q("querying.py (Query Engine)"):::internal
    end

    %% External Services
    LLM("LLM Service"):::external
    DB(("Neo4j Knowledge Graph")):::external

    %% Auxiliary Modules
    D("data_ingestion.py (Data Ingestion Module)"):::internal
    C("create_communities.py (Graph Community Creator)"):::internal

    %% Primary Flow
    U -->|"Sends_Query"| M
    M -->|"Directs_Query"| Q
    Q -->|"Fetches_Data"| DB
    DB -->|"Returns_Data"| Q
    Q -->|"Provides_Context"| M
    M -->|"Generates_Response"| LLM
    LLM -->|"Response_Data"| M
    M -->|"Sends_Response"| U

    %% Auxiliary Processes Updating Neo4j
    D ---|"Feeds_Data"| DB
    C ---|"Clusters_Data"| DB

    %% Click Events for Components from Component Mapping
    click M "https://github.com/d3vmeh/llm-policy-generation/blob/main/main.py"
    click D "https://github.com/d3vmeh/llm-policy-generation/blob/main/data_ingestion.py"
    click C "https://github.com/d3vmeh/llm-policy-generation/blob/main/create_communities.py"

    %% Styles for Color Coding
    classDef internal fill:#AEDFF7,stroke:#000,stroke-width:2px;
    classDef external fill:#C2FABC,stroke:#000,stroke-width:2px;
```
