# microsoft/kblam architecture

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

- Interactive diagram: https://gitdiagram.com/microsoft/kblam
- Repository: https://github.com/microsoft/kblam

## Overview

##### Project’s Core Purpose and Type
- KBLaM is a research project (for ICLR 2025) that augments large language models (LLMs) with external knowledge using specialized adapter modules. It integrates with pre-trained models from the Hugging Face hub (e.g., Meta-Llama, Phi-3).
- It aims to eliminate the need for external retrieval modules by directly incorporating knowledge base (KB) tokens into the LLM workflow. It also includes synthetic dataset generation, training, and evaluation, which are all central to understanding the system.

##### Break Down Main Components Based on the File Structure
a. Data generation and dataset preparation:
- Folder: dataset_generation
- Contains scripts like gen_synthetic_data.py (to create synthetic KB data) and generate_kb_embeddings.py (to convert KB information into embeddings).
- Folder: datasets
- Contains example datasets (both real and synthetic) and dataset metadata.

b. Experimentation, Training, and Evaluation Components:
- Folder: experiments
- Includes training scripts (train.py) and evaluation tools (eval.py, output_scorer.py) that replicate experiments described in the paper.
- Notebook: eval_acc.ipynb for interactive evaluation analysis.

c. Core Library/Processing Modules:
- Folder: src/kblam
- The main library that implements KBLaM functionality.
- Contains submodules:
- gpt_session.py: Likely handles session management with the GPT or transformer models.
- kb_encoder.py: Implements the encoding logic to integrate the KB into LLM inputs.
- models: Contains multiple adapter modules and configuration classes:
- kblam_config.py and kblam_processor.py: Define processing configurations and core adapters.
- Specific model adapters like llama3_model.py and phi3_model.py that interface with supported LLMs.
- utils: Contains helper scripts for data conversion, evaluation utilities, model training utilities, etc.

d. Development & Integration Support:
- Folders such as .github (workflows, CI/CD configs), tests (unit tests) and additional documentation like CODE_OF_CONDUCT.md, LICENSE, and SUPPORT.md help maintain project integrity.

##### Relationships and Data/Control Flow
- Data Generation to Dataset Construction:
- Synthetic KB creation and embedding generation modules (in dataset_generation) produce structured datasets that feed into training modules.
- Model Adapters and Core LLM Integration:
- The core adapter modules (in src/kblam/models) act as intermediaries between the external LLM (from Hugging Face) and the augmented KB tokens generated from the KB encoder.
- The kb_encoder (in src/kblam) preprocesses the KB data that is then formatted by model-specific adapter scripts.
- Training and Experimentation:
- Scripts in experiments use processed data (possibly from the datasets) and call functions from src/kblam (including session management and training utilities) to run the training pipelines.
- Evaluation:
- The evaluation scripts and notebooks (eval.py, eval_acc.ipynb, and scorer scripts) assess performance in terms of retrieval accuracy, refusal rate, precision, and recall.
- External Dependencies:
- An element for the “Hugging Face Hub” and external services (e.g., Azure OpenAI for dataset generation).

##### Architectural Patterns and Design Principles
- Modular/Layered Architecture: Separate layers for data ingestion, model adapter logic, training, and evaluation.
- Adapter Pattern: The model adapters (e.g., llama3_model.py, phi3_model.py) serve as plug-in components that can be extended, representing the extensibility of the system.
- Separation of Concerns: Clear distinct modules for dataset generation, core library functionality (encoding & model processing), training experiments, and testing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Generation & Dataset Preparation
    subgraph "Data Generation & Dataset Preparation"
        DS1["gen_synthetic_data.py"]:::file
        DS2["generate_kb_embeddings.py"]:::file
        DS3["create_train_test_split.py"]:::file
        DS_datasets["Datasets"]:::folder
    end

    %% Core Processing Module (src/kblam)
    subgraph "Core Processing Module (src/kblam)"
        CP1["GPT Session (gpt_session.py)"]:::file
        CP2["KB Encoder (kb_encoder.py)"]:::file
        subgraph "Model Adapters & Configuration (src/kblam/models)"
            MA1["KBLaM Config (kblam_config.py)"]:::file
            MA2["KBLaM Processor (kblam_processor.py)"]:::file
            MA3["Llama3 Model (llama3_model.py)"]:::file
            MA4["Phi3 Model (phi3_model.py)"]:::file
        end
        subgraph "Utilities (src/kblam/utils)"
            U1["Convert (convert.py)"]:::file
            U2["Data Utils (data_utils.py)"]:::file
            U3["Eval Utils (eval_utils.py)"]:::file
            U4["Model Utils (model_utils.py)"]:::file
            U5["Train Utils (train_utils.py)"]:::file
        end
    end

    %% Experimentation, Training & Evaluation
    subgraph "Experimentation, Training & Evaluation"
        ET1["Train Script (train.py)"]:::file
        ET2["Eval Script (eval.py)"]:::file
        ET3["Output Scorer (output_scorer.py)"]:::file
        ET4["Output Scorer Open Ended (output_scorer_open_ended.py)"]:::file
        ET5["Makefile"]:::file
        ET6["Interactive Eval (eval_acc.ipynb)"]:::file
    end

    %% External Dependencies
    subgraph "External Dependencies"
        EX1["Hugging Face Hub"]:::external
        EX2["Azure OpenAI"]:::external
    end

    %% Development & Integration Support
    subgraph "Development & Integration Support"
        DEV1[".github"]:::folder
        DEV2["Tests"]:::folder
        DEV3["CODE_OF_CONDUCT.md"]:::file
        DEV4["LICENSE"]:::file
        DEV5["SUPPORT.md"]:::file
        DEV6["README.md"]:::file
    end

    %% Connections: Data Generation flows
    DS1 -->|"produces"| DS_datasets
    DS2 -->|"produces"| DS_datasets
    DS3 -->|"produces"| DS_datasets

    %% Datasets feed into Core Processing
    DS_datasets -->|"feeds"| CP2

    %% Core Processing: KB Encoder and Adapter flow
    CP2 -->|"encodes KB"| MA1
    MA1 -->|"configures"| MA3
    MA1 -->|"configures"| MA4
    CP1 -->|"session management"| ET1

    %% Datasets directly used for Training
    DS_datasets -->|"training data"| ET1

    %% Model Adapters augment training pipeline
    MA3 -->|"augments LLM"| ET1
    MA4 -->|"augments LLM"| ET1

    %% Training links to Evaluation pipeline
    ET1 -->|"triggers"| ET2
    ET2 -->|"evaluation"| ET3
    ET3 -->|"scoring"| ET4
    ET2 -->|"analysis"| ET6
    ET2 -->|"feedback"| CP2

    %% External Dependencies provide models and services
    EX1 -->|"LLM access"| MA3
    EX1 -->|"LLM access"| MA4
    EX2 -->|"assists with data"| DS1

    %% Click Events for Data Generation & Dataset Preparation
    click DS1 "https://github.com/microsoft/kblam/blob/main/dataset_generation/gen_synthetic_data.py"
    click DS2 "https://github.com/microsoft/kblam/blob/main/dataset_generation/generate_kb_embeddings.py"
    click DS3 "https://github.com/microsoft/kblam/blob/main/dataset_generation/create_train_test_split.py"
    click DS_datasets "https://github.com/microsoft/kblam/tree/main/datasets/"

    %% Click Events for Core Processing Modules
    click CP1 "https://github.com/microsoft/kblam/blob/main/src/kblam/gpt_session.py"
    click CP2 "https://github.com/microsoft/kblam/blob/main/src/kblam/kb_encoder.py"
    click MA1 "https://github.com/microsoft/kblam/blob/main/src/kblam/models/kblam_config.py"
    click MA2 "https://github.com/microsoft/kblam/blob/main/src/kblam/models/kblam_processor.py"
    click MA3 "https://github.com/microsoft/kblam/blob/main/src/kblam/models/llama3_model.py"
    click MA4 "https://github.com/microsoft/kblam/blob/main/src/kblam/models/phi3_model.py"
    click U1 "https://github.com/microsoft/kblam/blob/main/src/kblam/utils/convert.py"
    click U2 "https://github.com/microsoft/kblam/blob/main/src/kblam/utils/data_utils.py"
    click U3 "https://github.com/microsoft/kblam/blob/main/src/kblam/utils/eval_utils.py"
    click U4 "https://github.com/microsoft/kblam/blob/main/src/kblam/utils/model_utils.py"
    click U5 "https://github.com/microsoft/kblam/blob/main/src/kblam/utils/train_utils.py"

    %% Click Events for Experimentation, Training & Evaluation
    click ET1 "https://github.com/microsoft/kblam/blob/main/experiments/train.py"
    click ET2 "https://github.com/microsoft/kblam/blob/main/experiments/eval.py"
    click ET3 "https://github.com/microsoft/kblam/blob/main/experiments/output_scorer.py"
    click ET4 "https://github.com/microsoft/kblam/blob/main/experiments/output_scorer_open_ended.py"
    click ET5 "https://github.com/microsoft/kblam/tree/main/experiments/Makefile"
    click ET6 "https://github.com/microsoft/kblam/blob/main/eval_acc.ipynb"

    %% Click Events for Development & Integration Support
    click DEV1 "https://github.com/microsoft/kblam/tree/main/.github/"
    click DEV2 "https://github.com/microsoft/kblam/tree/main/tests/"
    click DEV3 "https://github.com/microsoft/kblam/blob/main/CODE_OF_CONDUCT.md"
    click DEV4 "https://github.com/microsoft/kblam/tree/main/LICENSE"
    click DEV5 "https://github.com/microsoft/kblam/blob/main/SUPPORT.md"
    click DEV6 "https://github.com/microsoft/kblam/blob/main/README.md"

    %% Styles
    classDef file fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef folder fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef external fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
```
