mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 00:28:31 +00:00
Tools cache and delegation improvements (#68)
* Fixing repeated tool usage treatment * Improving agent delegation prompt
This commit is contained in:
@@ -5,15 +5,19 @@ class TaskRepeatedUsageException(OutputParserException):
|
|||||||
"""Exception raised when a task is used twice in a roll."""
|
"""Exception raised when a task is used twice in a roll."""
|
||||||
|
|
||||||
error: str = "TaskRepeatedUsageException"
|
error: str = "TaskRepeatedUsageException"
|
||||||
message: str = "\nI just used the {tool} tool with input {tool_input}. So I already know the result of that.\n"
|
message: str = "I just used the {tool} tool with input {tool_input}. So I already know the result of that and don't need to use it now.\n"
|
||||||
|
|
||||||
def __init__(self, tool: str, tool_input: str):
|
def __init__(self, tool: str, tool_input: str, text: str):
|
||||||
|
self.text = text
|
||||||
self.tool = tool
|
self.tool = tool
|
||||||
self.tool_input = tool_input
|
self.tool_input = tool_input
|
||||||
self.message = self.message.format(tool=tool, tool_input=tool_input)
|
self.message = self.message.format(tool=tool, tool_input=tool_input)
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
error=self.error, observation=self.message, send_to_llm=True, llm_output=""
|
error=self.error,
|
||||||
|
observation=self.message,
|
||||||
|
send_to_llm=True,
|
||||||
|
llm_output=self.text,
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|||||||
@@ -66,7 +66,9 @@ class CrewAgentOutputParser(ReActSingleInputOutputParser):
|
|||||||
"input": tool_input,
|
"input": tool_input,
|
||||||
}
|
}
|
||||||
if usage == last_tool_usage:
|
if usage == last_tool_usage:
|
||||||
raise TaskRepeatedUsageException(tool=action, tool_input=tool_input)
|
raise TaskRepeatedUsageException(
|
||||||
|
tool=action, tool_input=tool_input, text=text
|
||||||
|
)
|
||||||
|
|
||||||
result = self.cache.read(action, tool_input)
|
result = self.cache.read(action, tool_input)
|
||||||
if result:
|
if result:
|
||||||
|
|||||||
@@ -18,11 +18,13 @@ class AgentTools(BaseModel):
|
|||||||
func=self.delegate_work,
|
func=self.delegate_work,
|
||||||
name="Delegate work to co-worker",
|
name="Delegate work to co-worker",
|
||||||
description=dedent(
|
description=dedent(
|
||||||
f"""Useful to delegate a specific task to one of the
|
f"""\
|
||||||
|
Useful to delegate a specific task to one of the
|
||||||
following co-workers: [{', '.join([agent.role for agent in self.agents])}].
|
following co-workers: [{', '.join([agent.role for agent in self.agents])}].
|
||||||
The input to this tool should be a pipe (|) separated text of length
|
The input to this tool should be a pipe (|) separated text of length
|
||||||
three, representing the role you want to delegate it to, the task and
|
three, representing the co-worker you want to ask it to (one of the options),
|
||||||
information necessary. For example, `coworker|task|information`.
|
the task and all actual context you have for the task.
|
||||||
|
For example, `coworker|task|context`.
|
||||||
"""
|
"""
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -30,11 +32,13 @@ class AgentTools(BaseModel):
|
|||||||
func=self.ask_question,
|
func=self.ask_question,
|
||||||
name="Ask question to co-worker",
|
name="Ask question to co-worker",
|
||||||
description=dedent(
|
description=dedent(
|
||||||
f"""Useful to ask a question, opinion or take from on
|
f"""\
|
||||||
|
Useful to ask a question, opinion or take from on
|
||||||
of the following co-workers: [{', '.join([agent.role for agent in self.agents])}].
|
of the following co-workers: [{', '.join([agent.role for agent in self.agents])}].
|
||||||
The input to this tool should be a pipe (|) separated text of length
|
The input to this tool should be a pipe (|) separated text of length
|
||||||
three, representing the role you want to ask it to, the question and
|
three, representing the co-worker you want to ask it to (one of the options),
|
||||||
information necessary. For example, `coworker|question|information`.
|
the question and all actual context you have for the question.
|
||||||
|
For example, `coworker|question|context`.
|
||||||
"""
|
"""
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -51,12 +55,12 @@ class AgentTools(BaseModel):
|
|||||||
def __execute(self, command):
|
def __execute(self, command):
|
||||||
"""Execute the command."""
|
"""Execute the command."""
|
||||||
try:
|
try:
|
||||||
agent, task, information = command.split("|")
|
agent, task, context = command.split("|")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return "\nError executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|task|information`.\n"
|
return "\nError executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|task|context`. I need to make sure to pass context as context\n"
|
||||||
|
|
||||||
if not agent or not task or not information:
|
if not agent or not task or not context:
|
||||||
return "\nError executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|question|information`.\n"
|
return "\nError executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|task|context`. I need to make sure to pass context as context.\n"
|
||||||
|
|
||||||
agent = [
|
agent = [
|
||||||
available_agent
|
available_agent
|
||||||
@@ -68,5 +72,5 @@ class AgentTools(BaseModel):
|
|||||||
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"
|
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]
|
agent = agent[0]
|
||||||
result = agent.execute_task(task, information)
|
result = agent.execute_task(task, context)
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ def test_delegate_work_with_wrong_input():
|
|||||||
|
|
||||||
assert (
|
assert (
|
||||||
result
|
result
|
||||||
== "\nError executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|task|information`.\n"
|
== "\nError executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|task|context`. I need to make sure to pass context as context\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user