Adding manager llm

This commit is contained in:
João Moura
2024-04-16 16:50:37 -03:00
parent 60995e03d1
commit f3bfabb149
4 changed files with 2865 additions and 6 deletions

View File

@@ -25,7 +25,7 @@ from crewai.process import Process
from crewai.task import Task
from crewai.telemetry import Telemetry
from crewai.tools.agent_tools import AgentTools
from crewai.utilities import I18N, Logger, RPMController, FileHandler
from crewai.utilities import I18N, FileHandler, Logger, RPMController
class Crew(BaseModel):
@@ -173,7 +173,9 @@ class Crew(BaseModel):
@model_validator(mode="after")
def check_manager_llm(self):
"""Validates that the language model is set when using hierarchical process."""
if self.process == Process.hierarchical and not self.manager_llm:
if self.process == Process.hierarchical and (
not self.manager_llm and not self.manager_agent
):
raise PydanticCustomError(
"missing_manager_llm",
"Attribute `manager_llm` is required when using hierarchical process.",
@@ -311,9 +313,11 @@ class Crew(BaseModel):
i18n = I18N(language=self.language, language_file=self.language_file)
try:
self.manager_agent.allow_delegation = (
True # Forcing Allow delegation to the manager
)
manager = self.manager_agent
manager.allow_delegation = True # Forcing Allow delegation to the manager
except:
except:
manager = Agent(
role=i18n.retrieve("hierarchical_manager_agent", "role"),
goal=i18n.retrieve("hierarchical_manager_agent", "goal"),
@@ -321,7 +325,7 @@ class Crew(BaseModel):
tools=AgentTools(agents=self.agents).tools(),
llm=self.manager_llm,
verbose=True,
)
)
task_output = ""
for task in self.tasks:

View File

@@ -28,5 +28,5 @@ class LongTermMemory(Memory):
datetime=item.datetime,
)
def search(self, task: str, latest_n: int) -> Dict[str, Any]:
def search(self, task: str, latest_n: int = 3) -> Dict[str, Any]:
return self.storage.load(task, latest_n)

File diff suppressed because it is too large Load Diff

View File

@@ -912,3 +912,35 @@ def test_crew_log_file_output(tmp_path):
crew = Crew(agents=[researcher], tasks=tasks, output_log_file=str(test_file))
crew.kickoff()
assert test_file.exists()
@pytest.mark.vcr(filter_headers=["authorization"])
def test_manager_agent():
from unittest.mock import patch
from langchain_openai import ChatOpenAI
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,
llm=ChatOpenAI(temperature=0, model="gpt-4"),
)
crew = Crew(
agents=[researcher, writer],
process=Process.hierarchical,
manager_agent=manager,
tasks=[task],
)
with patch.object(Task, "execute") as execute:
crew.kickoff()
assert manager.allow_delegation == True
execute.assert_called()