Skip to content

Overview

graphorchestrator.nodes.nodes.AINode

Bases: ProcessingNode

A node that represents an AI model.

This node wraps an AI model action.

Source code in graphorchestrator\nodes\nodes.py
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
class AINode(ProcessingNode):
    """
    A node that represents an AI model.

    This node wraps an AI model action.
    """

    def __init__(
        self,
        node_id: str,
        description: str,
        model_action: Callable[[State], State],
        response_format: Optional[str] = None,
        response_parser: Optional[Callable[[State], Any]] = None,
    ) -> None:
        super().__init__(node_id, model_action)
        self.description = description
        self.response_format = response_format
        self.response_parser = response_parser

        GraphLogger.get().info(
            **wrap_constants(
                message="AINode created",
                **{
                    LC.EVENT_TYPE: "node",
                    LC.NODE_ID: self.node_id,
                    LC.NODE_TYPE: "AINode",
                    LC.ACTION: "node_created",
                    LC.CUSTOM: {
                        "description": description,
                        "response_format": response_format,
                        "has_parser": bool(response_parser),
                    },
                },
            )
        )

    async def execute(self, state: State) -> State:
        """
        Executes the AI model action.

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

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

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

        result = await self.func(state)

        if not isinstance(result, State):
            log.error(
                **wrap_constants(
                    message="AINode returned invalid output",
                    **{
                        LC.EVENT_TYPE: "node",
                        LC.NODE_ID: self.node_id,
                        LC.NODE_TYPE: "AINode",
                        LC.ACTION: "invalid_output",
                        LC.CUSTOM: {"result_type": str(type(result))},
                    },
                )
            )
            raise InvalidAIActionOutput(result)

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

        return result

execute(state) async

Executes the AI model action.

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 model action.

Source code in graphorchestrator\nodes\nodes.py
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
async def execute(self, state: State) -> State:
    """
    Executes the AI model action.

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

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

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

    result = await self.func(state)

    if not isinstance(result, State):
        log.error(
            **wrap_constants(
                message="AINode returned invalid output",
                **{
                    LC.EVENT_TYPE: "node",
                    LC.NODE_ID: self.node_id,
                    LC.NODE_TYPE: "AINode",
                    LC.ACTION: "invalid_output",
                    LC.CUSTOM: {"result_type": str(type(result))},
                },
            )
        )
        raise InvalidAIActionOutput(result)

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

    return result