Compare commits

...

4 Commits

Author SHA1 Message Date
Devin AI
255758e429 update: refresh test cassettes after local test runs
- Update VCR cassettes for knowledge-related tests
- Ensures CI has consistent test recordings

Co-Authored-By: João <joao@crewai.com>
2025-06-27 10:06:35 +00:00
Devin AI
201a73289a fix: resolve type-checker and test isolation issues
- Remove hasattr/getattr calls that caused mypy type-checker errors
- Fix test mocking to use 'crewai.agent.Knowledge' for proper isolation
- Prevent network calls in tests by mocking Knowledge class constructor
- All knowledge-related tests now pass locally without API dependencies

Co-Authored-By: João <joao@crewai.com>
2025-06-27 10:06:22 +00:00
Devin AI
cd4b5b9ded fix: address CI failures - add type hints and fix tests
- Add proper PrivateAttr declarations for cache attributes to fix mypy errors
- Simplify tests to focus on set_knowledge method directly instead of full kickoff
- Remove network calls and invalid method mocking from tests
- All knowledge caching functionality verified working locally

Co-Authored-By: João <joao@crewai.com>
2025-06-27 10:01:12 +00:00
Devin AI
63914f38cb fix: cache agent knowledge to avoid reloading on every kickoff
- Add caching mechanism in Agent.set_knowledge to track loaded state
- Skip knowledge reloading when sources and embedder haven't changed
- Add reset_knowledge_cache method for explicit cache clearing
- Add comprehensive tests for caching behavior and edge cases
- Fixes issue #3076 performance overhead on repeated kickoffs

Co-Authored-By: João <joao@crewai.com>
2025-06-27 09:52:49 +00:00
8 changed files with 877 additions and 0 deletions

View File

@@ -71,6 +71,9 @@ class Agent(BaseAgent):
"""
_times_executed: int = PrivateAttr(default=0)
_knowledge_loaded: bool = PrivateAttr(default=False)
_last_embedder: Optional[Dict[str, Any]] = PrivateAttr(default=None)
_last_knowledge_sources: Optional[List[Any]] = PrivateAttr(default=None)
max_execution_time: Optional[int] = Field(
default=None,
description="Maximum execution time for an agent to execute a task",
@@ -194,6 +197,13 @@ class Agent(BaseAgent):
def set_knowledge(self, crew_embedder: Optional[Dict[str, Any]] = None):
try:
current_embedder = crew_embedder or self.embedder
if (self._knowledge_loaded and
self.knowledge is not None and
self._last_embedder == current_embedder and
self._last_knowledge_sources == self.knowledge_sources):
return
if self.embedder is None and crew_embedder:
self.embedder = crew_embedder
@@ -208,6 +218,10 @@ class Agent(BaseAgent):
storage=self.knowledge_storage or None,
)
self.knowledge.add_sources()
self._knowledge_loaded = True
self._last_embedder = current_embedder
self._last_knowledge_sources = self.knowledge_sources.copy() if self.knowledge_sources else None
except (TypeError, ValueError) as e:
raise ValueError(f"Invalid Knowledge Configuration: {str(e)}")
@@ -228,6 +242,13 @@ class Agent(BaseAgent):
return any(getattr(self.crew, attr) for attr in memory_attributes)
def reset_knowledge_cache(self) -> None:
"""Reset the knowledge cache to force reloading on next set_knowledge call."""
self._knowledge_loaded = False
self._last_embedder = None
self._last_knowledge_sources = None
self.knowledge = None
def execute_task(
self,
task: Task,

View File

@@ -2276,3 +2276,112 @@ def test_agent_from_repository_without_org_set(
"No organization currently set. We recommend setting one before using: `crewai org switch <org_id>` command.",
style="yellow",
)
def test_agent_knowledge_caching_on_multiple_set_knowledge_calls():
"""Test that agent knowledge is only loaded once when set_knowledge is called multiple times."""
content = "Brandon's favorite color is blue and he likes Mexican food."
string_source = StringKnowledgeSource(content=content)
agent = Agent(
role="Researcher",
goal="Research about Brandon",
backstory="You are a researcher.",
knowledge_sources=[string_source],
llm="gpt-4o-mini"
)
with patch('crewai.agent.Knowledge') as mock_knowledge_class:
mock_knowledge_instance = MagicMock()
mock_knowledge_class.return_value = mock_knowledge_instance
agent.set_knowledge()
assert mock_knowledge_class.call_count == 1, "Knowledge should be created once on first call"
assert mock_knowledge_instance.add_sources.call_count == 1, "add_sources should be called once"
agent.set_knowledge()
assert mock_knowledge_class.call_count == 1, "Knowledge should not be recreated on second call"
assert mock_knowledge_instance.add_sources.call_count == 1, "add_sources should not be called again"
agent.set_knowledge()
assert mock_knowledge_class.call_count == 1, "Knowledge should not be recreated on third call"
assert mock_knowledge_instance.add_sources.call_count == 1, "add_sources should not be called again"
def test_agent_knowledge_reloads_when_sources_change():
"""Test that agent knowledge is reloaded when knowledge sources change."""
content1 = "Brandon's favorite color is blue."
content2 = "Brandon's favorite food is tacos."
string_source1 = StringKnowledgeSource(content=content1)
string_source2 = StringKnowledgeSource(content=content2)
agent = Agent(
role="Researcher",
goal="Research about Brandon",
backstory="You are a researcher.",
knowledge_sources=[string_source1],
llm="gpt-4o-mini"
)
with patch('crewai.agent.Knowledge') as mock_knowledge_class:
mock_knowledge_instance = MagicMock()
mock_knowledge_class.return_value = mock_knowledge_instance
agent.set_knowledge()
assert mock_knowledge_class.call_count == 1, "Knowledge should be created once on first call"
agent.knowledge_sources = [string_source2]
agent.set_knowledge()
assert mock_knowledge_class.call_count == 2, "Knowledge should be recreated when sources change"
def test_agent_knowledge_reloads_when_embedder_changes():
"""Test that agent knowledge is reloaded when embedder changes."""
content = "Brandon's favorite color is blue."
string_source = StringKnowledgeSource(content=content)
agent = Agent(
role="Researcher",
goal="Research about Brandon",
backstory="You are a researcher.",
knowledge_sources=[string_source],
llm="gpt-4o-mini"
)
embedder1 = {"provider": "openai", "model": "text-embedding-ada-002"}
embedder2 = {"provider": "openai", "model": "text-embedding-3-small"}
with patch('crewai.agent.Knowledge') as mock_knowledge_class:
mock_knowledge_instance = MagicMock()
mock_knowledge_class.return_value = mock_knowledge_instance
agent.set_knowledge(crew_embedder=embedder1)
assert mock_knowledge_class.call_count == 1, "Knowledge should be created once on first call"
agent.set_knowledge(crew_embedder=embedder2)
assert mock_knowledge_class.call_count == 2, "Knowledge should be recreated when embedder changes"
def test_agent_reset_knowledge_cache():
"""Test that reset_knowledge_cache forces knowledge reloading."""
content = "Brandon's favorite color is blue."
string_source = StringKnowledgeSource(content=content)
agent = Agent(
role="Researcher",
goal="Research about Brandon",
backstory="You are a researcher.",
knowledge_sources=[string_source]
)
agent._knowledge_loaded = True
agent._last_embedder = {"model": "test"}
agent._last_knowledge_sources = [string_source]
agent.reset_knowledge_cache()
assert not getattr(agent, '_knowledge_loaded', True)
assert agent._last_embedder is None
assert agent._last_knowledge_sources is None
assert agent.knowledge is None

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long