-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphrag.py
250 lines (200 loc) · 7.53 KB
/
graphrag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import os
from typing import List
from dotenv import load_dotenv
import json
from networkx import Graph
from openai import OpenAI
import networkx as nx
from cdlib import algorithms
import instructor
import tiktoken
from data_types import Object, ObjectType, Summary
from prompts import (
OBJECT_EXTRACTION_PROMPT,
SUMMARY_PROMPT,
)
load_dotenv()
client = instructor.from_openai(
OpenAI(
base_url=os.getenv("OPENAI_API_BASE", "https://api.openai.com/v1"),
api_key=os.getenv("OPENAI_API_KEY"),
)
)
model = "gpt-4o"
encoding = tiktoken.encoding_for_model("gpt-4o")
def chunk_text(texts: List[str], size: int = 600, overlap: int = 100) -> List[str]:
chunks = []
for text in texts:
tokens = encoding.encode(text)
for i in range(0, len(tokens), size - overlap):
chunk_tokens = tokens[i : i + size]
chunk_text = encoding.decode(chunk_tokens)
chunks.append(chunk_text)
return chunks
def extract_objects(chunks: List[str]) -> List[Object]:
objects = []
for chunk in chunks:
response = client.chat.completions.create(
response_model=List[Object],
model=model,
messages=[
{"role": "system", "content": OBJECT_EXTRACTION_PROMPT},
{"role": "user", "content": chunk},
],
)
objects += response
return objects
def summarise_objects(objects: List[Object], batch_size: int = 100) -> List[Object]:
summarised_objects = []
for i in range(0, len(objects), batch_size):
objects_to_summarise = objects[i : min(i + batch_size, len(objects))]
objs = []
for obj in objects_to_summarise:
if obj.type == ObjectType.ENTITY:
e = obj.object
objs.append(f"Entity {e.name} ({e.type}): {e.description}")
elif obj.type == ObjectType.RELATIONSHIP:
r = obj.object
objs.append(
f"Relationship from {r.from_entity.name} to {r.to_entity.name} with label {r.label} and strength {r.strength}"
)
objs_str = "\n".join(objs)
response = client.chat.completions.create(
response_model=List[Object],
model=model,
messages=[
{"role": "system", "content": SUMMARY_PROMPT},
{"role": "user", "content": objs_str},
],
)
summarised_objects += response
return summarised_objects
def build_graph(summaries: list[Object]) -> Graph:
G = nx.Graph()
for object in summaries:
if object.type == ObjectType.ENTITY:
G.add_node(object.object.name)
elif object.type == ObjectType.RELATIONSHIP:
G.add_edge(
object.object.from_entity.name,
object.object.to_entity.name,
desc=object.object.label,
strength=object.object.strength,
)
return G
def get_communities_from_graph(graph: Graph):
all_communities = []
for cc in nx.connected_components(graph):
subgraph = graph.subgraph(cc)
if len(subgraph.nodes) > 1:
try:
leiden_communities = algorithms.leiden(subgraph)
for community in leiden_communities.communities:
all_communities.append(community)
except Exception as e:
print(f"Error processing community: {e}")
else:
all_communities.append(list(subgraph.nodes))
return all_communities
def summarise_communities(communities, graph):
summaries = []
for community in communities:
subgraph = graph.subgraph(community)
# Prepare community information
nodes = list(subgraph.nodes())
edges = list(subgraph.edges(data=True))
# Create a description of the community
community_desc = (
f"This community consists of {len(nodes)} members: {', '.join(nodes)}.\n"
)
# Add information about relationships
relationships = []
for edge in edges:
source, target, data = edge
desc = data.get("desc", "connected")
strength = data.get("strength", 0)
label = data.get("label", "")
relationships.append(
f"{source} is {desc} to {target} (strength: {strength:.1f}, label: {label})"
)
community_desc += "Relationships:\n- " + "\n- ".join(relationships)
# Generate summary using an LLM (replace this with your actual LLM call)
response = client.chat.completions.create(
response_model=Summary,
model=model,
messages=[
{
"role": "system",
"content": "Summarise the following community based on the entities and relationships.",
},
{"role": "user", "content": community_desc},
],
)
summary = response.summary
summaries.append(summary)
return summaries
def answer_user_query(community_summaries, query):
community_answers = []
for summary in community_summaries:
response = client.chat.completions.create(
response_model=str,
model=model,
messages=[
{
"role": "system",
"content": f"Answer the user query using the provided summary: {summary}",
},
{"role": "user", "content": query},
],
)
community_answers.append(response)
global_answer = client.chat.completions.create(
response_model=str,
model=model,
messages=[
{
"role": "system",
"content": "Combine these answers into a single concise response.",
},
{"role": "user", "content": f"Answers: {community_answers}"},
],
)
return global_answer
def main():
texts = []
dir = "input"
# Section 2.1: Source Documents -> Text Chunks
for file in os.listdir(dir):
if file.endswith(".txt"):
path = os.path.join(dir, file)
with open(path, "r") as f:
texts.append(f.read())
chunks = chunk_text(texts)
print(f"Texts chunked in {len(chunks)} chunks.")
# Section 2.2: Text Chunks -> Element Instances
objects = extract_objects(chunks)
with open("objects.jsonl", "w") as f:
for object in objects:
json.dump(object.object.model_dump(), f)
f.write("\n")
print(f"{len(objects)} objects found.")
# Section 2.3: Element Instances -> Element Summaries
summaries = summarise_objects(objects)
with open("summarised_objects.jsonl", "w") as f:
for summary in summaries:
json.dump(summary.object.model_dump(), f)
f.write("\n")
print(f"{len(summaries)} summarised objects found.")
# Section 2.4: Element Summaries -> Graph Communities
g = build_graph(summaries)
communities = get_communities_from_graph(g)
print(f"{len(communities)} communities found.")
# Section 2.5: Graph Communities -> Community Summaries
community_summaries = summarise_communities(communities=communities, graph=g)
print(f"{len(community_summaries)} community summaries generated.")
# Section 2.6: Community Summaries → Community Answers → Global Answer
query = "What are the top themes in this text?"
answer = answer_user_query(community_summaries=community_summaries, query=query)
print(answer)
if __name__ == "__main__":
main()