Compare commits

...

2 Commits

Author SHA1 Message Date
João Moura
35b47cb662 internal crew 2024-05-14 15:01:29 -03:00
João Moura
9841d57216 initial autonomous 2024-05-14 14:59:45 -03:00
10 changed files with 230 additions and 34 deletions

View File

@@ -1,7 +1,9 @@
import os
import json
import uuid
from typing import Any, Dict, List, Optional, Union
from langchain_openai import ChatOpenAI
from langchain_core.callbacks import BaseCallbackHandler
from pydantic import (
UUID4,
@@ -27,7 +29,6 @@ from crewai.telemetry import Telemetry
from crewai.tools.agent_tools import AgentTools
from crewai.utilities import I18N, FileHandler, Logger, RPMController
class Crew(BaseModel):
"""
Represents a group of agents, defining how they should collaborate and the tasks they should perform.
@@ -101,6 +102,15 @@ class Crew(BaseModel):
config: Optional[Union[Json, Dict[str, Any]]] = Field(default=None)
id: UUID4 = Field(default_factory=uuid.uuid4, frozen=True)
share_crew: Optional[bool] = Field(default=False)
autonomous_llm: Optional[Any] = Field(
default_factory=lambda: ChatOpenAI(
model=os.environ.get("OPENAI_MODEL_NAME", "gpt-4")
),
description="Language model that will used for agents dinamycally created.",
)
autonomous_tools: Optional[List[Any]] = Field(
default_factory=list, description="Tools at agents disposal when dinamically generated."
)
step_callback: Optional[Any] = Field(
default=None,
description="Callback to be executed after each step for all agents execution.",
@@ -266,6 +276,10 @@ class Crew(BaseModel):
result, manager_metrics = self._run_hierarchical_process() # type: ignore # Unpacking a string is disallowed
metrics.append(manager_metrics) # type: ignore # Cannot determine type of "manager_metrics"
result, manager_metrics = self._run_hierarchical_process()
metrics.append(manager_metrics)
elif self.process == Process.autonomous:
result = self._run_autonomous_process()
else:
raise NotImplementedError(
f"The process '{self.process}' is not implemented yet."
@@ -315,6 +329,61 @@ class Crew(BaseModel):
self._finish_execution(task_output)
return self._format_output(task_output)
def _run_autonomous_process(self) -> str:
"""Executes high level tasks by automatically creating agents and tasks for achieving an initial task"""
from crewai.internal.crew.planning_crew.crew import PlanningCrewCrew
#task_output = ""
# Need to decide how to break the initial task into smaller tasks
# Need to decide on what agents to create to fullfill the tasks
# Need to decide what process to use, whether sequential or hierarchical
# Need to decide on what tools to use for the agents
import pkgutil
import inspect
import crewai_tools
def list_crewai_tools():
tool_list = []
for importer, modname, ispkg in pkgutil.iter_modules(crewai_tools.__path__):
module = importer.find_module(modname).load_module(modname)
for name, obj in inspect.getmembers(module, inspect.isclass):
if obj.__module__ == module.__name__:
tool_list.append(name)
return tool_list
# Get the list of tools
tools = list_crewai_tools()
descriptions = []
for tool in tools:
args = {
k: {k2: v2 for k2, v2 in v.items() if k2 in ["description", "type"]}
for k, v in tool.args.items()
}
descriptions.append(
"\n".join(
[
f"Tool Name: {tool.name.lower()}",
f"Tool Description: {tool.description}",
f"Tool Arguments: {args}",
]
)
)
descriptions = "\n--\n".join(descriptions)
print(descriptions)
crew = PlanningCrewCrew().crew()
for task in self.tasks:
crew.kickoff({
"task": task.description,
"goal": task.expected_output,
"tools_list": descriptions
})
def _run_hierarchical_process(self) -> str:
"""Creates and assigns a manager agent to make sure the crew completes the tasks."""
@@ -364,6 +433,7 @@ class Crew(BaseModel):
for task in self.tasks:
if not task.callback:
task.callback = self.task_callback
task.callback = self.task_callback
def _interpolate_inputs(self, inputs: Dict[str, Any]) -> None:
"""Interpolates the inputs in the tasks and agents."""

View File

@@ -0,0 +1,15 @@
from crewai.agent import Agent
from crewai.tools.agent_tools import AgentTools
from crewai.utilities import I18N
class HierarchicalManagerAgent:
def __init__(self, llm, agents, verbose):
i18n = I18N()
self.agent = Agent(
role=i18n.retrieve("hierarchical_manager_agent", "role"),
goal=i18n.retrieve("hierarchical_manager_agent", "goal"),
backstory=i18n.retrieve("hierarchical_manager_agent", "backstory"),
tools=AgentTools(agents=agents).tools(),
llm=llm,
verbose=verbose,
)

View File

@@ -0,0 +1,12 @@
from crewai.agent import Agent
from crewai.utilities import i18n
class PlanningManagerAgent:
def __init__(self, llm, verbose):
self.agent = Agent(
role=i18n.retrieve("planning_manager_agent", "role"),
goal=i18n.retrieve("planning_manager_agent", "goal"),
backstory=i18n.retrieve("planning_manager_agent", "backstory"),
verbose=verbose,
llm=llm,
)

View File

@@ -0,0 +1,21 @@
project_manager:
role: >
High-Level Task Decomposer
goal: >
Efficiently break down high-level tasks into actionable subtasks.
backstory: >
As a visionary leader and strategist, you excel at dissecting complex
projects into manageable parts. Your expertise lies in identifying key
components and stages of a project, ensuring that each piece is addressed
with precision and aligned with overarching objectives.
resource_manager:
role: >
Resource Allocation Specialist
goal: >
Assign the right resources (agents and tools) to the generated subtasks.
backstory: >
With a strategic mind and a masterful grasp of logistics, you specialize
in allocating the best-suited agents and tools for specific tasks. Your
ability to match project needs with available resources ensures optimal
efficiency and effectiveness in project execution.

View File

@@ -0,0 +1,36 @@
task_decomposition:
description: >
Analyze the high-level task, "{task}", provided and break it down into
distinct, manageable subtasks that are specific and actionable.
Consider all aspects of the task and ensure that each subtask is
aligned with the final goal. Additionally, outline the roles, goals,
and backstories for the agents you plan to recruit for performing
each subtask. Make sure to clearly define their responsibilities in
the context of this project.
expected_output: >
A detailed list of subtasks, each with defined objectives and
scopes, ensuring they collectively address all components of the
original task, "{task}". Each subtask should be assigned to a
specific agent you will recruit, complete with their role, goal, and
backstory. The main goal is to ensure systematic progression and
effective task management.
resource_allocation:
description: >
Evaluate the list of subtasks generated from the high-level task
"{task}" and determine the most appropriate agents and tools needed
for each. Assign resources based on the complexity, required skills,
and the tools needed to effectively complete each subtask. This
evaluation should consider the unique requirements of each subtask
and align resources to optimize task completion. Available tools to
choose from include:
{tools_list}.
expected_output: >
An allocation plan that lists each subtask along with the assigned
agent and the tools they will use, explaining the rationale for each
resource assignment to ensure transparency and optimal task
alignment. The expected outcome is to have a fully resourced plan
that facilitates efficient task execution and achievement of the
main goal: "{goal}".
Ensure each tool selected is from the provided list of available tools:
{tools_list}.

View File

@@ -0,0 +1,51 @@
from .... import Agent, Crew, Process, Task
from ....project import CrewBase, agent, crew, task
from .crew_config import CrewConfig
@CrewBase
class PlanningCrewCrew():
"""PlanningCrew crew"""
agents_config = '../internal/crew/planning_crew/config/agents.yaml'
tasks_config = '../internal/crew/planning_crew/config/tasks.yaml'
@agent
def project_manager(self) -> Agent:
return Agent(
config=self.agents_config['project_manager'],
allow_delegation=False,
verbose=True
)
@agent
def resource_manager(self) -> Agent:
return Agent(
config=self.agents_config['resource_manager'],
allow_delegation=False,
verbose=True
)
@task
def task_decomposition(self) -> Task:
return Task(
config=self.tasks_config['task_decomposition'],
agent=self.project_manager(),
output_pydantic=CrewConfig
)
@task
def resource_allocation(self) -> Task:
return Task(
config=self.tasks_config['resource_allocation'],
agent=self.resource_manager(),
output_pydantic=CrewConfig
)
@crew
def crew(self) -> Crew:
"""Creates the PlanningCrew crew"""
return Crew(
agents=[self.project_manager(), self.resource_manager()],
tasks=[self.task_decomposition(), self.resource_allocation()],
process=Process.sequential
)

View File

@@ -0,0 +1,17 @@
from typing import List
from pydantic import BaseModel, Field
class AgentConfig(BaseModel):
role: str = Field(..., description="The role of the agent")
goal: str = Field(..., description="The goal of the agent")
backstory: str = Field(..., description="The backstory of the agent")
tools: List[str] = Field(..., description="The tools used by the agent")
class TaskConfig(BaseModel):
description: str = Field(..., description="The description of the task")
expected_output: str = Field(..., description="The expected output of the task")
agent: AgentConfig = Field(..., description="The agent responsible for the task")
class CrewConfig(BaseModel):
tasks: List[TaskConfig] = Field(..., description="The tasks to be performed by the crew")

View File

@@ -6,6 +6,7 @@ class Process(str, Enum):
Class representing the different processes that can be used to tackle tasks
"""
autonomous = "autonomous"
sequential = "sequential"
hierarchical = "hierarchical"
# TODO: consensual = 'consensual'

View File

@@ -4,6 +4,11 @@
"goal": "Manage the team to complete the task in the best way possible.",
"backstory": "You are a seasoned manager with a knack for getting the best out of your team.\nYou are also known for your ability to delegate work to the right people, and to ask the right questions to get the best out of your team.\nEven though you don't perform tasks by yourself, you have a lot of experience in the field, which allows you to properly evaluate the work of your team members."
},
"planning_manager_agent": {
"role": "Crew Creator",
"goal": "Do the best most granular break down of a final goal into tasks and agents to complete them.",
"backstory": "You are a very seasoned manager very capable of breaking goals into smaller tasks and recruiting the right people to complete them.\n You are also great at set expectation for the team you recruit and provide them with the best tools for their work."
},
"slices": {
"observation": "\nObservation",
"task": "\nCurrent Task: {input}\n\nBegin! This is VERY important to you, use the tools available and give your best Final Answer, your job depends on it!\n\nThought:",

View File

@@ -960,36 +960,4 @@ def test_manager_agent_in_agents_raises_exception():
process=Process.hierarchical,
manager_agent=manager,
tasks=[task],
)
def test_manager_agent_with_tools_raises_exception():
from crewai_tools import tool
@tool
def testing_tool(first_number: int, second_number: int) -> int:
"""Useful for when you need to multiply two numbers together."""
return first_number * second_number
task = Task(
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
expected_output="5 bullet points with a paragraph for each idea.",
)
manager = Agent(
role="Manager",
goal="Manage the crew and ensure the tasks are completed efficiently.",
backstory="You're an experienced manager, skilled in overseeing complex projects and guiding teams to success. Your role is to coordinate the efforts of the crew members, ensuring that each task is completed on time and to the highest standard.",
allow_delegation=False,
tools=[testing_tool],
)
crew = Crew(
agents=[researcher, writer],
process=Process.hierarchical,
manager_agent=manager,
tasks=[task],
)
with pytest.raises(Exception):
crew.kickoff()
)