Browse Source

fix up json parsing (#875)

Robert Brennan 1 year ago
parent
commit
cc6626ff0d

+ 7 - 2
agenthub/monologue_agent/utils/json.py

@@ -25,6 +25,11 @@ def loads(s, **kwargs):
     """
     Create a JSON object from str
     """
-    s_repaired = repair_json(s)
-    return json.loads(s_repaired, **kwargs)
+    json_start = s.find("{")
+    json_end = s.rfind("}") + 1
+    if json_start == -1 or json_end == -1:
+        raise ValueError("Invalid response: no JSON found")
+    s = s[json_start:json_end]
+    s = repair_json(s)
+    return json.loads(s, **kwargs)
 

+ 0 - 3
agenthub/monologue_agent/utils/prompts.py

@@ -151,9 +151,6 @@ def parse_action_response(response: str) -> Action:
     Returns:
     - Action: The action that was found in the response string
     """
-    json_start = response.find("{")
-    json_end = response.rfind("}") + 1
-    response = response[json_start:json_end]
     action_dict = json.loads(response)
     if 'content' in action_dict:
         # The LLM gets confused here. Might as well be robust