Skip to content

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(doc): Update node creation docs (#7462)

Bently2dc673614f0f
diff --git a/docs/content/server/new_blocks.md b/docs/content/server/new_blocks.md
index fd4a2976bf..9d207e59e8 100644
--- a/docs/content/server/new_blocks.md
+++ b/docs/content/server/new_blocks.md
@@ -1,18 +1,20 @@
-# Contributing to AutoGPT Agent Server: Creating Blocks!
+# Contributing to AutoGPT Agent Server: Creating and Testing Blocks
-This guide will walk you through the process of creating a new block for the AutoGPT Agent Server. We'll use the GetWikipediaSummary block as an example.
+This guide will walk you through the process of creating and testing a new block for the AutoGPT Agent Server, using the WikipediaSummaryBlock as an example.
-## Understanding Blocks
+## Understanding Blocks and Testing
-Blocks are reusable components that can be connected to form a graph representing an agent's behavior. Each block has inputs, outputs, and a specific function it performs.
+Blocks are reusable components that can be connected to form a graph representing an agent's behavior. Each block has inputs, outputs, and a specific function. Proper testing is crucial to ensure blocks work correctly and consistently.
-## Creating a New Block
+## Creating and Testing a New Block
-To create a new block, follow these steps:
+Follow these steps to create and test a new block:
1. **Create a new Python file** in the `autogpt_server/blocks` directory. Name it descriptively and use snake_case. For example: `get_wikipedia_summary.py`.
-2. **Import necessary modules and create a class that inherits from `Block`**. Make sure to include all necessary imports for your block. Every block should contain:
+2. **Import necessary modules and create a class that inherits from `Block`**. Make sure to include all necessary imports for your block.
+
+ Every block should contain the following:
```python
from autogpt_server.data.block import Block, BlockSchema, BlockOutput
@@ -21,10 +23,11 @@ To create a new block, follow these steps:
Example for the Wikipedia summary block:
```python
- import requests
from autogpt_server.data.block import Block, BlockSchema, BlockOutput
+ from autogpt_server.utils.get_request import GetRequest
+ import requests
- class GetWikipediaSummary(Block):
+ class WikipediaSummaryBlock(Block, GetRequest):
# Block implementation will go here
```
@@ -41,47 +44,60 @@ To create a new block, follow these steps:
class Output(BlockSchema):
summary: str # The summary of the topic from Wikipedia
+ error: str # Any error message if the request fails
```
-4. **Implement the `__init__` method**:
+4. **Implement the `__init__` method, including test data and mocks:**
```python
def __init__(self):
super().__init__(
- id="h5e7f8g9-1b2c-3d4e-5f6g-7h8i9j0k1l2m", # Unique ID for the block
- input_schema=GetWikipediaSummary.Input, # Assign input schema
- output_schema=GetWikipediaSummary.Output, # Assign output schema
+ # Unique ID for the block
+ # you can generate this with this python one liner
+ # print(__import__('uuid').uuid4())
+ id="h5e7f8g9-1b2c-3d4e-5f6g-7h8i9j0k1l2m",
+ input_schema=WikipediaSummaryBlock.Input, # Assign input schema
+ output_schema=WikipediaSummaryBlock.Output, # Assign output schema
- # Provide sample input and output for testing the block
+ # Provide sample input, output and test mock for testing the block
test_input={"topic": "Artificial Intelligence"},
- test_output={"summary": "Artificial intelligence (AI) is intelligence demonstrated by machines, in contrast to the natural intelligence displayed by humans and animals."},
+ test_output=("summary", "summary content"),
+ test_mock={"get_request": lambda url, json: {"extract": "summary content"}},
)
```
- `id`: A unique identifier for the block.
+
- `input_schema` and `output_schema`: Define the structure of the input and output data.
- - `test_input` and `test_output`: Provide sample input and output data for testing the block.
-5. **Implement the `run` method**, which contains the main logic of the block:
+ Let's break down the testing components:
+
+ - `test_input`: This is a sample input that will be used to test the block. It should be a valid input according to your Input schema.
+
+ - `test_output`: This is the expected output when running the block with the `test_input`. It should match your Output schema. For non-deterministic outputs or when you only want to assert the type, you can use Python types instead of specific values. In this example, `("summary", str)` asserts that the output key is "summary" and its value is a string.
+
+ - `test_mock`: This is crucial for blocks that make network calls. It provides a mock function that replaces the actual network call during testing.
+
+ In this case, we're mocking the `get_request` method to always return a dictionary with an 'extract' key, simulating a successful API response. This allows us to test the block's logic without making actual network requests, which could be slow, unreliable, or rate-limited.
+
+5. **Implement the `run` method with error handling:**, this should contain the main logic of the block:
```python
def run(self, input_data: Input) -> BlockOutput:
try:
- # Make the request to Wikipedia API
- response = requests.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{input_data.topic}")
- response.raise_for_status()
- summary_data = response.json()
+ topic = input_data.topic
+ url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{topic}"
- # Output the summary
- yield "summary", summary_data['extract']
+ response = self.get_request(url, json=True)
+ yield "summary", response['extract']
except requests.exceptions.HTTPError as http_err:
- raise ValueError(f"HTTP error occurred: {http_err}")
+ yield "error", f"HTTP error occurred: {http_err}"
except requests.RequestException as e:
- raise ValueError(f"Request to Wikipedia API failed: {e}")
+ yield "error", f"Request to Wikipedia failed: {e}"
except KeyError as e:
- raise ValueError(f"Error processing Wikipedia data: {e}")
+ yield "error", f"Error parsing Wikipedia response: {e}"
```
- **Try block**: Contains the main logic to fetch and process the Wikipedia summary.
@@ -89,84 +105,45 @@ To create a new block, follow these steps:
- **Error handling**: Handle various exceptions that might occur during the API request and data processing.
- **Yield**: Use `yield` to output the results.
-6. **Register the new block** by adding it to the `__init__.py` file in the `autogpt_server/blocks` directory. This step makes your new block available to the rest of the server.
-
- - Open the `__init__.py` file in the `autogpt_server/blocks` directory.
- - Add an import statement for your new block at the top of the file.
- - Add the new block to the `AVAILABLE_BLOCKS` and `__all__` lists.
-
- Example:
-
- ```python
- from autogpt_server.blocks import sample, reddit, text, ai, wikipedia, discord, get_wikipedia_summary # Import your new block
- from autogpt_server.data.block import Block
-
- AVAILABLE_BLOCKS = {
- block.id: block
- for block in [v() for v in Block.__subclasses__()]
- }
-
- __all__ = ["ai", "sample", "reddit", "text", "AVAILABLE_BLOCKS", "wikipedia", "discord", "get_wikipedia_summary"]
- ```
+## Key Points to Remember
- - The import statement ensures your new block is included in the module.
- - The `AVAILABLE_BLOCKS` dictionary includes all blocks by their ID.
- - The `__all__` list specifies all public objects that the module exports.
+- **Unique ID**: Give your block a unique ID in the **init** method.
+- **Input and Output Schemas**: Define clear input and output schemas.
+- **Error Handling**: Implement error handling in the `run` method.
+- **Output Results**: Use `yield` to output results in the `run` method.
+- **Testing**: Provide test input and output in the **init** method for automatic testing.
-### Full Code example
+## Understanding the Testing Process
-Here is the complete implementation of the `GetWikipediaSummary` blocks:
+The testing of blocks is handled by `test_block.py`, which does the following:
-```python
-import requests
-from autogpt_server.data.block import Block, BlockSchema, BlockOutput
+1. It calls the block with the provided `test_input`.
+2. If a `test_mock` is provided, it temporarily replaces the specified methods with the mock functions.
+3. It then asserts that the output matches the `test_output`.
-class GetWikipediaSummary(Block):
- # Define the input schema with the required field 'topic'
- class Input(BlockSchema):
- topic: str # The topic to get the Wikipedia summary for
+For the WikipediaSummaryBlock:
- # Define the output schema with the field 'summary'
- class Output(BlockSchema):
- summary: str # The summary of the topic from Wikipedia
+- The test will call the block with the topic "Artificial Intelligence".
+- Instead of making a real API call, it will use the mock function, which returns `{"extract": "summary content"}`.
+- It will then check if the output key is "summary" and its value is a string.
- def __init__(self):
- super().__init__(
- id="h5e7f8g9-1b2c-3d4e-5f6g-7h8i9j0k1l2m", # Unique ID for the block
- input_schema=GetWikipediaSummary.Input, # Assign input schema
- output_schema=GetWikipediaSummary.Output, # Assign output schema
+This approach allows us to test the block's logic comprehensively without relying on external services, while also accommodating non-deterministic outputs.
- # Provide sample input and output for testing the block
+## Tips for Effective Block Testing
- test_input={"topic": "Artificial Intelligence"},
- test_output={"summary": "Artificial intelligence (AI) is intelligence demonstrated by machines, in contrast to the natural intelligence displayed by humans and animals."},
- )
+1. **Provide realistic test_input**: Ensure your test input covers typical use cases.
- def run(self, input_data: Input) -> BlockOutput:
- try:
- # Make the request to Wikipedia API
- response = requests.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{input_data.topic}")
- response.raise_for_status()
- summary_data = response.json()
+2. **Define appropriate test_output**:
+ - For deterministic outputs, use specific expected values.
+ - For non-deterministic outputs or when only the type matters, use Python types (e.g., `str`, `int`, `dict`).
+ - You can mix specific values and types, e.g., `("key1", str), ("key2", 42)`.
- # Output the summary
- yield "summary", summary_data['extract']
+3. **Use test_mock for network calls**: This prevents tests from failing due to network issues or API changes.
- except requests.exceptions.HTTPError as http_err:
- raise ValueError(f"HTTP error occurred: {http_err}")
- except requests.RequestException as e:
- raise ValueError(f"Request to Wikipedia API failed: {e}")
- except KeyError as e:
- raise ValueError(f"Error processing Wikipedia data: {e}")
-```
+4. **Consider omitting test_mock for blocks without external dependencies**: If your block doesn't make network calls or use external resources, you might not need a mock.
-## Key Points to Remember
+5. **Consider edge cases**: Include tests for potential error conditions in your `run` method.
-- **Unique ID**: Give your block a unique ID in the `__init__` method.
-- **Input and Output Schemas**: Define clear input and output schemas.
-- **Error Handling**: Implement error handling in the `run` method.
-- **Output Results**: Use `yield` to output results in the `run` method.
-- **Register the Block**: Add your new block to the `__init__.py` file to make it available to the server.
-- **Testing**: Provide test input and output in the `__init__` method for automatic testing.
+6. **Update tests when changing block behavior**: If you modify your block, ensure the tests are updated accordingly.
By following these steps, you can create new blocks that extend the functionality of the AutoGPT Agent Server.