# KAG

> Knowledge-graph reasoning and Q&A framework for professional domains

- Category: Retrieval & Knowledge (RAG) / RAG Frameworks & Platforms
- Language: Python
- Licence: Apache-2.0
- Repository: https://github.com/OpenSPG/KAG
- Website: https://spg.openkg.cn/en-US
- Canonical page: https://ai-tldr.dev/tools/kag/

## In short

KAG (Knowledge Augmented Generation) is a logical reasoning and question-answering framework built on the OpenSPG engine and large language models. It is designed to build reasoning and Q&A solutions over vertical-domain knowledge bases, where unstructured documents, structured records, and expert rules all need to work together. It sits in the RAG Frameworks & Platforms category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python and licensed Apache-2.0.

## Overview

KAG (Knowledge Augmented Generation) is a logical reasoning and question-answering framework built on the OpenSPG engine and large language models. It is designed to build reasoning and Q&A solutions over vertical-domain knowledge bases, where unstructured documents, structured records, and expert rules all need to work together.

Plain vector RAG often struggles with ambiguity, and GraphRAG built from open information extraction can add noise. KAG aims to reduce both problems by linking knowledge to text chunks through a mutual index and by using logical forms to guide how a question is planned, retrieved, and reasoned over. It supports multi-hop fact questions and reports results that beat earlier methods on its benchmarks.

## What it does

- Knowledge and chunk mutual indexing that ties graph structure back to the original text for richer context
- Schema-constrained knowledge construction so domain expert rules and entity/event types can be represented
- Logical-form-guided hybrid solver with planning, reasoning, and retrieval operators
- Combines four problem-solving paths: exact-match retrieval, text retrieval, numerical calculation, and semantic reasoning
- Supports both private knowledge bases and public web sources through the MCP protocol
- Knowledge alignment via conceptual semantic reasoning to cut down on extraction noise

## Getting started

KAG offers a product mode for ordinary users (run the full engine with Docker Compose) and a toolkit mode for developers (install the Python package). The toolkit path below is the quickest way to start building on macOS or Linux.

1. Launch the engine with Docker Compose

   Download the docker-compose file and start the OpenSPG engine and its dependent services. Then open the product UI at http://127.0.0.1:8887 (default login openspg / openspg@kag).

```bash
curl -sSL https://raw.githubusercontent.com/OpenSPG/openspg/refs/heads/master/dev/release/docker-compose-west.yml -o docker-compose-west.yml
docker compose -f docker-compose-west.yml up -d
```

2. Create a Python environment

   For the developer toolkit, create and activate a Python 3.10 environment using conda.

```bash
conda create -n kag-demo python=3.10 && conda activate kag-demo
```

3. Clone and install KAG

   Clone the repository and install the package in editable mode so you can use the built-in components.

```bash
git clone https://github.com/OpenSPG/KAG.git
cd KAG && pip install -e .
```

## When to use it

- Building factual Q&A over a private, vertical-domain knowledge base that mixes documents and structured data
- Answering multi-hop questions that need several reasoning steps rather than a single similarity lookup
- Injecting domain expert rules and a custom schema to constrain how knowledge is extracted and reasoned over
- Powering inference-driven Q&A inside agent workflows by exposing KAG over the MCP protocol
