Larfii commited on
Commit
fbbd458
·
1 Parent(s): fad5373

Fix JSON parsing error

Browse files
Files changed (1) hide show
  1. lightrag/operate.py +12 -4
lightrag/operate.py CHANGED
@@ -479,10 +479,18 @@ async def kg_query(
479
  print(result)
480
  try:
481
  # json_text = locate_json_string_body_from_string(result) # handled in use_model_func
482
- result = re.search(r"{.*}", result, re.DOTALL)
483
- keywords_data = json.loads(result)
484
- hl_keywords = keywords_data.get("high_level_keywords", [])
485
- ll_keywords = keywords_data.get("low_level_keywords", [])
 
 
 
 
 
 
 
 
486
 
487
  # Handle parsing error
488
  except json.JSONDecodeError as e:
 
479
  print(result)
480
  try:
481
  # json_text = locate_json_string_body_from_string(result) # handled in use_model_func
482
+ match = re.search(r"\{.*\}", result, re.DOTALL)
483
+ if match:
484
+ result = match.group(0)
485
+ keywords_data = json.loads(result)
486
+
487
+ hl_keywords = keywords_data.get("high_level_keywords", [])
488
+ ll_keywords = keywords_data.get("low_level_keywords", [])
489
+
490
+ return hl_keywords, ll_keywords
491
+ else:
492
+ logger.error("No JSON-like structure found in the result.")
493
+ return PROMPTS["fail_response"]
494
 
495
  # Handle parsing error
496
  except json.JSONDecodeError as e: