mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-06 14:48:29 +00:00
Compare commits
4 Commits
lg-memory-
...
devin/1751
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
255758e429 | ||
|
|
201a73289a | ||
|
|
cd4b5b9ded | ||
|
|
63914f38cb |
@@ -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,
|
||||
|
||||
@@ -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
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user