Send JSON parsing exceptions to LLM (#1342)

* Add malformed JSON where we don't even start finding actions

* Send any exception during JSON parsing back

* Use specific exceptions

---------

Co-authored-by: Robert Brennan <accounts@rbren.io>
This commit is contained in:
Engel Nyst 2024-04-24 23:51:09 +02:00 committed by GitHub
parent f4ce7e7862
commit 2318ceae35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 5 deletions

View File

@ -172,10 +172,14 @@ def parse_action_response(response: str) -> Action:
return len(match[2]) if match[1] == 'think' else 130 # Crudely rank multiple responses by length
try:
action_dict = json.loads(max(response_json_matches, key=rank)[0]) # Use the highest ranked response
except ValueError as e:
except (ValueError, JSONDecodeError):
raise LLMOutputError(
"Output from the LLM isn't properly formatted. The model may be misconfigured."
) from e
'Invalid JSON, the response must be well-formed JSON as specified in the prompt.'
)
except ValueError:
raise LLMOutputError(
'Invalid JSON, the response must be well-formed JSON as specified in the prompt.'
)
if 'content' in action_dict:
# The LLM gets confused here. Might as well be robust
action_dict['contents'] = action_dict.pop('content')

View File

@ -16,7 +16,7 @@ from opendevin.observation import (
NullObservation,
)
from opendevin.agent import Agent
from opendevin.exceptions import AgentMalformedActionError, AgentNoActionError, MaxCharsExceedError
from opendevin.exceptions import AgentMalformedActionError, AgentNoActionError, MaxCharsExceedError, LLMOutputError
from opendevin.logger import opendevin_logger as logger
from opendevin.plan import Plan
from opendevin.state import State
@ -211,7 +211,7 @@ class AgentController:
action = self.agent.step(self.state)
if action is None:
raise AgentNoActionError('No action was returned')
except (AgentMalformedActionError, AgentNoActionError) as e:
except (AgentMalformedActionError, AgentNoActionError, LLMOutputError) as e:
observation = AgentErrorObservation(str(e))
logger.info(action, extra={'msg_type': 'ACTION'})