Skip to content

Tool Node

graphorchestrator.nodes.nodes.ToolNode

Bases: ProcessingNode

A node that represents a tool.

This node is a specialized ProcessingNode that wraps a tool method.

Source code in graphorchestrator\nodes\nodes.py
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
class ToolNode(ProcessingNode):
    """
    A node that represents a tool.

    This node is a specialized ProcessingNode that wraps a tool method.
    """

    def __init__(
        self,
        node_id: str,
        description: Optional[str],
        tool_method: Callable[[State], State],
    ) -> None:
        if not getattr(tool_method, "is_tool_method", False):
            raise ToolMethodNotDecorated(tool_method)
        if not (description or (tool_method.__doc__ or "").strip()):
            raise EmptyToolNodeDescriptionError(tool_method)

        super().__init__(node_id, tool_method)
        self.description = description

        GraphLogger.get().info(
            **wrap_constants(
                message="ToolNode created",
                **{
                    LC.EVENT_TYPE: "tool",
                    LC.NODE_ID: self.node_id,
                    LC.NODE_TYPE: "ToolNode",
                    LC.ACTION: "node_created",
                    LC.CUSTOM: {
                        "function": tool_method.__name__,
                        "has_description": bool(description),
                    },
                },
            )
        )

    async def execute(self, state: State) -> State:
        """
        Executes the tool method.

        Args:
            state (State): The input state for the node.

        Returns:
            State: The state after executing the tool method.
        """
        log = GraphLogger.get()

        log.info(
            **wrap_constants(
                message="ToolNode execution started",
                **{
                    LC.EVENT_TYPE: "tool",
                    LC.NODE_ID: self.node_id,
                    LC.NODE_TYPE: "ToolNode",
                    LC.ACTION: "execute_start",
                    LC.INPUT_SIZE: len(state.messages),
                },
            )
        )

        result = (
            await self.func(state)
            if asyncio.iscoroutinefunction(self.func)
            else self.func(state)
        )

        log.info(
            **wrap_constants(
                message="ToolNode execution completed",
                **{
                    LC.EVENT_TYPE: "tool",
                    LC.NODE_ID: self.node_id,
                    LC.NODE_TYPE: "ToolNode",
                    LC.ACTION: "execute_end",
                    LC.OUTPUT_SIZE: len(result.messages),
                },
            )
        )

        return result

execute(state) async

Executes the tool method.

Parameters:

Name Type Description Default
state State

The input state for the node.

required

Returns:

Name Type Description
State State

The state after executing the tool method.

Source code in graphorchestrator\nodes\nodes.py
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
async def execute(self, state: State) -> State:
    """
    Executes the tool method.

    Args:
        state (State): The input state for the node.

    Returns:
        State: The state after executing the tool method.
    """
    log = GraphLogger.get()

    log.info(
        **wrap_constants(
            message="ToolNode execution started",
            **{
                LC.EVENT_TYPE: "tool",
                LC.NODE_ID: self.node_id,
                LC.NODE_TYPE: "ToolNode",
                LC.ACTION: "execute_start",
                LC.INPUT_SIZE: len(state.messages),
            },
        )
    )

    result = (
        await self.func(state)
        if asyncio.iscoroutinefunction(self.func)
        else self.func(state)
    )

    log.info(
        **wrap_constants(
            message="ToolNode execution completed",
            **{
                LC.EVENT_TYPE: "tool",
                LC.NODE_ID: self.node_id,
                LC.NODE_TYPE: "ToolNode",
                LC.ACTION: "execute_end",
                LC.OUTPUT_SIZE: len(result.messages),
            },
        )
    )

    return result