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]
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")

View File

@@ -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)

View File

@@ -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):

View File

@@ -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

View File

@@ -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

View File

@@ -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)