Skip to content

Fix agent knowledge #2831

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

Vidit-Ostwal
Copy link
Contributor

Fixes #2806

Need to add test cases.

@Vidit-Ostwal Vidit-Ostwal marked this pull request as draft May 14, 2025 16:18
@joaomdmoura
Copy link
Collaborator

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

Code Review Comment for PR #2831

Overview

The changes in this pull request significantly enhance the knowledge handling capabilities within the Agent class of the crewAI project. This is crucial as it directly impacts how the agent interacts with knowledge sources, enhancing its ability to respond to queries effectively. Below are the detailed observations, issues identified, recommendations for improvement, and insights from related pull requests.

Key Observations

1. Knowledge Source Addition

The introduction of self.knowledge.add_sources() in the code is a positive enhancement. However, it currently lacks error handling and validation, which could lead to runtime exceptions if the sources provided are invalid or nonexistent.

Specific Improvements:

def set_knowledge(self, crew_embedder: Optional[Dict[str, Any]] = None):
    try:
        # ... existing code ...
        if self.knowledge_sources:  # Validate sources exist 
            self.knowledge.add_sources()
        else:
            logger.warning("No knowledge sources provided for addition")
    except Exception as e:
        logger.error(f"Failed to add knowledge sources: {str(e)}")
        raise

Ensure that knowledge sources are validated before addition to prevent unwanted behavior.

2. Knowledge Query Logic Refactoring

The refactoring performed ensures better separation between agent-specific and crew-specific knowledge queries, a good step towards maintainability. However, there appears to be duplication in the logic which may increase maintenance overhead.

Centralized Query Logic Example:

def _query_knowledge(self, query: str, config: Dict[str, Any]) -> Optional[str]:
    try:
        snippets = self.knowledge.query([query], **config)
        if snippets:
            return extract_knowledge_context(snippets) or None
    except Exception as e:
        logger.error(f"Knowledge query failed: {str(e)}")
    return None

Refactor duplicated code into functions to follow DRY principles.

General Suggestions

  1. Error Handling and Logging:

    • Expand error handling for all knowledge operations.
    • Implement robust logging to facilitate debugging.
    • Consider adding retry mechanisms for queries that may fail intermittently.
  2. Performance Optimization:

    • Implement caching for results of frequently accessed knowledge to improve performance.
  3. Documentation:

    • Improve docstring coverage for methods, detailing the knowledge querying process and expected formats for knowledge sources.
    • Include usage examples in method documentation for clarity.

Historical Context from Related PRs

It would be beneficial to reference historical changes that have focused on enhancing knowledge and querying mechanisms, particularly those aimed at improving error handling and performance. Analyzing related PRs where similar structures have been established can offer insights into best practices.

Testing Recommendations

  1. Unit Tests:

    def test_add_sources_with_valid_data():
        agent = Agent(...)
        agent.knowledge_sources = ["valid_source"]
        agent.set_knowledge()
        assert agent.knowledge.has_sources()
    
    def test_add_sources_with_invalid_data():
        agent = Agent(...)
        agent.knowledge_sources = None
        with pytest.raises(ValueError):
            agent.set_knowledge()
  2. Integration Tests:

    def test_knowledge_query_pipeline():
        agent = Agent(...)
        agent.set_knowledge()
        result = agent.execute_task(...)
        assert "knowledge context" in result

Summary of Critical Changes Needed

  1. Implement comprehensive error handling and logging.
  2. Validate input for knowledge sources before use.
  3. Refactor knowledge queries into separate, dedicated methods to enforce cleaner code practices.
  4. Optimize performance through caching strategies.
  5. Enhance documentation and expand testing coverage, particularly for new functionalities.

The changes present an excellent foundation for managing knowledge within the Agent class but require these improvements to fully realize their potential in a production setting. The integration of these recommendations will not only enhance the robustness of the code but also make it easier to maintain and extend in the future.

@Vidit-Ostwal Vidit-Ostwal changed the title Fix agent knowledge WIP Fix agent knowledge May 15, 2025
@Vidit-Ostwal Vidit-Ostwal marked this pull request as ready for review May 15, 2025 12:58
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.

[BUG] Cannot add knowledges to Agent and "knowledge_sources" parameter doesnt work on Crew
2 participants