Refractoring (#88)

Co-authored-by: João Moura <joaomdmoura@gmail.com>
This commit is contained in:
SashaXser
2024-01-10 07:04:13 +04:00
committed by GitHub
parent ae385eca06
commit f6de0928c4
6 changed files with 16 additions and 28 deletions

View File

@@ -90,12 +90,8 @@ class CrewAgentExecutor(AgentExecutor):
color_mapping[tool.name] = color_mapping[action.tool] color_mapping[tool.name] = color_mapping[action.tool]
actions: List[AgentAction] actions: List[AgentAction]
if isinstance(output, AgentAction): actions = [output] if isinstance(output, AgentAction) else output
actions = [output] yield from actions
else:
actions = output
for agent_action in actions:
yield agent_action
for agent_action in actions: for agent_action in actions:
if run_manager: if run_manager:
run_manager.on_agent_action(agent_action, color="green") run_manager.on_agent_action(agent_action, color="green")

View File

@@ -52,15 +52,13 @@ class CrewAgentOutputParser(ReActSingleInputOutputParser):
regex = ( regex = (
r"Action\s*\d*\s*:[\s]*(.*?)[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)" 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 := re.search(regex, text, re.DOTALL):
if action_match:
action = action_match.group(1).strip() action = action_match.group(1).strip()
action_input = action_match.group(2) action_input = action_match.group(2)
tool_input = action_input.strip(" ") tool_input = action_input.strip(" ")
tool_input = tool_input.strip('"') tool_input = tool_input.strip('"')
last_tool_usage = self.tools_handler.last_used_tool if last_tool_usage := self.tools_handler.last_used_tool:
if last_tool_usage:
usage = { usage = {
"tool": action, "tool": action,
"input": tool_input, "input": tool_input,
@@ -70,8 +68,7 @@ class CrewAgentOutputParser(ReActSingleInputOutputParser):
tool=action, tool_input=tool_input, text=text tool=action, tool_input=tool_input, text=text
) )
result = self.cache.read(action, tool_input) if result := self.cache.read(action, tool_input):
if result:
action = AgentAction(action, tool_input, text) action = AgentAction(action, tool_input, text)
return CacheHit(action=action, cache=self.cache) return CacheHit(action=action, cache=self.cache)

View File

@@ -57,10 +57,7 @@ class Crew(BaseModel):
@classmethod @classmethod
@field_validator("config", mode="before") @field_validator("config", mode="before")
def check_config_type(cls, v: Union[Json, Dict[str, Any]]): def check_config_type(cls, v: Union[Json, Dict[str, Any]]):
"""Ensures the 'config' field is a valid JSON or dictionary.""" return json.loads(v) if isinstance(v, Json) else v
if isinstance(v, Json):
return json.loads(v)
return v
@model_validator(mode="after") @model_validator(mode="after")
def check_config(self): def check_config(self):

View File

@@ -49,14 +49,13 @@ class Task(BaseModel):
Returns: Returns:
Output of the task. Output of the task.
""" """
if self.agent: if not 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:
raise Exception( 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." 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

View File

@@ -12,6 +12,6 @@ class TaskOutput(BaseModel):
@model_validator(mode="after") @model_validator(mode="after")
def set_summary(self): def set_summary(self):
excerpt = " ".join(self.description.split(" ")[0:10]) excerpt = " ".join(self.description.split(" ")[:10])
self.summary = f"{excerpt}..." self.summary = f"{excerpt}..."
return self return self

View File

@@ -68,9 +68,8 @@ class AgentTools(BaseModel):
if available_agent.role == agent 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" 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, context) return agent.execute_task(task, context)
return result