From f6de0928c402b4d784efa5f2e2eadd6debeb05c6 Mon Sep 17 00:00:00 2001 From: SashaXser <24498484+SashaXser@users.noreply.github.com> Date: Wed, 10 Jan 2024 07:04:13 +0400 Subject: [PATCH] Refractoring (#88) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: João Moura --- crewai/agents/executor.py | 8 ++------ crewai/agents/output_parser.py | 9 +++------ crewai/crew.py | 5 +---- crewai/task.py | 15 +++++++-------- crewai/tasks/task_output.py | 2 +- crewai/tools/agent_tools.py | 5 ++--- 6 files changed, 16 insertions(+), 28 deletions(-) diff --git a/crewai/agents/executor.py b/crewai/agents/executor.py index a28363223..f4fd4733d 100644 --- a/crewai/agents/executor.py +++ b/crewai/agents/executor.py @@ -90,12 +90,8 @@ class CrewAgentExecutor(AgentExecutor): color_mapping[tool.name] = color_mapping[action.tool] actions: List[AgentAction] - if isinstance(output, AgentAction): - actions = [output] - else: - actions = output - for agent_action in actions: - yield agent_action + actions = [output] if isinstance(output, AgentAction) else output + yield from actions for agent_action in actions: if run_manager: run_manager.on_agent_action(agent_action, color="green") diff --git a/crewai/agents/output_parser.py b/crewai/agents/output_parser.py index 85e28e2b8..a8b905f0b 100644 --- a/crewai/agents/output_parser.py +++ b/crewai/agents/output_parser.py @@ -52,15 +52,13 @@ class CrewAgentOutputParser(ReActSingleInputOutputParser): regex = ( r"Action\s*\d*\s*:[\s]*(.*?)[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)" ) - action_match = re.search(regex, text, re.DOTALL) - if action_match: + if action_match := re.search(regex, text, re.DOTALL): action = action_match.group(1).strip() action_input = action_match.group(2) tool_input = action_input.strip(" ") tool_input = tool_input.strip('"') - last_tool_usage = self.tools_handler.last_used_tool - if last_tool_usage: + if last_tool_usage := self.tools_handler.last_used_tool: usage = { "tool": action, "input": tool_input, @@ -70,8 +68,7 @@ class CrewAgentOutputParser(ReActSingleInputOutputParser): tool=action, tool_input=tool_input, text=text ) - result = self.cache.read(action, tool_input) - if result: + if result := self.cache.read(action, tool_input): action = AgentAction(action, tool_input, text) return CacheHit(action=action, cache=self.cache) diff --git a/crewai/crew.py b/crewai/crew.py index b4a8bb492..eb9af269f 100644 --- a/crewai/crew.py +++ b/crewai/crew.py @@ -57,10 +57,7 @@ class Crew(BaseModel): @classmethod @field_validator("config", mode="before") def check_config_type(cls, v: Union[Json, Dict[str, Any]]): - """Ensures the 'config' field is a valid JSON or dictionary.""" - if isinstance(v, Json): - return json.loads(v) - return v + return json.loads(v) if isinstance(v, Json) else v @model_validator(mode="after") def check_config(self): diff --git a/crewai/task.py b/crewai/task.py index 79818f620..89af3f298 100644 --- a/crewai/task.py +++ b/crewai/task.py @@ -49,14 +49,13 @@ class Task(BaseModel): Returns: Output of the task. """ - if self.agent: - result = self.agent.execute_task( - task=self.description, context=context, tools=self.tools - ) - - self.output = TaskOutput(description=self.description, result=result) - return result - else: + if not self.agent: raise Exception( f"The task '{self.description}' has no agent assigned, therefore it can't be executed directly and should be executed in a Crew using a specific process that support that, either consensual or hierarchical." ) + result = self.agent.execute_task( + task=self.description, context=context, tools=self.tools + ) + + self.output = TaskOutput(description=self.description, result=result) + return result diff --git a/crewai/tasks/task_output.py b/crewai/tasks/task_output.py index 8a3c52140..cfee52970 100644 --- a/crewai/tasks/task_output.py +++ b/crewai/tasks/task_output.py @@ -12,6 +12,6 @@ class TaskOutput(BaseModel): @model_validator(mode="after") def set_summary(self): - excerpt = " ".join(self.description.split(" ")[0:10]) + excerpt = " ".join(self.description.split(" ")[:10]) self.summary = f"{excerpt}..." return self diff --git a/crewai/tools/agent_tools.py b/crewai/tools/agent_tools.py index 54786c0dd..85da36fec 100644 --- a/crewai/tools/agent_tools.py +++ b/crewai/tools/agent_tools.py @@ -68,9 +68,8 @@ class AgentTools(BaseModel): if available_agent.role == agent ] - if len(agent) == 0: + if not agent: 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, context) - return result + return agent.execute_task(task, context)