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

Raise in `parse_and_process_response` if the proposed operation is the same as the last executed one.
This commit is contained in:
Krzysztof Czerwinski
2024-02-29 18:51:13 +01:00
committed by GitHub
parent 5047fd9fce
commit 2c96f6125f
2 changed files with 18 additions and 0 deletions

View File

@@ -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,

View File

@@ -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.