Skip to content

task_tool

langroid/agent/tools/task_tool.py

A tool that allows agents to delegate a task to a sub-agent with

specific tools enabled.

TaskTool

Bases: ToolMessage

Tool that spawns a sub-agent with specified tools to handle a task.

The sub-agent can be given a custom name for identification in logs. If no name is provided, a random unique name starting with 'agent' will be generated.

handle(agent, chat_doc=None)

Handle the TaskTool by creating a sub-agent with specified tools and running the task non-interactively.

Parameters:

Name Type Description Default
agent ChatAgent

The parent ChatAgent that is handling this tool

required
chat_doc Optional[ChatDocument]

The ChatDocument containing this tool message

None
Source code in langroid/agent/tools/task_tool.py
def handle(
    self, agent: ChatAgent, chat_doc: Optional[ChatDocument] = None
) -> Optional[ChatDocument]:
    """

    Handle the TaskTool by creating a sub-agent with specified tools
    and running the task non-interactively.

    Args:
        agent: The parent ChatAgent that is handling this tool
        chat_doc: The ChatDocument containing this tool message
    """

    task = self._set_up_task(agent)

    # Create a ChatDocument for the prompt with parent pointer
    prompt_doc = None
    if chat_doc is not None:
        from langroid.agent.chat_document import ChatDocMetaData

        prompt_doc = ChatDocument(
            content=self.prompt,
            metadata=ChatDocMetaData(
                parent_id=chat_doc.id(),
                agent_id=agent.id,
                sender=chat_doc.metadata.sender,
            ),
        )
        # Set bidirectional parent-child relationship
        chat_doc.metadata.child_id = prompt_doc.id()

    # Run the task with the ChatDocument or string prompt
    result = task.run(prompt_doc or self.prompt, turns=self.max_iterations or 10)
    return result

handle_async(agent, chat_doc=None) async

Async method to handle the TaskTool by creating a sub-agent with specified tools and running the task non-interactively.

Parameters:

Name Type Description Default
agent ChatAgent

The parent ChatAgent that is handling this tool

required
chat_doc Optional[ChatDocument]

The ChatDocument containing this tool message

None
Source code in langroid/agent/tools/task_tool.py
async def handle_async(
    self, agent: ChatAgent, chat_doc: Optional[ChatDocument] = None
) -> Optional[ChatDocument]:
    """
    Async method to handle the TaskTool by creating a sub-agent with specified tools
    and running the task non-interactively.

    Args:
        agent: The parent ChatAgent that is handling this tool
        chat_doc: The ChatDocument containing this tool message
    """
    task = self._set_up_task(agent)

    # Create a ChatDocument for the prompt with parent pointer
    prompt_doc = None
    if chat_doc is not None:
        from langroid.agent.chat_document import ChatDocMetaData

        prompt_doc = ChatDocument(
            content=self.prompt,
            metadata=ChatDocMetaData(
                parent_id=chat_doc.id(),
                agent_id=agent.id,
                sender=chat_doc.metadata.sender,
            ),
        )
        # Set bidirectional parent-child relationship
        chat_doc.metadata.child_id = prompt_doc.id()

    # Run the task with the ChatDocument or string prompt
    # TODO eventually allow the various task setup configs,
    #  including termination conditions
    result = await task.run_async(
        prompt_doc or self.prompt, turns=self.max_iterations or 10
    )
    return result