Can Agent call multiple tools it has access to, and combine the output?

Regarding your agent calling multiple tools based on the parameters the task receives, I hope the working example I posted helped you adapt it to your use case. In the example I gave, you create as many new tools as needed, then pass all the available tools to the tools parameter of your agent. Then you just need to state, clearly and simply, in the description of your task, which tools should be used for that id.

As for fine-grained control of your tasks, if you don’t need to stop the task execution, I believe an event listener is more suitable, as it’s less intrusive than overriding the base Task class. There can be many event listeners on crewAI’s event bus. For example, AgentOps implements its own event listener on the same bus.

So if you only need to be notified every time a task starts or completes, I propose the following functional code that listens to TaskStartedEvent, TaskCompletedEvent and TaskFailedEvent events. I have tested it together with the previous code, and everything ran smoothly. I hope it helps.

from crewai.utilities.events.base_event_listener import BaseEventListener
from crewai.utilities.events.task_events import (
    TaskCompletedEvent,
    TaskFailedEvent,
    TaskStartedEvent,
)

class MyOwnEventListener(BaseEventListener):
    def __init__(self):
        super().__init__()

    def setup_listeners(self, crewai_event_bus):
        @crewai_event_bus.on(TaskStartedEvent)
        def on_task_started(source, event: TaskStartedEvent):
            print(f"\nReceived 'TaskStartedEvent' with 'source': {source}\n")

        @crewai_event_bus.on(TaskCompletedEvent)
        def on_task_completed(source, event: TaskCompletedEvent):
            print(f"\nReceived 'TaskCompletedEvent' with 'source': {source}\n")

        @crewai_event_bus.on(TaskFailedEvent)
        def on_task_failed(source, event: TaskFailedEvent):
            print(f"\nReceived 'TaskFailedEvent' with 'source': {source}\n")

my_event_listener = MyOwnEventListener()
1 Like