Skip to content

Commit a65640b

Browse files
Graph rag (opea-project#1007)
Signed-off-by: Rita Brugarolas <rita.brugarolas.brufau@intel.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 7197286 commit a65640b

File tree

126 files changed

+5171
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+5171
-0
lines changed

GraphRAG/Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
FROM python:3.11-slim
5+
6+
RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
7+
git \
8+
libgl1-mesa-glx \
9+
libjemalloc-dev
10+
11+
RUN useradd -m -s /bin/bash user && \
12+
mkdir -p /home/user && \
13+
chown -R user /home/user/
14+
15+
WORKDIR /home/user/
16+
RUN git clone https://github.com/opea-project/GenAIComps.git
17+
18+
WORKDIR /home/user/GenAIComps
19+
RUN pip install --no-cache-dir --upgrade pip && \
20+
pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt && \
21+
pip install --no-cache-dir langchain_core
22+
23+
COPY ./graphrag.py /home/user/graphrag.py
24+
25+
ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
26+
27+
USER user
28+
29+
WORKDIR /home/user
30+
31+
RUN echo 'ulimit -S -n 999999' >> ~/.bashrc
32+
33+
ENTRYPOINT ["python", "graphrag.py"]

GraphRAG/README.md

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
# GraphRAG Application
2+
3+
While naive RAG works well in fetching precise information it fails on global questions directed at an entire text corpus, such as "What are the main themes in the dataset?".
4+
GraphRAG was introduced by Microsoft paper "From Local to Global: A Graph RAG Approach to Query-Focused Summarization". The key elements are:
5+
6+
- Uses LLM to derive an entity knowledge graph from the source documents
7+
- Uses hierarchical leiden algorithm to identify communities of closely-related entities and summaries are extracted for each community
8+
- For an input query the relevant communities are identified and partial answers are generated from each of the community summaries (query-focused summarization (QFS))
9+
- There is a final generation stage that responds to the query based on the intermediate community answers.
10+
11+
## Deploy GraphRAG Service
12+
13+
The GraphRAG service can be effortlessly deployed on Intel Gaudi2, Intel Xeon Scalable Processors.
14+
15+
Quick Start Deployment Steps:
16+
17+
1. Set up the environment variables.
18+
2. Run Docker Compose.
19+
3. Consume the GraphRAG Service.
20+
21+
Note: If you do not have docker installed you can run this script to install docker : `bash docker_compose/install_docker.sh`
22+
23+
### Quick Start: 1.Setup Environment Variable
24+
25+
To set up environment variables for deploying GraphRAG services, follow these steps:
26+
27+
1. Set the required private environment variables:
28+
29+
```bash
30+
export host_ip=${your_hostname IP} #local IP, i.e "192.168.1.1"
31+
export NEO4J_URI=${your_neo4j_url}
32+
export NEO4J_USERNAME=${your_neo4j_username}
33+
export NEO4J_PASSWORD=${your_neo4j_password}
34+
export PYTHONPATH=${path_to_comps}
35+
export OPENAI_KEY=${your_openai_api_key} #optional, when not provided will use smaller models TGI/TEI
36+
export HUGGINGFACEHUB_API_TOKEN=${your_hf_token} #needed for TGI/TEI models
37+
```
38+
39+
2. If you are in a proxy environment, also set the proxy-related environment variables:
40+
41+
```bash
42+
export http_proxy="Your_HTTP_Proxy"
43+
export https_proxy="Your_HTTPs_Proxy"
44+
export no_proxy=$no_proxy,${host_ip} #important to add {host_ip} for containers communication
45+
```
46+
47+
3. Set up other environment variables:
48+
49+
```bash
50+
# on Gaudi
51+
source ./docker_compose/intel/hpu/gaudi/set_env.sh
52+
```
53+
54+
### Quick Start: 2.Run Docker Compose
55+
56+
If the microservice images are available in Docker Hub they will be pulled, otherwise you will need to build the container images manually. Please refer to the 'Build Docker Images' in [Guide](../ChatQnA/docker_compose/intel/cpu/xeon/README.md). [test_compose.sh](tests/test_compose.sh) can be a good resource as it shows how to do image build, starting services, validated each microservices and megaservices. This is what is used in CI/CD.
57+
58+
Docker compose will start 8 services: ![8 servicesi in GraphRAG](assets/8microservices.png)
59+
60+
```bash
61+
cd GraphRAG/docker_compose/intel/hpu/gaudi
62+
docker compose -f compose.yaml up -d
63+
```
64+
65+
### QuickStart: 3.Upload RAG Files and Consume the GraphRAG Service
66+
67+
To chat with retrieved information, you need to upload a file using `Dataprep` service.
68+
69+
Here is an example of `Nike 2023` pdf.
70+
71+
```bash
72+
# download pdf file
73+
wget https://raw.githubusercontent.com/opea-project/GenAIComps/main/comps/retrievers/redis/data/nke-10k-2023.pdf
74+
# upload pdf file with dataprep
75+
curl -X POST "http://${host_ip}:6004/v1/dataprep" \
76+
-H "Content-Type: multipart/form-data" \
77+
-F "files=@./nke-10k-2023.pdf"
78+
```
79+
80+
```bash
81+
curl http://${host_ip}:8888/v1/graphrag \
82+
-H "Content-Type: application/json" \
83+
-d '{
84+
"model": "gpt-4o-mini","messages": [{"role": "user","content": "What is the revenue of Nike in 2023?
85+
"}]}'
86+
```
87+
88+
## Architecture and Deploy details
89+
90+
The GraphRAG example is implemented using the component-level microservices defined in [GenAIComps](https://github.com/opea-project/GenAIComps). The flow chart below shows the information flow between different microservices for this example.
91+
92+
```mermaid
93+
---
94+
config:
95+
flowchart:
96+
nodeSpacing: 400
97+
rankSpacing: 100
98+
curve: linear
99+
themeVariables:
100+
fontSize: 50px
101+
---
102+
flowchart LR
103+
%% Colors %%
104+
classDef blue fill:#ADD8E6,stroke:#ADD8E6,stroke-width:2px,fill-opacity:0.5
105+
classDef orange fill:#FBAA60,stroke:#ADD8E6,stroke-width:2px,fill-opacity:0.5
106+
classDef orchid fill:#C26DBC,stroke:#ADD8E6,stroke-width:2px,fill-opacity:0.5
107+
classDef invisible fill:transparent,stroke:transparent;
108+
style GraphRAG-MegaService stroke:#000000
109+
110+
%% Subgraphs %%
111+
subgraph GraphRAG-MegaService["GraphRAG MegaService "]
112+
direction LR
113+
RET([Retrieval MicroService]):::blue
114+
LLM([LLM MicroService]):::blue
115+
EM([Embedding MicroService]):::blue
116+
end
117+
subgraph UserInterface[" User Interface "]
118+
direction LR
119+
a([User Input Query]):::orchid
120+
Ingest([Ingest data]):::orchid
121+
UI([UI server<br>]):::orchid
122+
end
123+
124+
125+
GDB{{Graph DB<br><br>}}
126+
DP([Data Preparation MicroService]):::blue
127+
GW([GraphRAG GateWay<br>]):::orange
128+
129+
130+
%% Data Preparation flow
131+
%% Ingest data flow
132+
direction LR
133+
Ingest[Ingest data] --> UI
134+
UI --> DP
135+
136+
%% interactions buried inside the DP and RET microservice implementations
137+
DP <-.-> EM
138+
DP <-.-> LLM
139+
RET <-.-> EM
140+
RET <-.-> LLM
141+
142+
143+
%% Questions interaction
144+
direction LR
145+
a[User Input Query] --> UI
146+
UI --> GW
147+
GW <==> GraphRAG-MegaService
148+
RET ==> LLM
149+
150+
151+
direction TB
152+
%% Graph DB interaction
153+
RET <-.-> |d|GDB
154+
DP <-.-> |d|GDB
155+
156+
linkStyle 2 stroke:#000000,stroke-width:2px;
157+
linkStyle 3 stroke:#000000,stroke-width:2px;
158+
linkStyle 4 stroke:#000000,stroke-width:2px;
159+
linkStyle 5 stroke:#000000,stroke-width:2px;
160+
161+
162+
```
163+
164+
> **Note**: The Dataprep and Retriever microservices use the LLM Microservice and Embedding Microservice in their implementation. For example, Dataprep uses LLM to extract entities and relationships from text to build graph and Retriever uses LLM to summarize communities (these are clusters of similar entities and their properties). Those endpoint interactions with the corresponding prompt templates are buried in the microservice implementation thus not managed by the megaservice orchestrator scheduler and not exposed in the megaservice. Shown as thin black lines in diagram.
165+
166+
This GraphRAG use case performs RAG using Llama-index, Neo4J Graph Property Store and Text Generation Inference on [Intel Gaudi2](https://www.intel.com/content/www/us/en/products/details/processors/ai-accelerators/gaudi-overview.html) or [Intel Xeon Scalable Processors](https://www.intel.com/content/www/us/en/products/details/processors/xeon.html).
167+
In the below, we provide a table that describes for each microservice component in the GraphRAG architecture, the default configuration of the open source project, hardware, port, and endpoint.
168+
169+
Gaudi default compose.yaml
170+
| MicroService | Open Source Project | HW | Port | Endpoint |
171+
| ------------ | ------------------- | ----- | ---- | -------------------- |
172+
| Embedding | Llama-index | Xeon | 6006 | /v1/embaddings |
173+
| Retriever | Llama-index, Neo4j | Xeon | 6009 | /v1/retrieval |
174+
| LLM | Llama-index, TGI | Gaudi | 6005 | /v1/chat/completions |
175+
| Dataprep | Neo4j, LlamaIndex | Xeon | 6004 | /v1/dataprep |
176+
177+
### Models Selection
178+
179+
GraphRAG quality dependents heavily on the ability to extract a high quality graph. We highly recommend using the best model available to you. Table below shows default models specified in the codebase when OPENAI_API_KEY is available and for local inference w TEI/TGI. The local models are small since those will be used in CI/CD but users should improve upon these by changing the `xxx_MODEL_ID` in `docker_compose/xxx/set_env.sh`.
180+
181+
Working on a table comparison of various model sizes vs. naive RAG with a dataset that reflects well the benefits of GraphRAG. Stay tuned!
182+
183+
| Service | Model |
184+
| --------- | ------------------------------------- |
185+
| Embedding | BAAI/bge-base-en-v1.5 |
186+
| Embedding | "text-embedding-3-small" |
187+
| LLM | gpt-4o |
188+
| LLM | "meta-llama/Meta-Llama-3-8B-Instruct" |
189+
190+
## Consume GraphRAG Service with RAG
191+
192+
### Check Service Status
193+
194+
Before consuming GraphRAG Service, make sure each microservice is ready by checking the docker logs of each microservice. [test_compose.sh](tests/test_compose.sh) can be a good resource as it shows how CI/CD validated each microservices based on returned HTTP status and response body.
195+
196+
```bash
197+
docker logs container_name
198+
```
199+
200+
### Upload RAG Files
201+
202+
To chat with retrieved information, you need to upload a file using `Dataprep` service.
203+
204+
Here is an example of `Nike 2023` pdf.
205+
206+
```bash
207+
# download pdf file
208+
wget https://raw.githubusercontent.com/opea-project/GenAIComps/main/comps/retrievers/redis/data/nke-10k-2023.pdf
209+
# upload pdf file with dataprep
210+
curl -X POST "http://${host_ip}:6007/v1/dataprep" \
211+
-H "Content-Type: multipart/form-data" \
212+
-F "files=@./nke-10k-2023.pdf"
213+
```
214+
215+
### Consume GraphRAG Service
216+
217+
Two ways of consuming GraphRAG Service:
218+
219+
1. Use cURL command on terminal
220+
221+
```bash
222+
curl http://${host_ip}:8888/v1/graphrag \
223+
-H "Content-Type: application/json" \
224+
-d '{
225+
"model": "gpt-4o-mini","messages": [{"role": "user","content": "Who is John Brady and has he had any confrontations?
226+
"}]}'
227+
```
228+
229+
2. Access via frontend
230+
231+
To access the frontend, open the following URL in your browser: `http://{host_ip}:5173`
232+
233+
By default, the UI runs on port 5173 internally.
234+
235+
If you choose conversational UI, use this URL: `http://{host_ip}:5174`
236+
237+
## Troubleshooting
238+
239+
1. If you get errors like "Access Denied", [validate micro service](https://github.com/opea-project/GenAIExamples/blob/main/ChatQnA/docker_compose/intel/cpu/xeon/README.md#validate-microservices) first. A simple example:
240+
241+
```bash
242+
http_proxy="" curl ${host_ip}:6006/embed -X POST -d '{"inputs":"What is Deep Learning?"}' -H 'Content-Type: application/json'
243+
```
244+
245+
2. (Docker only) If all microservices work well, check the port ${host_ip}:8888, the port may be allocated by other users, you can modify the `compose.yaml`.
246+
247+
3. (Docker only) If you get errors like "The container name is in use", change container name in `compose.yaml`.
248+
249+
## Monitoring OPEA Service with Prometheus and Grafana dashboard
250+
251+
OPEA microservice deployment can easily be monitored through Grafana dashboards in conjunction with Prometheus data collection. Follow the [README](https://github.com/opea-project/GenAIEval/blob/main/evals/benchmark/grafana/README.md) to setup Prometheus and Grafana servers and import dashboards to monitor the OPEA service.
252+
253+
![chatqna dashboards](../ChatQnA/assets/img/chatqna_dashboards.png)
254+
![tgi dashboard](../ChatQnA//assets/img/tgi_dashboard.png)

GraphRAG/assets/8microservices.png

173 KB
Loading

GraphRAG/assets/img/chat_ui_init.png

14.8 KB
Loading
74.3 KB
Loading
85.9 KB
Loading
501 KB
Loading
100 KB
Loading
115 KB
Loading
53.5 KB
Loading
92.7 KB
Loading
61.6 KB
Loading

GraphRAG/assets/img/tgi_dashboard.png

414 KB
Loading
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright (C) 2024 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
# Update the package index
7+
sudo apt-get -y update
8+
9+
# Install prerequisites
10+
sudo apt-get -y install ca-certificates curl --no-install-recommends --fix-missing
11+
12+
# Create the directory for the Docker GPG key
13+
sudo install -m 0755 -d /etc/apt/keyrings
14+
15+
# Add Docker's official GPG key
16+
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
17+
18+
# Set permissions for the GPG key
19+
sudo chmod a+r /etc/apt/keyrings/docker.asc
20+
21+
# Add Docker repository to the sources list
22+
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
23+
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
24+
25+
# Update the package index with Docker packages
26+
sudo apt-get -y update
27+
28+
# Install Docker packages
29+
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin --no-install-recommends --fix-missing
30+
31+
# add existing user
32+
sudo usermod -aG docker $USER
33+
34+
# Optional: Verify that Docker is installed correctly
35+
sudo docker --version

0 commit comments

Comments
 (0)