Skip to content

Commit

Permalink
add max_iterations parameter for output_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
User committed Jul 5, 2024
1 parent 8591f80 commit 432aba1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
4 changes: 3 additions & 1 deletion motleycrew/agents/output_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from motleycrew.agents.abstract_parent import MotleyAgentAbstractParent
from motleycrew.common.exceptions import InvalidOutput
from motleycrew.common import Defaults
from motleycrew.tools import MotleyTool


Expand All @@ -22,7 +23,8 @@ class MotleyOutputHandler(MotleyTool, ABC):
_exceptions_to_handle: tuple[Exception] = (InvalidOutput,)
"""Exceptions that should be returned to the agent when raised in the `handle_output` method."""

def __init__(self):
def __init__(self, max_iterations: int = Defaults.DEFAULT_OUTPUT_HANDLER_MAX_ITERATIONS):
self.max_iterations = max_iterations # number of iterations of catching an exception
langchain_tool = self._create_langchain_tool()
super().__init__(langchain_tool)

Expand Down
15 changes: 13 additions & 2 deletions motleycrew/agents/parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

from motleycrew.agents.abstract_parent import MotleyAgentAbstractParent
from motleycrew.common import MotleyAgentFactory, MotleySupportedTool
from motleycrew.common import logger
from motleycrew.common import logger, Defaults
from motleycrew.common.exceptions import (
AgentNotMaterialized,
CannotModifyMaterializedAgent,
InvalidOutput,
OutputHandlerMaxIterationsExceeded,
)
from motleycrew.tools import MotleyTool

Expand Down Expand Up @@ -131,18 +132,28 @@ def _prepare_output_handler(self) -> Optional[MotleyTool]:
if isinstance(self.output_handler, MotleyOutputHandler):
exceptions_to_handle = self.output_handler.exceptions_to_handle
description = self.output_handler.description
max_iterations = self.output_handler.max_iterations

else:
exceptions_to_handle = (InvalidOutput,)
description = self.output_handler.description or f"Output handler"
assert isinstance(description, str)
description += "\n ONLY RETURN THE FINAL RESULT USING THIS TOOL!"
max_iterations = Defaults.DEFAULT_OUTPUT_HANDLER_MAX_ITERATIONS

iteration = 0

def handle_agent_output(*args, **kwargs):
assert self.output_handler
nonlocal iteration

try:
iteration += 1
output = self.output_handler._run(*args, **kwargs)
except exceptions_to_handle as exc:
return f"{exc.__class__.__name__}: {str(exc)}"
if iteration <= max_iterations:
return f"{exc.__class__.__name__}: {str(exc)}"
raise OutputHandlerMaxIterationsExceeded(*args, **kwargs)

raise DirectOutput(output)

Expand Down
6 changes: 5 additions & 1 deletion motleycrew/common/defaults.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
""" Module description """

from motleycrew.common import LLMFamily
from motleycrew.common import GraphStoreType


class Defaults:
""" Description
"""Description
Attributes:
DEFAULT_LLM_FAMILY (str):
Expand All @@ -15,8 +16,10 @@ class Defaults:
MODULE_INSTALL_COMMANDS (dict):
DEFAULT_NUM_THREADS (int):
DEFAULT_EVENT_LOOP_SLEEP (int):
DEFAULT_OUTPUT_HANDLER_MAX_ITERATIONS (int):
"""

DEFAULT_LLM_FAMILY = LLMFamily.OPENAI
DEFAULT_LLM_NAME = "gpt-4o"
DEFAULT_LLM_TEMPERATURE = 0.0
Expand All @@ -35,3 +38,4 @@ class Defaults:

DEFAULT_NUM_THREADS = 4
DEFAULT_EVENT_LOOP_SLEEP = 1
DEFAULT_OUTPUT_HANDLER_MAX_ITERATIONS = 5
11 changes: 11 additions & 0 deletions motleycrew/common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,14 @@ class InvalidOutput(Exception):
"""Raised in output handlers when an agent's output is not accepted"""

pass


class OutputHandlerMaxIterationsExceeded(BaseException):
"""Raised when the output handlers iteration limit is exceeded"""

def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs

def __str__(self):
return "\n args: {}\n kwargs: {}".format(self.args, self.kwargs)

0 comments on commit 432aba1

Please sign in to comment.