From 570790cd37300a9a6b2b67bdbd4539bb95c9aebe Mon Sep 17 00:00:00 2001 From: Larfii <834462287@qq.com> Date: Thu, 5 Dec 2024 20:22:44 +0800 Subject: [PATCH] Fix JSON parsing error --- lightrag/operate.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lightrag/operate.py b/lightrag/operate.py index 947a8183..2449983f 100644 --- a/lightrag/operate.py +++ b/lightrag/operate.py @@ -479,10 +479,18 @@ async def kg_query( print(result) try: # json_text = locate_json_string_body_from_string(result) # handled in use_model_func - result = re.search(r"{.*}", result, re.DOTALL) - keywords_data = json.loads(result) - hl_keywords = keywords_data.get("high_level_keywords", []) - ll_keywords = keywords_data.get("low_level_keywords", []) + match = re.search(r"\{.*\}", result, re.DOTALL) + if match: + result = match.group(0) + keywords_data = json.loads(result) + + hl_keywords = keywords_data.get("high_level_keywords", []) + ll_keywords = keywords_data.get("low_level_keywords", []) + + return hl_keywords, ll_keywords + else: + logger.error("No JSON-like structure found in the result.") + return PROMPTS["fail_response"] # Handle parsing error except json.JSONDecodeError as e: