KAG (Knowledge Augmented Generation)

repository·master·Indexed 27 days ago

https://github.com/openspg/kag

A framework for professional domain knowledge bases that combines the OpenSPG engine with LLMs to provide high-fidelity logical reasoning and factual Q&A. KAG consists of a kg-builder for LLM-friendly knowledge representation and a kg-solver for logical symbol-guided hybrid reasoning using planning, reasoning, and retrieval operators. It is designed to overcome limitations in traditional RAG and GraphRAG by integrating unstructured data, structured data, and expert rules into a unified business knowledge graph.

Tokens
14.3K
Snippets
26
Records
99
Agent score
94%

What's inside KAG

  1. Overview of KAG Core Features

    master

    KAG (Knowledge Augmented Generation) is a framework for building logical reasoning and Q&A solutions for vertical domain knowledge bases. It integrates the OpenSPG engine with LLMs to overcome RAG ambiguity and GraphRAG noise.

    Key Capabilities:

    • Knowledge Representation: Uses a DIKW-based hierarchy to integrate unstructured data (news, logs), structured data (transactions), and expert rules into a unified business knowledge graph. Supports mutual indexing between graph structures and original text blocks.
    • Mixed Reasoning: A logically formal guided hybrid solution using a kg-solver engine. It employs three operator types—planning, reasoning, and retrieval—to transform natural language into a combination of language and symbolic notation (including exact match, text retrieval, numerical calculation, and semantic reasoning).
  2. Overview of KAG Core Architecture

    master

    KAG (Knowledge Augmented Generation) is a logical reasoning QA framework based on the OpenSPG engine and LLMs. It is designed to build vertical domain knowledge bases by combining the logic of Knowledge Graphs (KG) with the flexibility of LLMs.

    The framework consists of three main components:

    1. kg-builder: Implements LLM-friendly knowledge representation (LLMFriSPG). It supports both schema-constrained professional knowledge construction and schema-less information extraction, maintaining mutual indexing between graph structures and original text chunks.
    2. kg-solver: A hybrid reasoning engine that uses logical symbols to guide the process. It converts natural language questions into a combination of language and symbolic problem-solving using three types of operators: Planning, Reasoning, and Retrieval.
    3. kag-model: (Planned for future release) The model component for KAG.
  3. Overview of KAG Architecture

    master

    The KAG framework consists of three primary components designed for knowledge-augmented generation:

    • kg-builder: Implements a knowledge representation friendly to LLMs. It supports both schema-free information extraction and schema-constrained expertise construction. It features a mutual index representation between graph structures and original text blocks to facilitate efficient retrieval.
    • kg-solver: A logical symbol-guided hybrid solving and reasoning engine. It uses three types of operators—planning, reasoning, and retrieval—to transform natural language queries into problem-solving processes involving exact match retrieval, text retrieval, numerical calculation, and semantic reasoning.
    • kag-model: (Future release) A component intended for gradual open-source release.
  4. Overview of KAG (Knowledge Augmented Generation)

    master
    KAG is a logical form-guided reasoning and retrieval framework built on the OpenSPG engine and Large Language Models (LLMs). It is designed to build logical reasoning and factual Q&A solutions for professional domain knowledge bases. KAG aims to overcome the limitations of traditional RAG (Retrieval-Augmented Generation) models that rely solely on vector similarity calculations by incorporating logical reasoning.
  5. Implement KAG for AffairQA Dataset

    master

    The AffairQA implementation uses the KAG (Knowledge-Augmented Generation) framework to build a knowledge graph from government service data and perform question-answering tasks. The process involves three main stages:

    1. Data Preprocessing & Schema Definition: Raw data is structured according to the schema/AffairQA.schema file, which defines Entity Types, Relation Types, and Properties (e.g., policy documents, agencies, service items) for the OpenSPG graph database.
    2. Knowledge Graph Construction: The builder/indexer.py script processes the data to instantiate entities and establish relationships (e.g., 'Policy' -> 'Published by' -> 'Agency') within the OpenSPG database.
    3. KAG-based QA Implementation: A pipeline defined in kag_config.yaml executes the following:
      • Planner (kag_static_planner): Analyzes the question and generates a structured retrieval plan using LLMs and retriever_static_planning templates.
      • Executor/Reasoner (reasoner_conf): Navigates the OpenSPG graph to retrieve facts based on the planner's instructions.
      • Generator (llm_generator_with_thought): Integrates retrieved facts and reasoning (thought) to produce a natural language answer.

    Performance is evaluated using evaluate_qa.py and count_correct.py to calculate Exact Match (EM) and F1 scores.

  6. KAG Architecture Components

    master

    The KAG framework is composed of three main parts (note: kag-model is planned for future release):

    1. kg-builder: Implements LLM-friendly knowledge representation based on the DIKW (Data, Information, Knowledge, Wisdom) hierarchy. It supports both schema-constrained expert knowledge construction and schema-free information extraction. It also supports mutual indexing between graph structures and original text fragments.
    2. kg-solver: A hybrid reasoning engine based on logical forms. It uses three types of operators—planning, reasoning, and searching—to transform natural language questions into a combination of linguistic and symbolic problem-solving processes. It integrates four distinct processes: search, knowledge graph reasoning, linguistic reasoning, and numerical calculation.
  7. Reproduce the MuSiQue Benchmark Example

    master

    This guide provides steps to build a knowledge graph for the MuSiQue multi-hop QA dataset and evaluate performance using KAG.

    Prerequisites: Ensure KAG and the OpenSPG server are installed following the Quick Start guide in developer mode.

    Reproduction Steps:

    1. Navigate to the directory: cd kag/open_benchmark/musique
    2. Configure Models: Update kag_config.yaml with your specific openie_llm, chat_llm, and vectorize_model settings. Ensure api_key, base_url, and model are correctly set for your provider.
    3. Initialize Project: Run knext project restore --host_addr http://127.0.0.1:8887 --proj_path .
    4. Commit Schema: Run knext schema commit to apply the schema defined in ./schema/MuSiQue.schema.
    5. Build Knowledge Graph: Run cd src && python indexer.py && cd ..
    6. Execute QA Task: Run cd src && python eval.py --qa_file ./data/qa_sub.json && cd .. to generate answers and calculate EM/F1 metrics.
    7. Cleanup (Optional): Remove checkpoints using rm -rf ./src/ckpt.
  8. Install KAG Toolkit (Developer-based)

    master

    Developers can install the KAG toolkit directly into a Python environment.

    macOS / Linux Installation:

    1. Create and activate a conda environment with Python 3.10.
    2. Clone the repository.
    3. Install in editable mode using pip.

    Windows Installation:

    1. Ensure Python 3.10+ and Git are installed.
    2. Create and activate a Python virtual environment.
    3. Clone the repository.
    4. Install in editable mode using pip.
  9. Install KAG via Docker Compose (Product-based)

    master

    For general users, KAG can be deployed using Docker Compose. This method sets up the necessary engine and dependencies automatically.

    System Requirements:

    • macOS: macOS Monterey 12.6 or later
    • Linux: CentOS 7 / Ubuntu 20.04 or later
    • Windows: Windows 10 LTSC 2021 or later

    Software Requirements:

    • macOS / Linux: Docker, Docker Compose
    • Windows: WSL 2 / Hyper-V, Docker, Docker Compose

    Installation Steps:

    1. (Windows only) Set the HOME environment variable.
    2. Download the docker-compose.yml file.
    3. Start the services using Docker Compose.

    Once running, access the KAG product at http://127.0.0.1:8887.

    # Windows users only: set the HOME environment variable
    # set HOME=%USERPROFILE%
    
    curl -sSL https://raw.githubusercontent.com/OpenSPG/openspg/refs/heads/master/dev/release/docker-compose.yml -o docker-compose.yml
    docker compose -f docker-compose.yml up -d