mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-24 23:58:15 +00:00
Compare commits
5 Commits
devin/1768
...
devin/1741
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ed94f2c426 | ||
|
|
57d0eb5b00 | ||
|
|
0bdb5e9b1b | ||
|
|
667fba5847 | ||
|
|
180fd99416 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -22,4 +22,6 @@ crew_tasks_output.json
|
|||||||
.ruff_cache
|
.ruff_cache
|
||||||
.venv
|
.venv
|
||||||
agentops.log
|
agentops.log
|
||||||
test_flow.html
|
test_flow.html# Test cassettes with sensitive data
|
||||||
|
tests/cassettes/*_with_knowledge_sources.yaml
|
||||||
|
tests/cassettes/*_sensitive_*.yaml
|
||||||
|
|||||||
@@ -735,7 +735,20 @@ class Crew(BaseModel):
|
|||||||
self._create_manager_agent()
|
self._create_manager_agent()
|
||||||
return self._execute_tasks(self.tasks)
|
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)
|
i18n = I18N(prompt_file=self.prompt_file)
|
||||||
if self.manager_agent is not None:
|
if self.manager_agent is not None:
|
||||||
self.manager_agent.allow_delegation = True
|
self.manager_agent.allow_delegation = True
|
||||||
@@ -748,6 +761,13 @@ class Crew(BaseModel):
|
|||||||
raise Exception("Manager agent should not have tools")
|
raise Exception("Manager agent should not have tools")
|
||||||
else:
|
else:
|
||||||
self.manager_llm = create_llm(self.manager_llm)
|
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(
|
manager = Agent(
|
||||||
role=i18n.retrieve("hierarchical_manager_agent", "role"),
|
role=i18n.retrieve("hierarchical_manager_agent", "role"),
|
||||||
goal=i18n.retrieve("hierarchical_manager_agent", "goal"),
|
goal=i18n.retrieve("hierarchical_manager_agent", "goal"),
|
||||||
@@ -755,10 +775,12 @@ class Crew(BaseModel):
|
|||||||
tools=AgentTools(agents=self.agents).tools(),
|
tools=AgentTools(agents=self.agents).tools(),
|
||||||
allow_delegation=True,
|
allow_delegation=True,
|
||||||
llm=self.manager_llm,
|
llm=self.manager_llm,
|
||||||
|
knowledge_sources=self.knowledge_sources,
|
||||||
verbose=self.verbose,
|
verbose=self.verbose,
|
||||||
)
|
)
|
||||||
self.manager_agent = manager
|
self.manager_agent = manager
|
||||||
manager.crew = self
|
manager.crew = self
|
||||||
|
return manager
|
||||||
|
|
||||||
def _execute_tasks(
|
def _execute_tasks(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ from crewai.agents.parser import AgentAction, CrewAgentParser, OutputParserExcep
|
|||||||
from crewai.knowledge.source.base_knowledge_source import BaseKnowledgeSource
|
from crewai.knowledge.source.base_knowledge_source import BaseKnowledgeSource
|
||||||
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource
|
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource
|
||||||
from crewai.llm import LLM
|
from crewai.llm import LLM
|
||||||
|
from crewai.process import Process
|
||||||
from crewai.tools import tool
|
from crewai.tools import tool
|
||||||
from crewai.tools.tool_calling import InstructorToolCalling
|
from crewai.tools.tool_calling import InstructorToolCalling
|
||||||
from crewai.tools.tool_usage import ToolUsage
|
from crewai.tools.tool_usage import ToolUsage
|
||||||
@@ -1797,3 +1798,52 @@ def test_litellm_anthropic_error_handling():
|
|||||||
|
|
||||||
# Verify the LLM call was only made once (no retries)
|
# Verify the LLM call was only made once (no retries)
|
||||||
mock_llm_call.assert_called_once()
|
mock_llm_call.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.vcr(filter_headers=["authorization"])
|
||||||
|
def test_custom_llm_with_knowledge_sources():
|
||||||
|
"""Test that knowledge sources work with custom LLMs in hierarchical crews."""
|
||||||
|
# Create a knowledge source with some content
|
||||||
|
content = "Brandon's favorite color is red and he likes Mexican food."
|
||||||
|
string_source = StringKnowledgeSource(content=content)
|
||||||
|
|
||||||
|
# Create a custom LLM
|
||||||
|
custom_llm = LLM(model="gpt-3.5-turbo")
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"crewai.knowledge.storage.knowledge_storage.KnowledgeStorage"
|
||||||
|
) as MockKnowledge:
|
||||||
|
mock_knowledge_instance = MockKnowledge.return_value
|
||||||
|
mock_knowledge_instance.sources = [string_source]
|
||||||
|
mock_knowledge_instance.query.return_value = [{"content": content}]
|
||||||
|
|
||||||
|
# Create an agent with the custom LLM
|
||||||
|
agent = Agent(
|
||||||
|
role="Information Agent",
|
||||||
|
goal="Provide information based on knowledge sources",
|
||||||
|
backstory="You have access to specific knowledge sources.",
|
||||||
|
llm=custom_llm,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create a task that requires the agent to use the knowledge
|
||||||
|
task = Task(
|
||||||
|
description="What is Brandon's favorite color?",
|
||||||
|
expected_output="Brandon's favorite color.",
|
||||||
|
agent=agent,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create a crew with hierarchical process and custom LLM as manager
|
||||||
|
crew = Crew(
|
||||||
|
agents=[agent],
|
||||||
|
tasks=[task],
|
||||||
|
process=Process.hierarchical,
|
||||||
|
manager_llm=custom_llm,
|
||||||
|
knowledge_sources=[string_source],
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch.object(crew, "_execute_tasks") as mock_execute_tasks:
|
||||||
|
mock_execute_tasks.return_value.raw = "Brandon's favorite color is red."
|
||||||
|
result = crew.kickoff()
|
||||||
|
|
||||||
|
# Assert that the agent provides the correct information
|
||||||
|
assert "red" in result.raw.lower()
|
||||||
|
|||||||
Reference in New Issue
Block a user