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.
feat(server): Add DictionaryLookup block for looking up values in a dictionary (#7427)
Toran Bruce Richardscb4b96a70ca2
diff --git a/rnd/autogpt_server/autogpt_server/blocks/basic.py b/rnd/autogpt_server/autogpt_server/blocks/basic.pyindex f4acef33b3..78d49f35a3 100644--- a/rnd/autogpt_server/autogpt_server/blocks/basic.py+++ b/rnd/autogpt_server/autogpt_server/blocks/basic.py@@ -71,3 +71,47 @@ class PrintingBlock(Block):def run(self, input_data: Input) -> BlockOutput:print(">>>>> Print: ", input_data.text)yield "status", "printed"+++class ObjectLookupBlock(Block):+ class Input(BlockSchema):+ input: Any = Field(description="Dictionary to lookup from")+ key: str | int = Field(description="Key to lookup in the dictionary")++ class Output(BlockSchema):+ output: Any = Field(description="Value found for the given key")+ missing: Any = Field(description="Value of the input that missing the key")++ def __init__(self):+ super().__init__(+ id="a1b2c3d4-5e6f-7g8h-9i0j-k1l2m3n4o5p6",+ input_schema=ObjectLookupBlock.Input,+ output_schema=ObjectLookupBlock.Output,+ test_input=[+ {"input": {"apple": 1, "banana": 2, "cherry": 3}, "key": "banana"},+ {"input": {"x": 10, "y": 20, "z": 30}, "key": "w"},+ {"input": [1, 2, 3], "key": 1},+ {"input": [1, 2, 3], "key": 3},+ {"input": ObjectLookupBlock.Input(input="!!", key="key"), "key": "key"},+ ],+ test_output=[+ ("output", 2),+ ("missing", {"x": 10, "y": 20, "z": 30}),+ ("output", 2),+ ("missing", [1, 2, 3]),+ ("output", "key"),+ ],+ )++ def run(self, input_data: Input) -> BlockOutput:+ obj = input_data.input+ key = input_data.key++ if isinstance(obj, dict) and key in obj:+ yield "output", obj[key]+ elif isinstance(obj, list) and isinstance(key, int) and 0 <= key < len(obj):+ yield "output", obj[key]+ elif isinstance(obj, object) and isinstance(key, str) and hasattr(obj, key):+ yield "output", getattr(obj, key)+ else:+ yield "missing", input_data.inputdiff --git a/rnd/autogpt_server/autogpt_server/blocks/wikipedia.py b/rnd/autogpt_server/autogpt_server/blocks/wikipedia.pyindex d25deaf94b..39fb06eef3 100644--- a/rnd/autogpt_server/autogpt_server/blocks/wikipedia.py+++ b/rnd/autogpt_server/autogpt_server/blocks/wikipedia.py@@ -2,7 +2,7 @@ import requestsfrom autogpt_server.data.block import Block, BlockSchema, BlockOutput-class GetWikipediaSummary(Block):+class WikipediaSummaryBlock(Block):class Input(BlockSchema):topic: str@@ -12,8 +12,8 @@ class GetWikipediaSummary(Block):def __init__(self):super().__init__(id="h5e7f8g9-1b2c-3d4e-5f6g-7h8i9j0k1l2m",- input_schema=GetWikipediaSummary.Input,- output_schema=GetWikipediaSummary.Output,+ input_schema=WikipediaSummaryBlock.Input,+ output_schema=WikipediaSummaryBlock.Output,test_input={"topic": "Artificial Intelligence"},test_output=("summary", str),)