Skip to content

Commit

Permalink
refactor(backend): Introduced Graph Input & Output Schema, Merge Grap…
Browse files Browse the repository at this point in the history
…hMeta & Graph, Remove subgraph functionality (#8526)
  • Loading branch information
majdyz authored Nov 7, 2024
1 parent af9ea5b commit 86c5441
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 419 deletions.
58 changes: 40 additions & 18 deletions autogpt_platform/backend/backend/blocks/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,12 @@ class Input(BlockSchema):
description="The value to be passed as input.",
default=None,
)
description: str = SchemaField(
title: str | None = SchemaField(
description="The title of the input.", default=None, advanced=True
)
description: str | None = SchemaField(
description="The description of the input.",
default="",
default=None,
advanced=True,
)
placeholder_values: List[Any] = SchemaField(
Expand All @@ -163,6 +166,16 @@ class Input(BlockSchema):
default=False,
advanced=True,
)
advanced: bool = SchemaField(
description="Whether to show the input in the advanced section, if the field is not required.",
default=False,
advanced=True,
)
secret: bool = SchemaField(
description="Whether the input should be treated as a secret.",
default=False,
advanced=True,
)

class Output(BlockSchema):
result: Any = SchemaField(description="The value passed as input.")
Expand Down Expand Up @@ -195,6 +208,7 @@ def __init__(self):
],
categories={BlockCategory.INPUT, BlockCategory.BASIC},
block_type=BlockType.INPUT,
static_output=True,
)

def run(self, input_data: Input, **kwargs) -> BlockOutput:
Expand All @@ -205,35 +219,42 @@ class AgentOutputBlock(Block):
"""
Records the output of the graph for users to see.
Attributes:
recorded_value: The value to be recorded as output.
name: The name of the output.
description: The description of the output.
fmt_string: The format string to be used to format the recorded_value.
Outputs:
output: The formatted recorded_value if fmt_string is provided and the recorded_value
can be formatted, otherwise the raw recorded_value.
Behavior:
If fmt_string is provided and the recorded_value is of a type that can be formatted,
the block attempts to format the recorded_value using the fmt_string.
If formatting fails or no fmt_string is provided, the raw recorded_value is output.
If `format` is provided and the `value` is of a type that can be formatted,
the block attempts to format the recorded_value using the `format`.
If formatting fails or no `format` is provided, the raw `value` is output.
"""

class Input(BlockSchema):
value: Any = SchemaField(description="The value to be recorded as output.")
value: Any = SchemaField(
description="The value to be recorded as output.",
default=None,
advanced=False,
)
name: str = SchemaField(description="The name of the output.")
description: str = SchemaField(
title: str | None = SchemaField(
description="The title of the input.", default=None, advanced=True
)
description: str | None = SchemaField(
description="The description of the output.",
default="",
default=None,
advanced=True,
)
format: str = SchemaField(
description="The format string to be used to format the recorded_value.",
default="",
advanced=True,
)
advanced: bool = SchemaField(
description="Whether to treat the output as advanced.",
default=False,
advanced=True,
)
secret: bool = SchemaField(
description="Whether the output should be treated as a secret.",
default=False,
advanced=True,
)

class Output(BlockSchema):
output: Any = SchemaField(description="The value recorded as output.")
Expand Down Expand Up @@ -271,6 +292,7 @@ def __init__(self):
],
categories={BlockCategory.OUTPUT, BlockCategory.BASIC},
block_type=BlockType.OUTPUT,
static_output=True,
)

def run(self, input_data: Input, **kwargs) -> BlockOutput:
Expand Down
29 changes: 15 additions & 14 deletions autogpt_platform/backend/backend/blocks/branching.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,18 @@ def __init__(self):
)

def run(self, input_data: Input, **kwargs) -> BlockOutput:
value1 = input_data.value1
operator = input_data.operator

value1 = input_data.value1
if isinstance(value1, str):
value1 = float(value1.strip())

value2 = input_data.value2
if isinstance(value2, str):
value2 = float(value2.strip())

yes_value = input_data.yes_value if input_data.yes_value is not None else value1
no_value = input_data.no_value if input_data.no_value is not None else value1
no_value = input_data.no_value if input_data.no_value is not None else value2

comparison_funcs = {
ComparisonOperator.EQUAL: lambda a, b: a == b,
Expand All @@ -86,17 +93,11 @@ def run(self, input_data: Input, **kwargs) -> BlockOutput:
ComparisonOperator.LESS_THAN_OR_EQUAL: lambda a, b: a <= b,
}

try:
result = comparison_funcs[operator](value1, value2)

yield "result", result
result = comparison_funcs[operator](value1, value2)

if result:
yield "yes_output", yes_value
else:
yield "no_output", no_value
yield "result", result

except Exception:
yield "result", None
yield "yes_output", None
yield "no_output", None
if result:
yield "yes_output", yes_value
else:
yield "no_output", no_value
41 changes: 4 additions & 37 deletions autogpt_platform/backend/backend/data/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
AgentNodeExecution,
AgentNodeExecutionInputOutput,
)
from prisma.types import (
AgentGraphExecutionInclude,
AgentGraphExecutionWhereInput,
AgentNodeExecutionInclude,
)
from prisma.types import AgentGraphExecutionWhereInput
from pydantic import BaseModel

from backend.data.block import BlockData, BlockInput, CompletedBlockOutput
from backend.data.includes import EXECUTION_RESULT_INCLUDE, GRAPH_EXECUTION_INCLUDE
from backend.util import json, mock


Expand Down Expand Up @@ -110,24 +107,6 @@ def from_db(execution: AgentNodeExecution):

# --------------------- Model functions --------------------- #

EXECUTION_RESULT_INCLUDE: AgentNodeExecutionInclude = {
"Input": True,
"Output": True,
"AgentNode": True,
"AgentGraphExecution": True,
}

GRAPH_EXECUTION_INCLUDE: AgentGraphExecutionInclude = {
"AgentNodeExecutions": {
"include": {
"Input": True,
"Output": True,
"AgentNode": True,
"AgentGraphExecution": True,
}
}
}


async def create_graph_execution(
graph_id: str,
Expand Down Expand Up @@ -268,21 +247,9 @@ async def update_graph_execution_start_time(graph_exec_id: str):

async def update_graph_execution_stats(
graph_exec_id: str,
error: Exception | None,
wall_time: float,
cpu_time: float,
node_count: int,
stats: dict[str, Any],
):
status = ExecutionStatus.FAILED if error else ExecutionStatus.COMPLETED
stats = (
{
"walltime": wall_time,
"cputime": cpu_time,
"nodecount": node_count,
"error": str(error) if error else None,
},
)

status = ExecutionStatus.FAILED if stats.get("error") else ExecutionStatus.COMPLETED
await AgentGraphExecution.prisma().update(
where={"id": graph_exec_id},
data={
Expand Down
Loading

0 comments on commit 86c5441

Please sign in to comment.