Skip to content

Commit

Permalink
feat(agent): Catch & disallow duplicate commands in LLM response pars…
Browse files Browse the repository at this point in the history
…er (#6937)

Raise in `parse_and_process_response` if the proposed operation is the same as the last executed one.
  • Loading branch information
kcze committed Feb 29, 2024
1 parent 5047fd9 commit 2c96f61
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions autogpts/autogpt/autogpt/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
AgentException,
AgentTerminated,
CommandExecutionError,
DuplicateOperationError,
UnknownCommandError,
)

Expand Down Expand Up @@ -186,6 +187,13 @@ def parse_and_process_response(
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,
Expand Down
10 changes: 10 additions & 0 deletions autogpts/autogpt/autogpt/models/action_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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

Expand Down Expand Up @@ -159,6 +160,15 @@ def register_result(self, result: ActionResult) -> None:
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.
Expand Down

0 comments on commit 2c96f61

Please sign in to comment.