Skip to content

Commit 448fa1c

Browse files
authored
Robust for abnormal response from LLMs. (#4747)
### What problem does this PR solve? ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
1 parent e786f59 commit 448fa1c

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

graphrag/general/community_reports_extractor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def __call__(self, graph: nx.Graph, callback: Callable | None = None):
7575
ent_df["entity"] = ent_df["entity_name"]
7676
del ent_df["entity_name"]
7777
rela_df = pd.DataFrame(self._get_relation_(list(ent_df["entity"]), list(ent_df["entity"]), 10000))
78+
if rela_df.empty:
79+
continue
7880
rela_df["source"] = rela_df["src_id"]
7981
rela_df["target"] = rela_df["tgt_id"]
8082
del rela_df["src_id"]

graphrag/search.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ def retrieval(self, question: str,
154154
tenant_ids = tenant_ids.split(",")
155155
idxnms = [index_name(tid) for tid in tenant_ids]
156156
ty_kwds = []
157-
ents = []
158157
try:
159158
ty_kwds, ents = self.query_rewrite(llm, qst, [index_name(tid) for tid in tenant_ids], kb_ids)
160159
logging.info(f"Q: {qst}, Types: {ty_kwds}, Entities: {ents}")
@@ -169,6 +168,9 @@ def retrieval(self, question: str,
169168
nhop_pathes = defaultdict(dict)
170169
for _, ent in ents_from_query.items():
171170
nhops = ent.get("n_hop_ents", [])
171+
if not isinstance(nhops, list):
172+
logging.warning(f"Abnormal n_hop_ents: {nhops}")
173+
continue
172174
for nbr in nhops:
173175
path = nbr["path"]
174176
wts = nbr["weights"]
@@ -246,7 +248,7 @@ def retrieval(self, question: str,
246248
"From Entity": f,
247249
"To Entity": t,
248250
"Score": "%.2f" % (rel["sim"] * rel["pagerank"]),
249-
"Description": json.loads(ent["description"]).get("description", "")
251+
"Description": json.loads(rel["description"]).get("description", "")
250252
})
251253
max_token -= num_tokens_from_string(str(relas[-1]))
252254
if max_token <= 0:

rag/nlp/search.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,14 @@ def retrieval(self, question, embd_mdl, tenant_ids, kb_ids, page, page_size, sim
388388
break
389389
id = sres.ids[i]
390390
chunk = sres.field[id]
391-
dnm = chunk["docnm_kwd"]
392-
did = chunk["doc_id"]
391+
dnm = chunk.get("docnm_kwd", "")
392+
did = chunk.get("doc_id", "")
393393
position_int = chunk.get("position_int", [])
394394
d = {
395395
"chunk_id": id,
396396
"content_ltks": chunk["content_ltks"],
397397
"content_with_weight": chunk["content_with_weight"],
398-
"doc_id": chunk["doc_id"],
398+
"doc_id": did,
399399
"docnm_kwd": dnm,
400400
"kb_id": chunk["kb_id"],
401401
"important_kwd": chunk.get("important_kwd", []),

0 commit comments

Comments
 (0)