Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix breakage when cloning agent/crew using knowledge_sources #1927

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/crewai/agents/agent_builder/base_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from crewai.agents.agent_builder.utilities.base_token_process import TokenProcess
from crewai.agents.cache.cache_handler import CacheHandler
from crewai.agents.tools_handler import ToolsHandler
from crewai.knowledge.source.base_knowledge_source import BaseKnowledgeSource
from crewai.tools import BaseTool
from crewai.tools.base_tool import Tool
from crewai.utilities import I18N, Logger, RPMController
Expand Down Expand Up @@ -130,6 +131,10 @@ class BaseAgent(ABC, BaseModel):
max_tokens: Optional[int] = Field(
default=None, description="Maximum number of tokens for the agent's execution."
)
knowledge_sources: Optional[List[BaseKnowledgeSource]] = Field(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add this here, shouldn't we drop it from agent.py?

default=None,
description="Knowledge sources for the agent.",
)

@model_validator(mode="before")
@classmethod
Expand Down Expand Up @@ -256,13 +261,21 @@ def copy(self: T) -> T: # type: ignore # Signature of "copy" incompatible with
"tools_handler",
"cache_handler",
"llm",
"knowledge_sources",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a test showing that we can successfully clone an agent with knowledge sources?

}

# Copy llm and clear callbacks
existing_llm = shallow_copy(self.llm)
copied_data = self.model_dump(exclude=exclude)
copied_data = {k: v for k, v in copied_data.items() if v is not None}
copied_agent = type(self)(**copied_data, llm=existing_llm, tools=self.tools)
copied_agent = type(self)(
**copied_data,
llm=existing_llm,
tools=self.tools,
knowledge_sources=self.knowledge_sources
if hasattr(self, "knowledge_sources")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

knowledge_sources=getattr(self, "knowledge_sources", None)

This is a little cleaner ^

else None,
)

return copied_agent

Expand Down
10 changes: 9 additions & 1 deletion src/crewai/crew.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,7 @@ def copy(self):
"_telemetry",
"agents",
"tasks",
"knowledge_sources",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a test showing that we can successfully clone a crew with knowledge sources?

}

cloned_agents = [agent.copy() for agent in self.agents]
Expand All @@ -1062,7 +1063,14 @@ def copy(self):
copied_data.pop("agents", None)
copied_data.pop("tasks", None)

copied_crew = Crew(**copied_data, agents=cloned_agents, tasks=cloned_tasks)
copied_crew = Crew(
**copied_data,
agents=cloned_agents,
tasks=cloned_tasks,
knowledge_sources=self.knowledge_sources
if hasattr(self, "knowledge_sources")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

knowledge_sources=getattr(self, "knowledge_sources", None)

Same here please.

else None,
)

return copied_crew

Expand Down
Loading