Skip to content

Commit 550d3fc

Browse files
committed
Add GraphRAG implementation (Issue #103)
1 parent 4675e46 commit 550d3fc

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

GraphRAG/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# GraphRAG
2+
3+
GraphRAG implementation using Llama-Index and NetworkX.
4+
5+
## Features
6+
- Builds a knowledge graph from documents
7+
- Indexes data points in the graph
8+
- Retrieves and generates contextually relevant responses using an LLM
9+
- Visualizes the knowledge graph
10+
11+
## Usage
12+
1. Install dependencies:
13+
```bash
14+
pip install llama-index networkx
15+
```
16+
2. Run `graphrag.py`.
17+
3. Example query and knowledge graph visualization will be generated.
18+
19+
## Requirements
20+
- Python 3.x
21+
- llama-index
22+
- networkx
23+
- OpenAI API key (for LLM and embeddings)
24+
25+
## License
26+
MIT

GraphRAG/graphrag.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
GraphRAG Implementation using Llama-Index and NetworkX
3+
Issue #103 for king04aman/All-In-One-Python-Projects
4+
"""
5+
import networkx as nx
6+
from llama_index.core import KnowledgeGraphIndex, SimpleNodeParser, QueryEngine
7+
from llama_index.llms.openai import OpenAI
8+
from llama_index.embeddings.openai import OpenAIEmbedding
9+
10+
# Example documents
11+
DOCUMENTS = [
12+
"Alice is a data scientist. She works at Acme Corp.",
13+
"Bob is a software engineer. He collaborates with Alice on ML projects.",
14+
"Acme Corp is a tech company based in New York."
15+
]
16+
17+
# Step 1: Parse documents into nodes
18+
parser = SimpleNodeParser()
19+
nodes = parser.get_nodes_from_documents(DOCUMENTS)
20+
21+
# Step 2: Build Knowledge Graph Index
22+
kg_index = KnowledgeGraphIndex(nodes)
23+
24+
# Step 3: Create Query Engine (using OpenAI LLM and embeddings)
25+
llm = OpenAI(model="gpt-3.5-turbo")
26+
embed_model = OpenAIEmbedding(model="text-embedding-ada-002")
27+
query_engine = QueryEngine(kg_index, llm=llm, embed_model=embed_model)
28+
29+
# Step 4: Example query
30+
query = "Who works at Acme Corp?"
31+
response = query_engine.query(query)
32+
print("Query:", query)
33+
print("Response:", response)
34+
35+
# Step 5: Visualize Knowledge Graph
36+
G = nx.Graph()
37+
for node in nodes:
38+
for rel in node.relationships:
39+
G.add_edge(node.entity, rel.entity, label=rel.type)
40+
41+
nx.write_gml(G, "knowledge_graph.gml")
42+
print("Knowledge graph saved as knowledge_graph.gml")

GraphRAG/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
llama-index
2+
networkx

0 commit comments

Comments
 (0)