Skip to content

Commit

Permalink
Better agent execution error handling (crewAIInc#54)
Browse files Browse the repository at this point in the history
A few quality of life improvements around cache handling and repeated tool usage
  • Loading branch information
joaomdmoura authored Jan 5, 2024
1 parent 6b05465 commit 3f9c4df
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 43 deletions.
2 changes: 1 addition & 1 deletion crewai/agents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .cache_handler import CacheHandler
from .cache.cache_handler import CacheHandler
from .executor import CrewAgentExecutor
from .output_parser import CrewAgentOutputParser
from .tools_handler import ToolsHandler
2 changes: 2 additions & 0 deletions crewai/agents/cache/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .cache_handler import CacheHandler
from .cache_hit import CacheHit
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions crewai/agents/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from langchain_core.exceptions import OutputParserException


class TaskRepeatedUsageException(OutputParserException):
"""Exception raised when a task is used twice in a roll."""

error: str = "TaskRepeatedUsageException"
message: str = "\nI just used the {tool} tool with input {tool_input}. So I already know the result of that.\n"

def __init__(self, tool: str, tool_input: str):
self.tool = tool
self.tool_input = tool_input
self.message = self.message.format(tool=tool, tool_input=tool_input)

super().__init__(
error=self.error, observation=self.message, send_to_llm=True, llm_output=""
)

def __str__(self):
return self.message
2 changes: 1 addition & 1 deletion crewai/agents/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from langchain_core.tools import BaseTool

from ..tools.cache_tools import CacheTools
from .cache_hit import CacheHit
from .cache.cache_hit import CacheHit


class CrewAgentExecutor(AgentExecutor):
Expand Down
9 changes: 3 additions & 6 deletions crewai/agents/output_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

from langchain.agents.output_parsers import ReActSingleInputOutputParser
from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.exceptions import OutputParserException

from .cache_handler import CacheHandler
from .cache_hit import CacheHit
from .cache import CacheHandler, CacheHit
from .exceptions import TaskRepeatedUsageException
from .tools_handler import ToolsHandler

FINAL_ANSWER_ACTION = "Final Answer:"
Expand Down Expand Up @@ -67,9 +66,7 @@ def parse(self, text: str) -> Union[AgentAction, AgentFinish, CacheHit]:
"input": tool_input,
}
if usage == last_tool_usage:
raise OutputParserException(
f"""\nI just used the {action} tool with input {tool_input}. So I already know the result of that."""
)
raise TaskRepeatedUsageException(tool=action, tool_input=tool_input)

result = self.cache.read(action, tool_input)
if result:
Expand Down
2 changes: 1 addition & 1 deletion crewai/agents/tools_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from langchain.callbacks.base import BaseCallbackHandler

from ..tools.cache_tools import CacheTools
from .cache_handler import CacheHandler
from .cache.cache_handler import CacheHandler


class ToolsHandler(BaseCallbackHandler):
Expand Down
Empty file removed crewai/base/__init__.py
Empty file.
24 changes: 0 additions & 24 deletions crewai/base/model.py

This file was deleted.

2 changes: 1 addition & 1 deletion crewai/crew.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pydantic_core import PydanticCustomError

from crewai.agent import Agent
from crewai.agents import CacheHandler
from crewai.agents.cache import CacheHandler
from crewai.process import Process
from crewai.task import Task
from crewai.tools.agent_tools import AgentTools
Expand Down
6 changes: 3 additions & 3 deletions crewai/tools/agent_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ def __execute(self, command):
try:
agent, task, information = command.split("|")
except ValueError:
return "\nError executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|task|information`."
return "\nError executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|task|information`.\n"

if not agent or not task or not information:
return "\nError executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|question|information`."
return "\nError executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|question|information`.\n"

agent = [
available_agent
Expand All @@ -65,7 +65,7 @@ def __execute(self, command):
]

if len(agent) == 0:
return f"\nError executing tool. Co-worker mentioned on the Action Input not found, it must to be one of the following options: {', '.join([agent.role for agent in self.agents])}."
return f"\nError executing tool. Co-worker mentioned on the Action Input not found, it must to be one of the following options: {', '.join([agent.role for agent in self.agents])}.\n"

agent = agent[0]
result = agent.execute_task(task, information)
Expand Down
2 changes: 1 addition & 1 deletion crewai/tools/cache_tools.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from langchain.tools import Tool
from pydantic import BaseModel, ConfigDict, Field

from crewai.agents import CacheHandler
from crewai.agents.cache import CacheHandler


class CacheTools(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion tests/agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from langchain.chat_models import ChatOpenAI as OpenAI

from crewai.agent import Agent
from crewai.agents import CacheHandler
from crewai.agents.cache import CacheHandler


def test_agent_creation():
Expand Down
6 changes: 3 additions & 3 deletions tests/agent_tools/agent_tools_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_delegate_work_with_wrong_input():

assert (
result
== "\nError executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|task|information`."
== "\nError executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|task|information`.\n"
)


Expand All @@ -59,7 +59,7 @@ def test_delegate_work_to_wrong_agent():

assert (
result
== "\nError executing tool. Co-worker mentioned on the Action Input not found, it must to be one of the following options: researcher."
== "\nError executing tool. Co-worker mentioned on the Action Input not found, it must to be one of the following options: researcher.\n"
)


Expand All @@ -70,5 +70,5 @@ def test_ask_question_to_wrong_agent():

assert (
result
== "\nError executing tool. Co-worker mentioned on the Action Input not found, it must to be one of the following options: researcher."
== "\nError executing tool. Co-worker mentioned on the Action Input not found, it must to be one of the following options: researcher.\n"
)
2 changes: 1 addition & 1 deletion tests/crew_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from crewai.agent import Agent
from crewai.agents import CacheHandler
from crewai.agents.cache import CacheHandler
from crewai.crew import Crew
from crewai.process import Process
from crewai.task import Task
Expand Down

0 comments on commit 3f9c4df

Please sign in to comment.