Fix JSON parsing error

This commit is contained in:
Larfii
2024-12-05 20:22:44 +08:00
parent 9af3676991
commit 1e4e2ea4f3

View File

@@ -479,11 +479,19 @@ async def kg_query(
print(result) print(result)
try: try:
# json_text = locate_json_string_body_from_string(result) # handled in use_model_func # json_text = locate_json_string_body_from_string(result) # handled in use_model_func
result = re.search(r"{.*}", result, re.DOTALL) match = re.search(r"\{.*\}", result, re.DOTALL)
if match:
result = match.group(0)
keywords_data = json.loads(result) keywords_data = json.loads(result)
hl_keywords = keywords_data.get("high_level_keywords", []) hl_keywords = keywords_data.get("high_level_keywords", [])
ll_keywords = keywords_data.get("low_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 # Handle parsing error
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
print(f"JSON parsing error: {e} {result}") print(f"JSON parsing error: {e} {result}")