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(blocks): Implement AI Condition Block for natural language condition evaluation (#10996)
Copilot4663066e654d
diff --git a/autogpt_platform/backend/backend/blocks/ai_condition.py b/autogpt_platform/backend/backend/blocks/ai_condition.pynew file mode 100644index 0000000000..18f9046a57--- /dev/null+++ b/autogpt_platform/backend/backend/blocks/ai_condition.py@@ -0,0 +1,214 @@+from typing import Any++from backend.blocks.llm import (+ TEST_CREDENTIALS,+ TEST_CREDENTIALS_INPUT,+ AIBlockBase,+ AICredentials,+ AICredentialsField,+ LlmModel,+ LLMResponse,+ llm_call,+)+from backend.data.block import BlockCategory, BlockOutput, BlockSchema+from backend.data.model import APIKeyCredentials, NodeExecutionStats, SchemaField+++class AIConditionBlock(AIBlockBase):+ """+ An AI-powered condition block that uses natural language to evaluate conditions.++ This block allows users to define conditions in plain English (e.g., "the input is an email address",+ "the input is a city in the USA") and uses AI to determine if the input satisfies the condition.+ It provides the same yes/no data pass-through functionality as the standard ConditionBlock.+ """++ class Input(BlockSchema):+ input_value: Any = SchemaField(+ description="The input value to evaluate with the AI condition",+ placeholder="Enter the value to be evaluated (text, number, or any data)",+ )+ condition: str = SchemaField(+ description="A plaintext English description of the condition to evaluate",+ placeholder="E.g., 'the input is the body of an email', 'the input is a City in the USA', 'the input is an error or a refusal'",+ )+ yes_value: Any = SchemaField(+ description="(Optional) Value to output if the condition is true. If not provided, input_value will be used.",+ placeholder="Leave empty to use input_value, or enter a specific value",+ default=None,+ )+ no_value: Any = SchemaField(+ description="(Optional) Value to output if the condition is false. If not provided, input_value will be used.",+ placeholder="Leave empty to use input_value, or enter a specific value",+ default=None,+ )+ model: LlmModel = SchemaField(+ title="LLM Model",+ default=LlmModel.GPT4O,+ description="The language model to use for evaluating the condition.",+ advanced=False,+ )+ credentials: AICredentials = AICredentialsField()++ class Output(BlockSchema):+ result: bool = SchemaField(+ description="The result of the AI condition evaluation (True or False)"+ )+ yes_output: Any = SchemaField(+ description="The output value if the condition is true"+ )+ no_output: Any = SchemaField(+ description="The output value if the condition is false"+ )+ error: str = SchemaField(+ description="Error message if the AI evaluation is uncertain or fails"+ )++ def __init__(self):+ super().__init__(+ id="553ec5b8-6c45-4299-8d75-b394d05f72ff",+ input_schema=AIConditionBlock.Input,+ output_schema=AIConditionBlock.Output,+ description="Uses AI to evaluate natural language conditions and provide conditional outputs",+ categories={BlockCategory.AI, BlockCategory.LOGIC},+ test_input={+ "input_value": "[email protected]",+ "condition": "the input is an email address",+ "yes_value": "Valid email",+ "no_value": "Not an email",+ "model": LlmModel.GPT4O,+ "credentials": TEST_CREDENTIALS_INPUT,+ },+ test_credentials=TEST_CREDENTIALS,+ test_output=[+ ("result", True),+ ("yes_output", "Valid email"),+ ],+ test_mock={+ "llm_call": lambda *args, **kwargs: LLMResponse(+ raw_response="",+ prompt=[],+ response="true",+ tool_calls=None,+ prompt_tokens=50,+ completion_tokens=10,+ reasoning=None,+ )+ },+ )++ async def llm_call(+ self,+ credentials: APIKeyCredentials,+ llm_model: LlmModel,+ prompt: list,+ max_tokens: int,+ ) -> LLMResponse:+ """Wrapper method for llm_call to enable mocking in tests."""+ return await llm_call(+ credentials=credentials,+ llm_model=llm_model,+ prompt=prompt,+ force_json_output=False,+ max_tokens=max_tokens,+ )++ async def run(+ self, input_data: Input, *, credentials: APIKeyCredentials, **kwargs+ ) -> BlockOutput:+ """+ Evaluate the AI condition and return appropriate outputs.+ """+ # Prepare the yes and no values, using input_value as default+ yes_value = (+ input_data.yes_value+ if input_data.yes_value is not None+ else input_data.input_value+ )+ no_value = (+ input_data.no_value+ if input_data.no_value is not None+ else input_data.input_value+ )++ # Convert input_value to string for AI evaluation+ input_str = str(input_data.input_value)++ # Create the prompt for AI evaluation+ prompt = [+ {+ "role": "system",+ "content": (+ "You are an AI assistant that evaluates conditions based on input data. "+ "You must respond with only 'true' or 'false' (lowercase) to indicate whether "+ "the given condition is met by the input value. Be accurate and consider the "+ "context and meaning of both the input and the condition."+ ),+ },+ {+ "role": "user",+ "content": (+ f"Input value: {input_str}\n"+ f"Condition to evaluate: {input_data.condition}\n\n"+ f"Does the input value satisfy the condition? Respond with only 'true' or 'false'."+ ),+ },+ ]++ # Call the LLM+ try:+ response = await self.llm_call(+ credentials=credentials,+ llm_model=input_data.model,+ prompt=prompt,+ max_tokens=10, # We only expect a true/false response+ )++ # Extract the boolean result from the response+ response_text = response.response.strip().lower()+ if response_text == "true":+ result = True+ elif response_text == "false":+ result = False+ else:+ # If the response is not clear, try to interpret it using word boundaries+ import re++ # Use word boundaries to avoid false positives like 'untrue' or '10'+ tokens = set(re.findall(r"\b(true|false|yes|no|1|0)\b", response_text))++ if tokens == {"true"} or tokens == {"yes"} or tokens == {"1"}:+ result = True+ elif tokens == {"false"} or tokens == {"no"} or tokens == {"0"}:+ result = False+ else:+ # Unclear or conflicting response - default to False and yield error+ result = False+ yield "error", f"Unclear AI response: '{response.response}'"++ # Update internal stats+ self.merge_stats(+ NodeExecutionStats(+ input_token_count=response.prompt_tokens,+ output_token_count=response.completion_tokens,+ )+ )+ self.prompt = response.prompt++ except Exception as e:+ # In case of any error, default to False to be safe+ result = False+ # Log the error but don't fail the block execution+ import logging++ logger = logging.getLogger(__name__)+ logger.error(f"AI condition evaluation failed: {str(e)}")+ yield "error", f"AI evaluation failed: {str(e)}"++ # Yield results+ yield "result", result++ if result:+ yield "yes_output", yes_value+ else:+ yield "no_output", no_valuediff --git a/docs/content/platform/blocks/ai_condition.md b/docs/content/platform/blocks/ai_condition.mdnew file mode 100644index 0000000000..2643d5eff5--- /dev/null+++ b/docs/content/platform/blocks/ai_condition.md@@ -0,0 +1,79 @@+# AI Condition Block++## What it is+The AI Condition Block is a logical component that uses artificial intelligence to evaluate natural language conditions and produces outputs based on the result. This block allows you to define conditions in plain English rather than using traditional comparison operators.++## What it does+This block takes an input value and a natural language condition, then uses AI to determine whether the input satisfies the condition. Based on the result, it provides conditional outputs similar to a traditional if/else statement but with the flexibility of natural language evaluation.++## How it works+The block uses a Large Language Model (LLM) to evaluate the condition by:+1. Converting the input value to a string representation+2. Sending a carefully crafted prompt to the AI asking it to evaluate whether the input meets the specified condition+3. Parsing the AI's response to determine a true/false result+4. Outputting the appropriate value based on the result++## Inputs+| Input | Description |+|-------|-------------|+| Input Value | The value to be evaluated (can be text, number, or any data type) |+| Condition | A plaintext English description of the condition to evaluate |+| Yes Value | (Optional) The value to output if the condition is true. If not provided, Input Value will be used |+| No Value | (Optional) The value to output if the condition is false. If not provided, Input Value will be used |+| Model | The LLM model to use for evaluation (defaults to GPT-4o) |+| Credentials | API credentials for the LLM provider |++## Outputs+| Output | Description |+|--------|-------------|+| Result | A boolean value (true or false) indicating whether the condition was met |+| Yes Output | The output value if the condition is true. This will be the Yes Value if provided, or Input Value if not |+| No Output | The output value if the condition is false. This will be the No Value if provided, or Input Value if not |+| Error Message | Error message if the AI evaluation is uncertain or fails (empty string if successful) |++## Examples++### Email Address Validation+- **Input Value**: `"[email protected]"`+- **Condition**: `"the input is an email address"`+- **Result**: `true`+- **Yes Output**: `"[email protected]"` (or custom Yes Value)++### Geographic Location Check+- **Input Value**: `"San Francisco"`+- **Condition**: `"the input is a city in the USA"`+- **Result**: `true`+- **Yes Output**: `"San Francisco"` (or custom Yes Value)++### Error Detection+- **Input Value**: `"Error: Connection timeout"`+- **Condition**: `"the input is an error message or refusal"`+- **Result**: `true`+- **Yes Output**: `"Error: Connection timeout"` (or custom Yes Value)++### Content Classification+- **Input Value**: `"This is a detailed explanation of how machine learning works..."`+- **Condition**: `"the input is the body of an email"`+- **Result**: `false` (it's more like article content)+- **No Output**: Custom No Value or the input value++## Possible Use Cases+- **Content Classification**: Automatically classify text content (emails, articles, comments, etc.)+- **Data Validation**: Validate input data using natural language rules+- **Smart Routing**: Route data through different paths based on AI-evaluated conditions+- **Quality Control**: Check if content meets certain quality or format standards+- **Language Detection**: Determine if text is in a specific language or style+- **Sentiment Analysis**: Evaluate if content has positive, negative, or neutral sentiment+- **Error Handling**: Detect and route error messages or problematic inputs++## Advantages over Traditional Condition Blocks+- **Flexibility**: Can handle complex, nuanced conditions that would be difficult to express with simple comparisons+- **Natural Language**: Uses everyday language instead of programming logic+- **Context Awareness**: AI can understand context and meaning, not just exact matches+- **Adaptability**: Can handle variations in input format and wording++## Considerations+- **Performance**: Requires an API call to an LLM, which adds latency compared to traditional conditions+- **Cost**: Each evaluation consumes LLM tokens, which has associated costs+- **Reliability**: AI responses may occasionally be inconsistent, so critical logic should include fallback handling+- **Network Dependency**: Requires internet connectivity to access the LLM API\ No newline at end of file