AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
refactor(Blocks): Simplify `MathsBlock` and `CounterBlock` to Return Numeric Results Only (#7582)
Toran Bruce Richardsa8c0cbef5455
diff --git a/rnd/autogpt_server/autogpt_server/blocks/maths.py b/rnd/autogpt_server/autogpt_server/blocks/maths.pyindex 6f06d7c6f3..02ac91042e 100644--- a/rnd/autogpt_server/autogpt_server/blocks/maths.py+++ b/rnd/autogpt_server/autogpt_server/blocks/maths.py@@ -1,8 +1,6 @@import operatorfrom enum import Enum-from typing import Any, Union--import pydantic+from typing import Anyfrom autogpt_server.data.block import Block, BlockOutput, BlockSchemafrom autogpt_server.data.model import SchemaField@@ -16,17 +14,6 @@ class Operation(Enum):POWER = "Power"-class MathsResult(pydantic.BaseModel):- result: Union[float, int] | None = None- explanation: str---class CounterResult(pydantic.BaseModel):- count: int | None = None- type: str- explanation: str--class MathsBlock(Block):class Input(BlockSchema):operation: Operation = SchemaField(@@ -45,9 +32,7 @@ class MathsBlock(Block):)class Output(BlockSchema):- result: MathsResult = SchemaField(- description="The result of your calculation with an explanation"- )+ result: float = SchemaField(description="The result of your calculation")def __init__(self):super().__init__(@@ -61,12 +46,7 @@ class MathsBlock(Block):"round_result": False,},test_output=[- (- "result",- MathsResult(- result=15.0, explanation="Added 10.0 and 5.0 to get 15.0"- ),- ),+ ("result", 15.0),],)@@ -76,14 +56,14 @@ class MathsBlock(Block):b = input_data.boperations = {- Operation.ADD: (operator.add, "Added"),- Operation.SUBTRACT: (operator.sub, "Subtracted"),- Operation.MULTIPLY: (operator.mul, "Multiplied"),- Operation.DIVIDE: (operator.truediv, "Divided"),- Operation.POWER: (operator.pow, "Raised"),+ Operation.ADD: operator.add,+ Operation.SUBTRACT: operator.sub,+ Operation.MULTIPLY: operator.mul,+ Operation.DIVIDE: operator.truediv,+ Operation.POWER: operator.pow,}- op_func, op_word = operations[operation]+ op_func = operations[operation]try:if operation == Operation.DIVIDE and b == 0:@@ -91,27 +71,15 @@ class MathsBlock(Block):result = op_func(a, b)- if operation == Operation.POWER:- explanation = f"{op_word} {a} to the power of {b} to get {result}"- elif operation == Operation.DIVIDE:- explanation = f"{op_word} {a} by {b} to get {result}"- else:- explanation = f"{op_word} {a} and {b} to get {result}"-if input_data.round_result:result = round(result)- explanation += " (rounded to the nearest whole number)"- yield "result", MathsResult(result=result, explanation=explanation)+ yield "result", resultexcept ZeroDivisionError:- yield "result", MathsResult(- result=None, explanation="Cannot divide by zero"- )- except Exception as e:- yield "result", MathsResult(- result=None, explanation=f"An error occurred: {str(e)}"- )+ yield "result", float("inf") # Return infinity for division by zero+ except Exception:+ yield "result", float("nan") # Return NaN for other errorsclass CounterBlock(Block):@@ -122,7 +90,7 @@ class CounterBlock(Block):)class Output(BlockSchema):- result: CounterResult = SchemaField(description="The result of the count")+ count: int = SchemaField(description="The number of items in the collection")def __init__(self):super().__init__(@@ -131,12 +99,7 @@ class CounterBlock(Block):output_schema=CounterBlock.Output,test_input={"collection": [1, 2, 3, 4, 5]},test_output=[- (- "result",- CounterResult(- count=5, type="list", explanation="Counted 5 items in a list"- ),- ),+ ("count", 5),],)@@ -146,27 +109,12 @@ class CounterBlock(Block):try:if isinstance(collection, (str, list, tuple, set, dict)):count = len(collection)- collection_type = type(collection).__name__elif hasattr(collection, "__iter__"):count = sum(1 for _ in collection)- collection_type = "iterable"else:raise ValueError("Input is not a countable collection")- if isinstance(collection, str):- item_word = "character" if count == 1 else "characters"- elif isinstance(collection, dict):- item_word = "key-value pair" if count == 1 else "key-value pairs"- else:- item_word = "item" if count == 1 else "items"-- explanation = f"Counted {count} {item_word} in a {collection_type}"-- yield "result", CounterResult(- count=count, type=collection_type, explanation=explanation- )+ yield "count", count- except Exception as e:- yield "result", CounterResult(- count=None, type="error", explanation=f"An error occurred: {str(e)}"- )+ except Exception:+ yield "count", -1 # Return -1 to indicate an error