ayushnoori / KG_RAG

This repository holds the scripts that implement Knowledge Graph based Retrieval-Augmented Generation for Large Language Models

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Knowledge Graph-based Retrieval Augmented Generation (KG-RAG)

Table of Contents

What is KG-RAG

Example use case of KG-RAG

How to run KG-RAG

What is KG-RAG?

KG-RAG stands for Knowledge Graph-based Retrieval Augmented Generation.

Start by watching the video of KG-RAG

video.mov

It is a task agnostic framework that combines the explicit knowledge of a Knowledge Graph (KG) with the implicit knowledge of a Large Language Model (LLM).

Here, we utilize a massive biomedical KG called SPOKE as the provider for the biomedical context. SPOKE has incorporated over 40 biomedical knowledge repositories from diverse domains, each focusing on biomedical concept like genes, proteins, drugs, compounds, diseases, and their established connections. SPOKE consists of more than 27 million nodes of 21 different types and 53 million edges of 55 types [Ref]

The main feature of KG-RAG is that it extracts "prompt-aware context" from SPOKE KG, which is defined as:

the minimal context sufficient enough to respond to the user prompt.

Hence, this framework empowers a general-purpose LLM by incorporating an optimized domain-specific 'prompt-aware context' from a biomedical KG.

Example use case of KG-RAG

Following snippet shows the news from FDA website about the drug "setmelanotide" approved by FDA for weight management in patients with Bardet-Biedl Syndrome

Ask GPT-3.5-Turbo about the above drug:

WITHOUT KG-RAG

Note: We are prompting GPT from the terminal, NOT from the chatGPT browser. Temperature parameter is set to 0 for all the analysis. Refer this yaml file for parameter setting

prompt_based_use_case.mov

WITH KG-RAG

Note: Temperature parameter is set to 0 for all the analysis. Refer this yaml file for parameter setting

KG_RAG_based_use_case_2.mov

You can see that, KG-RAG was able to give the correct information about the FDA approved drug.

How to run KG-RAG

Step 1: Clone the repo

Clone this repository. All Biomedical data used in the paper are uploaded to this repository, hence you don't have to download that separately.

Step 2: Create a virtual environment

Note: Scripts in this repository were run using python 3.10.9

conda create -n kg_rag python=3.10.9
conda activate kg_rag
cd KG_RAG

Step 3: Install dependencies

pip install -r requirements.txt

Step 4: Update config.yaml

config.yaml holds all the necessary information required to run the scripts in your machine. Make sure to populate this yaml file accordingly.

Note: There is another yaml file called system_prompts.yaml. This is already populated and it holds all the system prompts used in the KG-RAG framework.

Step 5: Run the setup script

Note: Make sure you are in KG_RAG folder

Setup script runs in an interactive fashion.

Running the setup script will:

  • create disease vector database for KG-RAG
  • download Llama model in your machine (optional, you can skip this and that is totally fine)
python -m kg_rag.run_setup

Step 6: Run KG-RAG from your terminal

Note: Make sure you are in KG_RAG folder

You can run KG-RAG using GPT and Llama model.

Using GPT

python -m kg_rag.rag_based_generation.GPT.text_generation <your favorite gpt model - "gpt-4" or "gpt-35-turbo">

Example:

Note: The following example was run on AWS p3.8xlarge EC2 instance.

KG_RAG_PD_variants_2.mov

Using GPT interactive mode

This allows the user to go over each step of the process

python -m kg_rag.rag_based_generation.GPT.text_generation <your favorite gpt model - "gpt-4" or "gpt-35-turbo"> interactive

Using Llama

Note: If you haven't downloaded Llama during setup step, then when you run the following, it may take sometime since it will download the model first.

python -m kg_rag.rag_based_generation.Llama.text_generation

Example:

Note: The following example was run on AWS p3.8xlarge EC2 instance.

KG_RAG_Llama_2.mov

Using Llama interactive mode

This allows the user to go over each step of the process

python -m kg_rag.rag_based_generation.Llama.text_generation interactive

About

This repository holds the scripts that implement Knowledge Graph based Retrieval-Augmented Generation for Large Language Models

License:Apache License 2.0


Languages

Language:Python 100.0%