Skip to content

add: include crewai.utilities.events.knowledge_events package #3091

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 2 commits into
base: main
Choose a base branch
from

Conversation

sonnh-uit
Copy link

On instruction on this: https://docs.crewai.com/en/concepts/knowledge#knowledge-events
The example: Monitoring Knowledge Retrieval look like this

from crewai.utilities.events import (
    KnowledgeRetrievalStartedEvent,
    KnowledgeRetrievalCompletedEvent,
)
from crewai.utilities.events.base_event_listener import BaseEventListener

class KnowledgeMonitorListener(BaseEventListener):
    def setup_listeners(self, crewai_event_bus):
        @crewai_event_bus.on(KnowledgeRetrievalStartedEvent)
        def on_knowledge_retrieval_started(source, event):
            print(f"Agent '{event.agent.role}' started retrieving knowledge")
            
        @crewai_event_bus.on(KnowledgeRetrievalCompletedEvent)
        def on_knowledge_retrieval_completed(source, event):
            print(f"Agent '{event.agent.role}' completed knowledge retrieval")
            print(f"Query: {event.query}")
            print(f"Retrieved {len(event.retrieved_knowledge)} knowledge chunks")

# Create an instance of your listener
knowledge_monitor = KnowledgeMonitorListener()

When run example code above, it happen error:

ImportError: cannot import name 'KnowledgeRetrievalStartedEvent' from 'crewai.utilities.events'

This PR to fix error above.

@joaomdmoura
Copy link
Collaborator

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

Code Review for PR #3091

Overview

This pull request modifies src/crewai/utilities/events/__init__.py to incorporate new imports from the knowledge_events module, enhancing the crewAI framework with capabilities for handling knowledge-related events. This change aims at better management of event-driven architectures within the application.

Positive Aspects

  1. Organized Imports: The structure clearly segregates knowledge events, improving code readability and maintainability.
  2. Naming Consistency: The event classes adhere to established naming conventions, which is beneficial for future code maintainability.
  3. Logical Separation: Isolating knowledge events fosters a modular design, enhancing the architecture's clarity.

Recommendations for Improvement

  1. Improved Import Organization:
    Grouping and commenting on imports can greatly enhance clarity:

    # LLM-related events
    from .llm_events import (
        LLMCallStartedEvent,
        LLMCallType,
        LLMStreamChunkEvent,
    )
    
    # Knowledge-related events
    from .knowledge_events import (
        KnowledgeRetrievalStartedEvent,
        KnowledgeRetrievalCompletedEvent,
        KnowledgeQueryStartedEvent,
        KnowledgeQueryFailedEvent,
        KnowledgeQueryCompletedEvent,
        KnowledgeSearchQueryFailedEvent,
    )
    
    # Event Handling
    from .event_listener import EventListener
    from .third_party.agentops_listener import agentops_listener
  2. Module-Level Documentation:
    Adding a module-level docstring can provide context about the events:

    """
    Event definitions for the crewAI utilities package.
    This module facilitates event classes for operations including:
    - LLM interactions
    - Knowledge retrieval and querying
    - Third-party integrations
    """
  3. Define __all__:
    Including an __all__ variable can help manage public API exposure:

    __all__ = [
        'KnowledgeRetrievalStartedEvent',
        'KnowledgeRetrievalCompletedEvent',
        'KnowledgeQueryStartedEvent',
        'KnowledgeQueryFailedEvent',
        'KnowledgeQueryCompletedEvent',
        'KnowledgeSearchQueryFailedEvent',
        'EventListener',
        'agentops_listener',
    ]

Additional Suggestions

  1. Type Hints: Consider incorporating type hints in your event classes to enhance the overall robustness and maintainability of the code.

  2. Version Management:
    Implementing version checks can prevent compatibility issues:

    import pkg_resources
    
    required_version = '1.0.0'  # Specify minimum version
    try:
        pkg_resources.require(f'knowledge-package>={required_version}')
    except pkg_resources.VersionConflict:
        warnings.warn(f"Knowledge events require knowledge-package>={required_version}")
  3. Graceful Error Handling:
    Adding try-except blocks around imports can safeguard against missing dependencies:

    try:
        from .knowledge_events import (
            KnowledgeRetrievalStartedEvent,
            KnowledgeRetrievalCompletedEvent,
            # ... other imports
        )
        KNOWLEDGE_EVENTS_AVAILABLE = True
    except ImportError:
        KNOWLEDGE_EVENTS_AVAILABLE = False
        warnings.warn("Knowledge events package not available")

Conclusion

The changes made in PR #3091 are well-structured and significantly enhance the crewAI framework’s functionality by incorporating knowledge event management. By implementing the suggested improvements, you can improve the maintainability, clarity, and robustness of the codebase, facilitating better collaboration among developers and potentially decreasing future technical debt.

@lucasgomide
Copy link
Contributor

good call!

would you mind to sync your branch again and resolve conflicts?

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