Skip to main content

BYOKG-RAG: Bring Your Own Knowledge Graph for Retrieval Augmented Generation

BYOKG-RAG Architecture

BYOKG-RAG is a novel approach to Knowledge Graph Question Answering (KGQA) that combines the power of Large Language Models (LLMs) with structured knowledge graphs. The system allows users to bring their own knowledge graph and perform complex question answering over it.

Key Features

  • Multi-strategy Retrieval: Combines multiple retrieval strategies through iterative processing:
    • Agentic triplet retrieval for LLM-guided dynamic graph exploration
    • Scoring-based triplet retrieval for semantic-based triplet retrieval
    • Path-based retrieval for multi-hop reasoning through entity paths
    • Query-based retrieval for direct Cypher graph queries
  • Iterative Processing: Uses iterative approach combining multi-strategy and Cypher-based retrieval
  • LLM-powered Reasoning: Leverages state-of-the-art LLMs for question understanding and answer generation

Prerequisites

Python Version

Python 3.10 or higher is required.

AWS Services

The byokg-rag library integrates with the following AWS services:

  • Amazon Bedrock - Provides access to foundation models for LLM inference and embeddings
  • Amazon Neptune Analytics - Graph analytics service with native vector search (optional)
  • Amazon Neptune Database - Graph database service for transactional workloads (optional)
  • Amazon S3 - Object storage for data loading and embedding storage

NOTE: You can use the local graph store for development without AWS services. Production deployments typically use Neptune Analytics or Neptune Database.

IAM Permissions

Minimum IAM permissions required for AWS integration:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "bedrock:InvokeModel"
      ],
      "Resource": "arn:aws:bedrock:<region>::foundation-model/*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "neptune-graph:ReadDataViaQuery",
        "neptune-graph:GetGraph"
      ],
      "Resource": "arn:aws:neptune-graph:<region>:<account-id>:graph/*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:PutObject"
      ],
      "Resource": "arn:aws:s3:::<bucket-name>/*"
    }
  ]
}

NOTE: Additional permissions may be required for Neptune Database (neptune-db:*) or specific Bedrock models. Adjust the policy based on your deployment.

System Components

  1. ByoKGQueryEngine (src/graphrag_toolkit/byokg_rag/byokg_query_engine.py)

    • Core orchestrating component with dual-mode processing
    • Implements iterative retrieval with configurable iterations
    • Combines multi-strategy and Cypher-based approaches
  2. KG Linkers (src/graphrag_toolkit/byokg_rag/graph_connectors)

    • KGLinker: Base class for LLM-guided graph operations
    • CypherKGLinker: Specialized for Cypher query generation and execution
    • Links natural language queries to graph entities and relationships
  3. Graph Retrievers (src/graphrag_toolkit/byokg_rag/graph_retrievers)

    • AgenticRetriever: LLM-guided iterative exploration with entity linking
    • PathRetriever: Multi-hop reasoning through entity relationship paths
    • GraphQueryRetriever: Direct Cypher query execution and result processing
    • TextTripletRetriever: Ranks every triplet by similarity to the question, independent of entity linking
    • Rerankers: BGE-based semantic reranking for improving retrieval relevance
  4. Graph Store (src/graphrag_toolkit/byokg_rag/graphstore)

    • Manages knowledge graph data structure and connectivity
    • Provides interfaces for graph traversal and querying
    • Supports multiple graph database backends

Security: Query Modification Protection

The GraphQueryRetriever blocks Cypher queries containing modification keywords (CREATE, INSERT, MERGE, DELETE, SET, REMOVE, DROP, DETACH, CALL) to protect against LLM-generated or user-supplied queries that could modify graph data. The check removes comments outside string literals before matching, so a comment marker inside a string literal can't hide a keyword ('//' CREATE ...), and a comment between or inside keywords (CRE/**/ATE, SET/**/n) can't hide a keyword either.

Treat is_query_safe() as defence in depth. A keyword check has to match each engine's lexer exactly. To block writes, rely on a read-only IAM role, described below.

