This repository was archived by the owner on May 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 421
Expand file tree
/
Copy pathquery_streaming.py
More file actions
455 lines (415 loc) · 19 KB
/
query_streaming.py
File metadata and controls
455 lines (415 loc) · 19 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import inspect
import json
import os
import traceback
import pandas as pd
import yaml
from fastapi import (
APIRouter,
Depends,
HTTPException,
)
from fastapi.responses import StreamingResponse
from graphrag.api.query import (
global_search_streaming as global_search_streaming_internal,
)
from graphrag.api.query import (
local_search_streaming as local_search_streaming_internal,
)
from graphrag.config import create_graphrag_config
from graphrag_app.api.query import _is_index_complete
from graphrag_app.logger.load_logger import load_pipeline_logger
from graphrag_app.typing.models import GraphRequest
from graphrag_app.utils.azure_clients import AzureClientManager
from graphrag_app.utils.common import (
get_df,
sanitize_name,
subscription_key_check,
validate_index_file_exist,
)
from .query import _get_embedding_description_store, _update_context
query_streaming_route = APIRouter(
prefix="/query/streaming",
tags=["Query Streaming Operations"],
)
if os.getenv("KUBERNETES_SERVICE_HOST"):
query_streaming_route.dependencies.append(Depends(subscription_key_check))
@query_streaming_route.post(
"/global",
summary="Stream a response back after performing a global search",
description="The global query method generates answers by searching over all AI-generated community reports in a map-reduce fashion. This is a resource-intensive method, but often gives good responses for questions that require an understanding of the dataset as a whole.",
)
async def global_search_streaming(request: GraphRequest):
# this is a slightly modified version of graphrag_app.api.query.global_query() method
if isinstance(request.index_name, str):
index_names = [request.index_name]
else:
index_names = request.index_name
sanitized_index_names = [sanitize_name(name) for name in index_names]
sanitized_index_names_link = {
s: i for s, i in zip(sanitized_index_names, index_names)
}
for index_name in sanitized_index_names:
if not _is_index_complete(index_name):
raise HTTPException(
status_code=500,
detail=f"{sanitized_index_names_link[index_name]} not ready for querying.",
)
COMMUNITY_REPORT_TABLE = "output/create_final_community_reports.parquet"
ENTITIES_TABLE = "output/create_final_entities.parquet"
NODES_TABLE = "output/create_final_nodes.parquet"
if isinstance(request.community_level, int):
COMMUNITY_LEVEL = request.community_level
else:
# Current investigations show that community level 1 is the most useful for global search. Set this as the default value
COMMUNITY_LEVEL = 1
for index_name in sanitized_index_names:
validate_index_file_exist(index_name, COMMUNITY_REPORT_TABLE)
validate_index_file_exist(index_name, ENTITIES_TABLE)
validate_index_file_exist(index_name, NODES_TABLE)
try:
links = {
"nodes": {},
"community": {},
"entities": {},
"text_units": {},
"relationships": {},
"covariates": {},
}
max_vals = {
"nodes": -1,
"community": -1,
"entities": -1,
"text_units": -1,
"relationships": -1,
"covariates": -1,
}
community_dfs = []
entities_dfs = []
nodes_dfs = []
for index_name in sanitized_index_names:
community_report_table_path = (
f"abfs://{index_name}/{COMMUNITY_REPORT_TABLE}"
)
entities_table_path = f"abfs://{index_name}/{ENTITIES_TABLE}"
nodes_table_path = f"abfs://{index_name}/{NODES_TABLE}"
# read parquet files into DataFrames and add provenance information
# note that nodes need to set before communities to that max community id makes sense
nodes_df = get_df(nodes_table_path)
for i in nodes_df["human_readable_id"]:
links["nodes"][i + max_vals["nodes"] + 1] = {
"index_name": sanitized_index_names_link[index_name],
"id": i,
}
if max_vals["nodes"] != -1:
nodes_df["human_readable_id"] += max_vals["nodes"] + 1
nodes_df["community"] = nodes_df["community"].apply(
lambda x: str(int(x) + max_vals["community"] + 1) if x else x
)
nodes_df["title"] = nodes_df["title"].apply(lambda x: x + f"-{index_name}")
nodes_df["source_id"] = nodes_df["source_id"].apply(
lambda x: ",".join([i + f"-{index_name}" for i in x.split(",")])
)
max_vals["nodes"] = nodes_df["human_readable_id"].max()
nodes_dfs.append(nodes_df)
community_df = get_df(community_report_table_path)
for i in community_df["community"].astype(int):
links["community"][i + max_vals["community"] + 1] = {
"index_name": sanitized_index_names_link[index_name],
"id": str(i),
}
if max_vals["community"] != -1:
col = community_df["community"].astype(int) + max_vals["community"] + 1
community_df["community"] = col.astype(str)
max_vals["community"] = community_df["community"].astype(int).max()
community_dfs.append(community_df)
entities_df = get_df(entities_table_path)
for i in entities_df["human_readable_id"]:
links["entities"][i + max_vals["entities"] + 1] = {
"index_name": sanitized_index_names_link[index_name],
"id": i,
}
if max_vals["entities"] != -1:
entities_df["human_readable_id"] += max_vals["entities"] + 1
entities_df["name"] = entities_df["name"].apply(
lambda x: x + f"-{index_name}"
)
entities_df["text_unit_ids"] = entities_df["text_unit_ids"].apply(
lambda x: [i + f"-{index_name}" for i in x]
)
max_vals["entities"] = entities_df["human_readable_id"].max()
entities_dfs.append(entities_df)
# merge the dataframes
nodes_combined = pd.concat(nodes_dfs, axis=0, ignore_index=True, sort=False)
community_combined = pd.concat(
community_dfs, axis=0, ignore_index=True, sort=False
)
entities_combined = pd.concat(
entities_dfs, axis=0, ignore_index=True, sort=False
)
# load custom pipeline settings
this_directory = os.path.dirname(
os.path.abspath(inspect.getfile(inspect.currentframe()))
)
data = yaml.safe_load(open(f"{this_directory}/pipeline-settings.yaml"))
# layer the custom settings on top of the default configuration settings of graphrag
parameters = create_graphrag_config(data, ".")
return StreamingResponse(
_wrapper(
global_search_streaming_internal(
config=parameters,
nodes=nodes_combined,
entities=entities_combined,
community_reports=community_combined,
community_level=COMMUNITY_LEVEL,
response_type="Multiple Paragraphs",
query=request.query,
),
links,
),
media_type="application/json",
)
except Exception as e:
logger = load_pipeline_logger()
logger.error(
message="Error encountered while streaming global search response",
cause=e,
stack=traceback.format_exc(),
)
raise HTTPException(status_code=500, detail=None)
@query_streaming_route.post(
"/local",
summary="Stream a response back after performing a local search",
description="The local query method generates answers by combining relevant data from the AI-extracted knowledge-graph with text chunks of the raw documents. This method is suitable for questions that require an understanding of specific entities mentioned in the documents (e.g. What are the healing properties of chamomile?).",
)
async def local_search_streaming(request: GraphRequest):
# this is a slightly modified version of graphrag_app.api.query.local_query() method
if isinstance(request.index_name, str):
index_names = [request.index_name]
else:
index_names = request.index_name
sanitized_index_names = [sanitize_name(name) for name in index_names]
sanitized_index_names_link = {
s: i for s, i in zip(sanitized_index_names, index_names)
}
for index_name in sanitized_index_names:
if not _is_index_complete(index_name):
raise HTTPException(
status_code=500,
detail=f"{sanitized_index_names_link[index_name]} not ready for querying.",
)
azure_client_manager = AzureClientManager()
blob_service_client = azure_client_manager.get_blob_service_client()
community_dfs = []
covariates_dfs = []
entities_dfs = []
nodes_dfs = []
relationships_dfs = []
text_units_dfs = []
links = {
"nodes": {},
"community": {},
"entities": {},
"text_units": {},
"relationships": {},
"covariates": {},
}
max_vals = {
"nodes": -1,
"community": -1,
"entities": -1,
"text_units": -1,
"relationships": -1,
"covariates": -1,
}
COMMUNITY_REPORT_TABLE = "output/create_final_community_reports.parquet"
COVARIATES_TABLE = "output/create_final_covariates.parquet"
ENTITIES_TABLE = "output/create_final_entities.parquet"
NODES_TABLE = "output/create_final_nodes.parquet"
RELATIONSHIPS_TABLE = "output/create_final_relationships.parquet"
TEXT_UNITS_TABLE = "output/create_final_text_units.parquet"
if isinstance(request.community_level, int):
COMMUNITY_LEVEL = request.community_level
else:
# Current investigations show that community level 2 is the most useful for local search. Set this as the default value
COMMUNITY_LEVEL = 2
try:
for index_name in sanitized_index_names:
# check for existence of files the query relies on to validate the index is complete
validate_index_file_exist(index_name, COMMUNITY_REPORT_TABLE)
validate_index_file_exist(index_name, ENTITIES_TABLE)
validate_index_file_exist(index_name, NODES_TABLE)
validate_index_file_exist(index_name, RELATIONSHIPS_TABLE)
validate_index_file_exist(index_name, TEXT_UNITS_TABLE)
community_report_table_path = (
f"abfs://{index_name}/{COMMUNITY_REPORT_TABLE}"
)
covariates_table_path = f"abfs://{index_name}/{COVARIATES_TABLE}"
entities_table_path = f"abfs://{index_name}/{ENTITIES_TABLE}"
nodes_table_path = f"abfs://{index_name}/{NODES_TABLE}"
relationships_table_path = f"abfs://{index_name}/{RELATIONSHIPS_TABLE}"
text_units_table_path = f"abfs://{index_name}/{TEXT_UNITS_TABLE}"
# read the parquet files into DataFrames and add provenance information
# note that nodes need to set before communities to that max community id makes sense
nodes_df = get_df(nodes_table_path)
for i in nodes_df["human_readable_id"]:
links["nodes"][i + max_vals["nodes"] + 1] = {
"index_name": sanitized_index_names_link[index_name],
"id": i,
}
if max_vals["nodes"] != -1:
nodes_df["human_readable_id"] += max_vals["nodes"] + 1
nodes_df["community"] = nodes_df["community"].apply(
lambda x: str(int(x) + max_vals["community"] + 1) if x else x
)
nodes_df["id"] = nodes_df["id"].apply(lambda x: x + f"-{index_name}")
nodes_df["title"] = nodes_df["title"].apply(lambda x: x + f"-{index_name}")
nodes_df["source_id"] = nodes_df["source_id"].apply(
lambda x: ",".join([i + f"-{index_name}" for i in x.split(",")])
)
max_vals["nodes"] = nodes_df["human_readable_id"].max()
nodes_dfs.append(nodes_df)
community_df = get_df(community_report_table_path)
for i in community_df["community"].astype(int):
links["community"][i + max_vals["community"] + 1] = {
"index_name": sanitized_index_names_link[index_name],
"id": str(i),
}
if max_vals["community"] != -1:
col = community_df["community"].astype(int) + max_vals["community"] + 1
community_df["community"] = col.astype(str)
max_vals["community"] = community_df["community"].astype(int).max()
community_dfs.append(community_df)
entities_df = get_df(entities_table_path)
for i in entities_df["human_readable_id"]:
links["entities"][i + max_vals["entities"] + 1] = {
"index_name": sanitized_index_names_link[index_name],
"id": i,
}
if max_vals["entities"] != -1:
entities_df["human_readable_id"] += max_vals["entities"] + 1
entities_df["id"] = entities_df["id"].apply(lambda x: x + f"-{index_name}")
entities_df["name"] = entities_df["name"].apply(
lambda x: x + f"-{index_name}"
)
entities_df["text_unit_ids"] = entities_df["text_unit_ids"].apply(
lambda x: [i + f"-{index_name}" for i in x]
)
max_vals["entities"] = entities_df["human_readable_id"].max()
entities_dfs.append(entities_df)
relationships_df = get_df(relationships_table_path)
for i in relationships_df["human_readable_id"].astype(int):
links["relationships"][i + max_vals["relationships"] + 1] = {
"index_name": sanitized_index_names_link[index_name],
"id": i,
}
if max_vals["relationships"] != -1:
col = (
relationships_df["human_readable_id"].astype(int)
+ max_vals["relationships"]
+ 1
)
relationships_df["human_readable_id"] = col.astype(str)
relationships_df["source"] = relationships_df["source"].apply(
lambda x: x + f"-{index_name}"
)
relationships_df["target"] = relationships_df["target"].apply(
lambda x: x + f"-{index_name}"
)
relationships_df["text_unit_ids"] = relationships_df["text_unit_ids"].apply(
lambda x: [i + f"-{index_name}" for i in x]
)
max_vals["relationships"] = (
relationships_df["human_readable_id"].astype(int).max()
)
relationships_dfs.append(relationships_df)
text_units_df = get_df(text_units_table_path)
text_units_df["id"] = text_units_df["id"].apply(
lambda x: f"{x}-{index_name}"
)
text_units_dfs.append(text_units_df)
index_container_client = blob_service_client.get_container_client(
index_name
)
if index_container_client.get_blob_client(COVARIATES_TABLE).exists():
covariates_df = get_df(covariates_table_path)
if i in covariates_df["human_readable_id"].astype(int):
links["covariates"][i + max_vals["covariates"] + 1] = {
"index_name": sanitized_index_names_link[index_name],
"id": i,
}
if max_vals["covariates"] != -1:
col = (
covariates_df["human_readable_id"].astype(int)
+ max_vals["covariates"]
+ 1
)
covariates_df["human_readable_id"] = col.astype(str)
max_vals["covariates"] = (
covariates_df["human_readable_id"].astype(int).max()
)
covariates_dfs.append(covariates_df)
nodes_combined = pd.concat(nodes_dfs, axis=0, ignore_index=True)
community_combined = pd.concat(community_dfs, axis=0, ignore_index=True)
entities_combined = pd.concat(entities_dfs, axis=0, ignore_index=True)
text_units_combined = pd.concat(text_units_dfs, axis=0, ignore_index=True)
relationships_combined = pd.concat(relationships_dfs, axis=0, ignore_index=True)
covariates_combined = (
pd.concat(covariates_dfs, axis=0, ignore_index=True)
if covariates_dfs != []
else None
)
# load custom pipeline settings
this_directory = os.path.dirname(
os.path.abspath(inspect.getfile(inspect.currentframe()))
)
data = yaml.safe_load(open(f"{this_directory}/pipeline-settings.yaml"))
# layer the custom settings on top of the default configuration settings of graphrag
parameters = create_graphrag_config(data, ".")
# add index_names to vector_store args
parameters.embeddings.vector_store["index_names"] = sanitized_index_names
# internally write over the get_embedding_description_store
# method to use the multi-index collection.
import graphrag.api.query
graphrag.api.query._get_embedding_description_store = (
_get_embedding_description_store
)
# perform streaming local search
return StreamingResponse(
_wrapper(
local_search_streaming_internal(
config=parameters,
nodes=nodes_combined,
entities=entities_combined,
community_reports=community_combined,
text_units=text_units_combined,
relationships=relationships_combined,
covariates=covariates_combined,
community_level=COMMUNITY_LEVEL,
response_type="Multiple Paragraphs",
query=request.query,
),
links,
),
media_type="application/json",
)
except Exception as e:
logger = load_pipeline_logger()
logger.error(
message="Error encountered while streaming local search response",
cause=e,
stack=traceback.format_exc(),
)
raise HTTPException(status_code=500, detail=None)
async def _wrapper(x, links):
context = None
async for i in x:
if context:
yield json.dumps({"token": i, "context": None}).encode("utf-8") + b"\n"
else:
context = i
context = _update_context(context, links)
context = json.dumps({"token": "<EOM>", "context": context}).encode("utf-8") + b"\n"
yield context