Compare commits

...

3 Commits

Author SHA1 Message Date
Devin AI
789d5f985c Fix import sorting in test file according to ruff standards
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-05-10 16:57:30 +00:00
Devin AI
5deea31ebd Fix import sorting in test file
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-05-10 16:54:41 +00:00
Devin AI
c8ca9d7747 Fix knowledge sources not being added to agents and crews
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-05-10 16:50:59 +00:00
2 changed files with 86 additions and 0 deletions

View File

@@ -173,6 +173,7 @@ class Agent(BaseAgent):
collection_name=self.role,
storage=self.knowledge_storage or None,
)
self.knowledge.add_sources()
except (TypeError, ValueError) as e:
raise ValueError(f"Invalid Knowledge Configuration: {str(e)}")
@@ -316,6 +317,45 @@ class Agent(BaseAgent):
error=str(e),
),
)
elif self.crew and hasattr(self.crew, "knowledge") and self.crew.knowledge:
crewai_event_bus.emit(
self,
event=KnowledgeRetrievalStartedEvent(
agent=self,
),
)
try:
self.knowledge_search_query = self._get_knowledge_search_query(
task_prompt
)
if self.knowledge_search_query:
knowledge_snippets = self.crew.query_knowledge(
[self.knowledge_search_query], **knowledge_config
)
if knowledge_snippets:
self.crew_knowledge_context = extract_knowledge_context(
knowledge_snippets
)
if self.crew_knowledge_context:
task_prompt += self.crew_knowledge_context
crewai_event_bus.emit(
self,
event=KnowledgeRetrievalCompletedEvent(
query=self.knowledge_search_query,
agent=self,
retrieved_knowledge=self.crew_knowledge_context or "",
),
)
except Exception as e:
crewai_event_bus.emit(
self,
event=KnowledgeSearchQueryFailedEvent(
query=self.knowledge_search_query or "",
agent=self,
error=str(e),
),
)
tools = tools or self.tools or []
self.create_agent_executor(tools=tools, task=task)

View File

@@ -0,0 +1,46 @@
from unittest.mock import MagicMock, patch
import pytest
from crewai import LLM, Agent, Crew, Process, Task
from crewai.knowledge.knowledge import Knowledge
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource
def test_agent_without_knowledge_uses_crew_knowledge():
"""Test that an agent without knowledge sources can use crew knowledge sources."""
content = "John is 30 years old and lives in San Francisco."
string_source = StringKnowledgeSource(content=content)
agent = Agent(
role="Information Agent",
goal="Provide information based on knowledge sources",
backstory="You have access to specific knowledge sources.",
llm=LLM(model="gpt-4o-mini"),
)
task = Task(
description="How old is John and where does he live?",
expected_output="John's age and location.",
agent=agent,
)
with patch('crewai.knowledge.knowledge.Knowledge.query', return_value=[{"context": content}]) as mock_query:
crew = Crew(
agents=[agent],
tasks=[task],
process=Process.sequential,
knowledge_sources=[string_source],
)
agent.crew = crew
with patch.object(Agent, '_get_knowledge_search_query', return_value="test query"):
with patch.object(Agent, '_execute_without_timeout', return_value="John is 30 years old and lives in San Francisco."):
result = agent.execute_task(task)
assert mock_query.called
assert hasattr(agent, 'crew_knowledge_context')
assert "John" in result