Neptune Database and Neptune Analytics (Graph): Neither query API accepts a readOnly parameter, so the only write protection inside the toolkit is the keyword check (is_query_safe()). The store logs a warning the first time it runs a query with read_only=True. To have the server enforce read-only access, give the client role neptune-db:ReadDataViaQuery and neptune-db:GetGraphSummary (Neptune DB) or neptune-graph:ReadDataViaQuery and neptune-graph:GetGraph (Neptune Analytics), without WriteDataViaQuery or DeleteDataViaQuery. Writes then fail with AccessDeniedException.

Performance

Our results show that BYOKG-RAG outperforms existing approaches across multiple knowledge graph benchmarks:

KGQA Hit (%) Wiki-KG Temp-KG Med-KG
Agent 77.8 57.3 59.2
BYOKG-RAG 80.1 65.5 65.0

See our paper for detailed methodology and results.

Getting Started

The byokg-rag toolkit requires Python and pip to install. You can install from PyPI:

pip install graphrag-byokg

Or install from source:

pip install .

Or install from GitHub:

pip install https://github.com/awslabs/graphrag-toolkit/archive/refs/tags/graphrag-byokg/v3.21.0.zip#subdirectory=byokg-rag

NOTE: The version number will vary based on the latest GitHub release.

Quick Start

Run the demo notebooks:

Configuration Reference

Complete documentation is available in the docs/byokg-rag/ directory:

  • Overview - Architecture, KGQA approach, and system components
  • Indexing - Dense index, fuzzy string index, and graph-store index setup
  • Graph Stores - Supported graph stores and connection setup
  • Configuration - Complete parameter documentation
  • FAQ - Common questions and troubleshooting

Examples

Additional examples are available in the examples/byokg-rag/ directory.

Unit testing

The complete unit tests can be found under tests/, please see `tests/README.md for more details.

Citation

If you use BYOKG-RAG in your research, please cite our paper (to appear in EMNLP Main 2025):

Paper: BYOKG-RAG: Multi-Strategy Graph Retrieval for Knowledge Graph Question Answering

@article{mavromatis2025byokg,
  title={BYOKG-RAG: Multi-Strategy Graph Retrieval for Knowledge Graph Question Answering},
  author={Mavromatis, Costas and Adeshina, Soji and Ioannidis, Vassilis N and Han, Zhen and Zhu, Qi and Robinson, Ian and Thompson, Bryan and Rangwala, Huzefa and Karypis, George},
  journal={arXiv preprint arXiv:2507.04127},
  year={2025}
}

License

This project is licensed under the Apache-2.0 License.

Metadata

Release files for graphrag-byokg 3.21.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graphrag-byokg 3.21.0
File Size Uploaded Provenance Actions
graphrag_byokg-3.21.0.tar.gz 129.7 kB With provenance Details for graphrag_byokg-3.21.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for graphrag-byokg 3.21.0
File Interpreter ABI Platform Actions
graphrag_byokg-3.21.0-py3-none-any.whl Python 3 none any Details for graphrag_byokg-3.21.0-py3-none-any.whl

Total release size: 200.9 kB

Release files / graphrag_byokg-3.21.0.tar.gz

Download URL graphrag_byokg-3.21.0.tar.gz
Size 129.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b17c414e581bbfb5a2862c29bba614813a952bee749d693972ee96ebff0bd0f7
BLAKE2b-256 checksum
How to use checksums
2265f53fd0e464372f7b0416eb2678b383aee06ffc7dd3fac1a6ad5fbbeb42f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / graphrag_byokg-3.21.0-py3-none-any.whl

Download URL graphrag_byokg-3.21.0-py3-none-any.whl
Size 71.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
83bdaa2fd75f51f291eb0616ac97ae44883b26791ad9c005c83cbf03b973f1fd
BLAKE2b-256 checksum
How to use checksums
6d2d65d8080fa3ba734077988b1f663b1cbc503e9f922329f8708cbf52359925
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Security for graphrag-byokg 3.21.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for graphrag_byokg-3.21.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page