Fix knowledge sources not being added to agents and crews

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-05-10 16:50:59 +00:00
parent cb1a98cabf
commit c8ca9d7747
2 changed files with 85 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
import pytest
from unittest.mock import patch, MagicMock
from crewai import Agent, Task, Crew, Process, LLM
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource
from crewai.knowledge.knowledge import Knowledge
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