-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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( | ||
default=None, | ||
description="Knowledge sources for the agent.", | ||
) | ||
|
||
@model_validator(mode="before") | ||
@classmethod | ||
|
@@ -256,13 +261,21 @@ def copy(self: T) -> T: # type: ignore # Signature of "copy" incompatible with | |
"tools_handler", | ||
"cache_handler", | ||
"llm", | ||
"knowledge_sources", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1036,6 +1036,7 @@ def copy(self): | |
"_telemetry", | ||
"agents", | ||
"tasks", | ||
"knowledge_sources", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
There was a problem hiding this comment.
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?