Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(agent): Catch & disallow duplicate commands in LLM response parser (#6937)

Krzysztof Czerwinski2c96f6125f12
diff --git a/autogpts/autogpt/autogpt/agents/agent.py b/autogpts/autogpt/autogpt/agents/agent.py
index 00122e9812..a978a52a4c 100644
--- a/autogpts/autogpt/autogpt/agents/agent.py
+++ b/autogpts/autogpt/autogpt/agents/agent.py
@@ -50,6 +50,7 @@ from .utils.exceptions import (
AgentException,
AgentTerminated,
CommandExecutionError,
+ DuplicateOperationError,
UnknownCommandError,
)
@@ -186,6 +187,13 @@ class Agent(
assistant_reply_dict,
) = self.prompt_strategy.parse_response_content(llm_response)
+ # Check if command_name and arguments are already in the event_history
+ if self.event_history.matches_last_command(command_name, arguments):
+ raise DuplicateOperationError(
+ f"The command {command_name} with arguments {arguments} "
+ f"has been just executed."
+ )
+
self.log_cycle_handler.log_cycle(
self.ai_profile.ai_name,
self.created_at,
diff --git a/autogpts/autogpt/autogpt/models/action_history.py b/autogpts/autogpt/autogpt/models/action_history.py
index 2358e2236f..51806fb038 100644
--- a/autogpts/autogpt/autogpt/models/action_history.py
+++ b/autogpts/autogpt/autogpt/models/action_history.py
@@ -9,6 +9,7 @@ from autogpt.processing.text import summarize_text
from autogpt.prompts.utils import format_numbered_list, indent
if TYPE_CHECKING:
+ from autogpt.agents.base import CommandArgs, CommandName
from autogpt.config.config import Config
from autogpt.core.resource.model_providers import ChatModelProvider
@@ -159,6 +160,15 @@ class EpisodicActionHistory(BaseModel):
self.current_episode.result = result
self.cursor = len(self.episodes)
+ def matches_last_command(
+ self, command_name: CommandName, arguments: CommandArgs
+ ) -> bool:
+ """Check if the last command matches the given name and arguments."""
+ if len(self.episodes) > 0:
+ last_command = self.episodes[-1].action
+ return last_command.name == command_name and last_command.args == arguments
+ return False
+
def rewind(self, number_of_episodes: int = 0) -> None:
"""Resets the history to an earlier state.