Skip to content

Commit

Permalink
fix up json parsing (All-Hands-AI#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbren authored Apr 8, 2024
1 parent fab2259 commit cc6626f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 7 additions & 2 deletions agenthub/monologue_agent/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

3 changes: 0 additions & 3 deletions agenthub/monologue_agent/utils/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cc6626f

Please sign in to comment.