From 5ca88a4fd9408f44989f2e4b2ff4a0ed155eaae4 Mon Sep 17 00:00:00 2001 From: John Wang Date: Tue, 30 May 2023 12:16:45 +0800 Subject: [PATCH] fix: raw json parse in llm router chain (#254) --- api/core/chain/llm_router_chain.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/api/core/chain/llm_router_chain.py b/api/core/chain/llm_router_chain.py index 3108561e6b..1e3ee57c21 100644 --- a/api/core/chain/llm_router_chain.py +++ b/api/core/chain/llm_router_chain.py @@ -84,6 +84,7 @@ class RouterOutputParser(BaseOutputParser[Dict[str, str]]): def parse_json_markdown(self, json_string: str) -> dict: # Remove the triple backticks if present + json_string = json_string.strip() start_index = json_string.find("```json") end_index = json_string.find("```", start_index + len("```json")) @@ -92,6 +93,9 @@ class RouterOutputParser(BaseOutputParser[Dict[str, str]]): # Parse the JSON string into a Python dictionary parsed = json.loads(extracted_content) + elif json_string.startswith("{"): + # Parse the JSON string into a Python dictionary + parsed = json.loads(json_string) else: raise Exception("Could not find JSON block in the output.")