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.
fix(blocks): restore batching logic in CreateListBlock
Zamil Majdye3d84d87f883
diff --git a/autogpt_platform/backend/backend/blocks/data_manipulation.py b/autogpt_platform/backend/backend/blocks/data_manipulation.pyindex ecab649c79..4b65f77d33 100644--- a/autogpt_platform/backend/backend/blocks/data_manipulation.py+++ b/autogpt_platform/backend/backend/blocks/data_manipulation.py@@ -4,6 +4,7 @@ from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchemafrom backend.data.model import SchemaFieldfrom backend.util.json import loadsfrom backend.util.mock import MockObject+from backend.util.prompt import estimate_token_count_str# =============================================================================# Dictionary Manipulation Blocks@@ -330,6 +331,16 @@ class CreateListBlock(Block):description="A list of values to be combined into a new list.",placeholder="e.g., ['Alice', 25, True]",)+ max_size: int | None = SchemaField(+ default=None,+ description="Maximum size of the list. If provided, the list will be yielded in chunks of this size.",+ advanced=True,+ )+ max_tokens: int | None = SchemaField(+ default=None,+ description="Maximum tokens for the list. If provided, the list will be yielded in chunks that fit within this token limit.",+ advanced=True,+ )class Output(BlockSchema):list: List[Any] = SchemaField(@@ -340,7 +351,7 @@ class CreateListBlock(Block):def __init__(self):super().__init__(id="a912d5c7-6e00-4542-b2a9-8034136930e4",- description="Creates a list with the specified values. Use this when you know all the values you want to add upfront.",+ description="Creates a list with the specified values. Use this when you know all the values you want to add upfront. This block can also yield the list in batches based on a maximum size or token limit.",categories={BlockCategory.DATA},input_schema=CreateListBlock.Input,output_schema=CreateListBlock.Output,@@ -365,10 +376,30 @@ class CreateListBlock(Block):)async def run(self, input_data: Input, **kwargs) -> BlockOutput:- try:- yield "list", input_data.values- except Exception as e:- yield "error", f"Failed to create list: {str(e)}"+ chunk = []+ cur_tokens, max_tokens = 0, input_data.max_tokens+ cur_size, max_size = 0, input_data.max_size++ for value in input_data.values:+ if max_tokens:+ tokens = estimate_token_count_str(value)+ else:+ tokens = 0++ # Check if adding this value would exceed either limit+ if (max_tokens and (cur_tokens + tokens > max_tokens)) or (+ max_size and (cur_size + 1 > max_size)+ ):+ yield "list", chunk+ chunk = [value]+ cur_size, cur_tokens = 1, tokens+ else:+ chunk.append(value)+ cur_size, cur_tokens = cur_size + 1, cur_tokens + tokens++ # Yield final chunk if any+ if chunk or not input_data.values:+ yield "list", chunkclass AddToListBlock(Block):