# yuxinglu613/karma architecture

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

- Interactive diagram: https://gitdiagram.com/yuxinglu613/karma
- Repository: https://github.com/yuxinglu613/karma

## Overview

##### Main Components
- Input & Document Ingestion: Begin with an "Input" component that accepts user-provided PDFs (and potentially text files). This component initiates the process and hands off the document for further processing.
- PDF & Text Parsing Module: Represent a module for parsing PDF documents using libraries like PyPDF2. This module is responsible for extracting raw text.
- Content Segmentation & Summarization: Include a stage where the raw text is segmented into context-aware sections and summarized.
- Multi-Agent Knowledge Extraction: Central to the KARMA system, this section involves several specialized agents:
- Entity Recognition Agent: Extracts key entities (e.g., scientific terms).
- Relationship Identification Agent: Determines relationships between recognized entities.
- Semantic Triple Formation Agent: Combines results into structured knowledge triples.
- Quality Assurance & Validation: A module dedicated to evaluating triples through scoring (e.g., confidence, clarity, relevance) and conflict resolution, ensuring only high-quality output.
- Knowledge Graph Operations: Represent a component that uses tools such as networkx to integrate or update the knowledge graph with the extracted and validated knowledge.
- Output Generation: Finally, design an “Output” component that writes the results as JSON (or into any other required format).

##### Relationships and Interactions
- Parsing module to the Content Segmentation module, as the text must be processed further.
- Link the extraction output to the Quality Assurance module, which reviews and either validates or adjusts the extracted knowledge.
- Finally, direct the processed output to the Output Generation module that produces the final JSON result.

##### Architectural Patterns & Design Principles
- Use of a pipeline (sequential stages with potential parallel processing in the multi-agent stage).
- Modularity: each stage (parsing, segmentation, extraction, validation) functions as an independent unit, which enhances maintainability and testing.
- Separation of concerns: from file handling to NLP processing, and finally to graph management.
- If desired, illustrate “control flow” by depicting feedback loops such as potential return paths when validation fails or conflicts are detected.

##### Technologies and Frameworks
- Python 3.8+ is the base language.
- Key libraries: PyPDF2 (PDF processing), spaCy (NLP), and networkx (knowledge graph operations). Also, include labels for the integrated LLM agents using OpenAI’s API.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "KARMA Pipeline"
        A["Input & Document Ingestion"]:::module
        B["PDF & Text Parsing Module"]:::module
        C["Content Segmentation & Summarization"]:::module
        subgraph "Multi-Agent Knowledge Extraction"
            D["Entity Recognition Agent"]:::agent
            E["Relationship Identification Agent"]:::agent
            F["Semantic Triple Formation Agent"]:::agent
        end
        G["Quality Assurance & Validation"]:::module
        H["Knowledge Graph Operations"]:::module
        I["Output Generation"]:::module
    end

    ExternalLLM["OpenAI API"]:::external

    A -->|"ingests"| B
    B -->|"extractsText"| C
    C -->|"feedsData"| D
    C -->|"feedsData"| E
    C -->|"feedsData"| F
    D -->|"results"| G
    E -->|"results"| G
    F -->|"results"| G
    G -->|"validatedData"| H
    H -->|"enrichedKG"| I
    D ---|"callsLLM"| ExternalLLM
    E ---|"callsLLM"| ExternalLLM
    F ---|"callsLLM"| ExternalLLM

    click A "https://github.com/yuxinglu613/karma/blob/main/karma_pipeline.py"
    click B "https://github.com/yuxinglu613/karma/blob/main/karma_pipeline.py"
    click C "https://github.com/yuxinglu613/karma/blob/main/karma_pipeline.py"
    click D "https://github.com/yuxinglu613/karma/blob/main/karma_pipeline.py"
    click E "https://github.com/yuxinglu613/karma/blob/main/karma_pipeline.py"
    click F "https://github.com/yuxinglu613/karma/blob/main/karma_pipeline.py"
    click G "https://github.com/yuxinglu613/karma/blob/main/karma_pipeline.py"
    click H "https://github.com/yuxinglu613/karma/blob/main/karma_pipeline.py"
    click I "https://github.com/yuxinglu613/karma/blob/main/karma_pipeline.py"

    classDef module fill:#f9f,stroke:#333,stroke-width:2px;
    classDef agent fill:#bbf,stroke:#333,stroke-width:2px;
    classDef external fill:#fa9,stroke:#333,stroke-width:2px;
```
