Add type hints, validation, and documentation to _create_manager_agent method

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-03-11 10:34:35 +00:00
parent 57d0eb5b00
commit ed94f2c426
2 changed files with 25 additions and 2 deletions

4
.gitignore vendored
View File

@@ -22,4 +22,6 @@ crew_tasks_output.json
.ruff_cache
.venv
agentops.log
test_flow.htmltests/cassettes/test_custom_llm_with_knowledge_sources.yaml
test_flow.html# Test cassettes with sensitive data
tests/cassettes/*_with_knowledge_sources.yaml
tests/cassettes/*_sensitive_*.yaml

View File

@@ -735,7 +735,20 @@ class Crew(BaseModel):
self._create_manager_agent()
return self._execute_tasks(self.tasks)
def _create_manager_agent(self):
def _create_manager_agent(self) -> Agent:
"""Create a manager agent for hierarchical process.
Creates or configures a manager agent that will be responsible for delegating tasks
to other agents in a hierarchical process. If knowledge sources are provided,
they will be passed to the manager agent to enhance its context awareness.
Returns:
Agent: The configured manager agent
Raises:
Exception: If the manager agent has tools, which is not allowed
ValueError: If knowledge sources are provided but not valid BaseKnowledgeSource instances
"""
i18n = I18N(prompt_file=self.prompt_file)
if self.manager_agent is not None:
self.manager_agent.allow_delegation = True
@@ -748,6 +761,13 @@ class Crew(BaseModel):
raise Exception("Manager agent should not have tools")
else:
self.manager_llm = create_llm(self.manager_llm)
# Validate knowledge sources if provided
if self.knowledge_sources and not all(
isinstance(ks, BaseKnowledgeSource) for ks in self.knowledge_sources
):
raise ValueError("All knowledge sources must be instances of BaseKnowledgeSource")
manager = Agent(
role=i18n.retrieve("hierarchical_manager_agent", "role"),
goal=i18n.retrieve("hierarchical_manager_agent", "goal"),
@@ -760,6 +780,7 @@ class Crew(BaseModel):
)
self.manager_agent = manager
manager.crew = self
return manager
def _execute_tasks(
self,