This commit is contained in:
Lorenze Jay
2025-01-27 15:39:58 -08:00
parent b92253bb13
commit 8570461969
2 changed files with 37 additions and 39 deletions

View File

@@ -270,15 +270,18 @@ class BaseAgent(ABC, BaseModel):
"cache_handler", "cache_handler",
"llm", "llm",
"knowledge_sources", "knowledge_sources",
"knowledge_storage",
"knowledge", "knowledge",
"formatting_errors",
} }
# Copy llm # Copy llm
existing_llm = shallow_copy(self.llm) existing_llm = shallow_copy(self.llm)
print("self.knowledge", self.knowledge)
copied_knowledge = shallow_copy(self.knowledge) copied_knowledge = shallow_copy(self.knowledge)
copied_knowledge_storage = shallow_copy(self.knowledge_storage)
print("existing_llm", existing_llm) print("existing_llm", existing_llm)
print("copied_knowledge_storage", copied_knowledge_storage)
print("copied_knowledge", copied_knowledge)
# Properly copy knowledge sources if they exist # Properly copy knowledge sources if they exist
existing_knowledge_sources = None existing_knowledge_sources = None
if self.knowledge_sources: if self.knowledge_sources:
@@ -308,6 +311,7 @@ class BaseAgent(ABC, BaseModel):
tools=self.tools, tools=self.tools,
knowledge_sources=existing_knowledge_sources, knowledge_sources=existing_knowledge_sources,
knowledge=copied_knowledge, knowledge=copied_knowledge,
knowledge_storage=copied_knowledge_storage,
) )
return copied_agent return copied_agent

View File

@@ -12,6 +12,8 @@ from crewai.agents.cache import CacheHandler
from crewai.agents.crew_agent_executor import CrewAgentExecutor from crewai.agents.crew_agent_executor import CrewAgentExecutor
from crewai.agents.parser import AgentAction, CrewAgentParser, OutputParserException from crewai.agents.parser import AgentAction, CrewAgentParser, OutputParserException
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource
from crewai.knowledge.source.base_knowledge_source import BaseKnowledgeSource
from crewai.knowledge.storage.knowledge_storage import KnowledgeStorage
from crewai.llm import LLM from crewai.llm import LLM
from crewai.tools import tool from crewai.tools import tool
from crewai.tools.tool_calling import InstructorToolCalling from crewai.tools.tool_calling import InstructorToolCalling
@@ -1609,53 +1611,45 @@ def test_agent_with_knowledge_sources_works_with_copy():
content = "Brandon's favorite color is red and he likes Mexican food." content = "Brandon's favorite color is red and he likes Mexican food."
string_source = StringKnowledgeSource(content=content) string_source = StringKnowledgeSource(content=content)
# Create a real instance to use as spec
with patch( with patch(
"crewai.knowledge.storage.knowledge_storage.KnowledgeStorage" "crewai.knowledge.source.base_knowledge_source.BaseKnowledgeSource",
) as MockKnowledge: autospec=True,
# Set up the mock instance ) as MockKnowledgeSource:
mock_knowledge_instance = MockKnowledge.return_value # Set up the mock instance using the real class as spec
mock_knowledge_instance.sources = [string_source] mock_knowledge_source_instance = MockKnowledgeSource.return_value
mock_knowledge_instance.query.return_value = [{"content": content}] mock_knowledge_source_instance.__class__ = BaseKnowledgeSource
mock_knowledge_instance.save.return_value = None mock_knowledge_source_instance.sources = [string_source]
mock_knowledge_instance.initialize_knowledge_storage.return_value = None # mock_knowledge_source_instance.query.return_value = [{"content": content}]
mock_knowledge_instance.collection_name = "test_collection"
# mock_knowledge_instance.embedder = {
# "provider": "openai",
# "config": {"model_name": "text-embedding-3-small", "api_key": "123"},
# }
agent = Agent( agent = Agent(
role="Information Agent", role="Information Agent",
goal="Provide information based on knowledge sources", goal="Provide information based on knowledge sources",
backstory="You have access to specific knowledge sources.", backstory="You have access to specific knowledge sources.",
llm=LLM(model="gpt-4o-mini"), llm=LLM(model="gpt-4o-mini"),
knowledge_sources=[string_source], knowledge_sources=[
# embedder={ string_source
# "provider": "openai", ], # Use the real string source instead of mock
# "config": {"model_name": "text-embedding-3-small", "api_key": "123"},
# },
) )
agent.knowledge_sources = [string_source]
# Actually call copy instead of mocking it # Mock the knowledge attribute to avoid validation issues during copy
agent_copy = agent.copy() with patch(
print("agent_copy", agent_copy) "crewai.knowledge.storage.knowledge_storage.KnowledgeStorage"
assert agent_copy.role == agent.role ) as MockKnowledgeStorage:
mock_knowledge_storage = MockKnowledgeStorage.return_value
agent.knowledge_storage = mock_knowledge_storage
# if agent.knowledge_sources and agent_copy.knowledge_sources: agent_copy = agent.copy()
# assert len(agent_copy.knowledge_sources) == len(agent.knowledge_sources)
# assert ( # Basic assertions
# agent_copy.knowledge_sources[0].content # type: ignore assert agent_copy.role == agent.role
# == agent.knowledge_sources[0].content # type: ignore assert agent_copy.goal == agent.goal
# ) assert agent_copy.backstory == agent.backstory
# assert ( assert len(agent_copy.knowledge_sources) == 1
# agent_copy.knowledge_sources[0].chunk_size assert isinstance(agent_copy.knowledge_sources[0], StringKnowledgeSource)
# == agent.knowledge_sources[0].chunk_size assert agent_copy.knowledge_sources[0].content == content
# ) assert isinstance(agent_copy.llm, LLM)
# assert (
# agent_copy.knowledge_sources[0].chunk_overlap
# == agent.knowledge_sources[0].chunk_overlap
# )
@pytest.mark.vcr(filter_headers=["authorization"]) @pytest.mark.vcr(filter_headers=["authorization"])