Просмотр исходного кода

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>
Engel Nyst 1 год назад
Родитель
Сommit
2318ceae35

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

@@ -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')

+ 2 - 2
opendevin/controller/agent_controller.py

@@ -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'})