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

Conversation

lorenzejay
Copy link
Collaborator

No description provided.

@joaomdmoura
Copy link
Collaborator

Disclaimer: This review was made by a crew of AI Agents.

Code Review Comment for PR #1927

Overview

This pull request effectively addresses the handling of knowledge sources in the cloning process of agents and crews. Modifications in two primary files, src/crewai/agents/agent_builder/base_agent.py and src/crewai/crew.py, facilitate a more rigorous approach to managing knowledge during the copying of agents and crews.

Key Observations

1. src/crewai/agents/agent_builder/base_agent.py

Positive Aspects:
  • Great implementation of type hints, enhancing code readability and maintainability.
  • Effective model validation helps prevent incorrect usage of the class.
  • Use of Field annotations provides clarity on property purposes.
Suggested Improvements:
  • Knowledge Sources Field Definition

    knowledge_sources: Optional[List[BaseKnowledgeSource]] = Field(
        default=None,
        description="Knowledge sources for the agent.",
    )

    Improvement: Switch to using default_factory=list.

    knowledge_sources: Optional[List[BaseKnowledgeSource]] = Field(
        default_factory=list,
        description="Knowledge sources for the agent to access information from.",
    )

    Rationale: This approach avoids potential None issues with mutable default arguments.

  • Copy Method Implementation

    knowledge_sources=self.knowledge_sources if hasattr(self, "knowledge_sources") else None,

    Improvement: Prefer getattr.

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

    Rationale: Cleaner and more pythonic, removing unnecessary checks.

2. src/crewai/crew.py

Positive Aspects:
  • Maintains correct agent and task copying practices.
  • Reflects a thoughtful design that supports the structure of agents.
Suggested Improvements:
  • Exclude Set Naming

    exclude = {
        "_telemetry",
        "agents",
        "tasks",
        "knowledge_source",  # Inconsistency in naming
    }

    Improvement: Change to plural for consistency.

    exclude = {
        "_telemetry",
        "agents",
        "tasks",
        "knowledge_sources",  # Match attribute name
    }
  • Knowledge Sources Handling

    knowledge_sources=self.knowledge_sources if hasattr(self, "knowledge_sources") else None,

    Improvement: Use getattr with an empty list as the default.

    knowledge_sources=getattr(self, "knowledge_sources", []),

    Rationale: Ensures the property is always initialized properly.

General Recommendations

  • Documentation Enhancements: Elaborate on knowledge_sources in both agent and crew documentation; examples would be particularly beneficial.
  • Type Safety Validation: Add a validator to ensure all elements in knowledge_sources are instances of BaseKnowledgeSource.
    @validator("knowledge_sources")
    def validate_knowledge_sources(cls, v):
        if v is not None and not all(isinstance(source, BaseKnowledgeSource) for source in v):
            raise ValueError("All knowledge sources must be instances of BaseKnowledgeSource")
        return v
  • Testing Enhancements: Develop comprehensive unit tests specifically for operations involving knowledge sources, covering edge scenarios such as empty lists and None values.
  • Error Handling Improvements: Introduce error handling for invalid knowledge sources types to uphold code integrity.

Conclusion

The reviewed changes generally seem well-founded; however, employing the suggested improvements will not only enhance code quality and maintainability but also uphold consistent user experience when dealing with agents and crews incorporating knowledge sources. Implementing robust validation, documentation, and testing will further mitigate future issues.

This concludes the review. Thank you for your work on this PR!

@@ -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?

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 ^

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.

@@ -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?

@@ -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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants