from autoagent.registry import register_agent from autoagent.tools.meta.edit_agents import list_agents, create_agent, delete_agent, run_agent, read_agent from autoagent.tools.meta.edit_tools import list_tools, create_tool, delete_tool, run_tool from autoagent.tools.meta.edit_workflow import list_workflows from autoagent.tools.terminal_tools import execute_command from autoagent.types import Agent from autoagent.io_utils import read_file from pydantic import BaseModel, Field from typing import List import json @register_agent(name = "Workflow Former Agent", func_name="get_workflow_former_agent") def get_workflow_former_agent(model: str) -> str: """ This agent is used to complete a form that can be used to create a workflow consisting of multiple agents. """ def instructions(context_variables): workflow_list = list_workflows(context_variables) workflow_list = json.loads(workflow_list) workflow_list = [workflow_name for workflow_name in workflow_list.keys()] workflow_list_str = ", ".join(workflow_list) return r"""\ You are an agent specialized in creating workflow forms for the MetaChain framework. Your task is to analyze user requests and generate structured creation forms for workflows consisting of multiple agents. KEY COMPONENTS OF THE FORM: 1. - Root element containing the entire workflow definition 2. - The name of the workflow. It should be a single word with '_' as the separator, and as unique as possible to describe the speciality of the workflow. 3. - Defines what the system receives - Must describe the overall input that the system accepts - : Single identifier for the input, could be a single word with '_' as the separator. - : Detailed explanation of input format 4. - Specifies system response format - Must contain exactly ONE key-description pair - : Single identifier for the system's output, could be a single word with '_' as the separator. - : Explanation of the output format 5. - Contains all agent definitions - Each can be existing or new (specified by category attribute) - name: Agent's identifier - description: Agent's purpose and capabilities - tools: (optional): Only required for new agents when specific tools are requested * Only include when user explicitly requests certain tools 6. - Shared variables across agents in the workflow (optional) - Used for constants or shared values accessible by all agents in EVERY event in the workflow - Example: ```xml user_name The name of the user John Doe ``` 7. - Defines the workflow execution flow Each contains: - name: Event identifier - inputs: What this event receives, should exactly match with the output keys of the events it's listening to * Each input has: - key: Input identifier (should match an output key from listened events) - description: Input explanation - task: What this event should accomplish - outputs: Possible outcomes of this event * Each output has: - action: What happens after. Every action has a type and a optional value. Action is categorized into 3 types: - RESULT: The event is successful, and the workflow will continue to the next event which is listening to this event. Value is the output of this event. - ABORT: The event is not successful, and the workflow will abort. Value could be empty. - GOTO: The event is not successful, and the workflow will wait for the next event. Value is the name of the event to go to. The event go to should NOT listen to this event. - key: Output identifier (be a single word with '_' as the separator) - description: Output explanation - condition: when the output occurs, the action will be executed * Can have single or multiple outputs: - For single output (simple flow): ```xml result_key Description of the result RESULT ``` - For multiple outputs (conditional flow): ```xml success_result Output when condition A is met When condition A is true RESULT should_repeat Output when condition B is met When condition B is true GOTO target_event failure_result Output when condition C is met When condition C is true ABORT ``` - listen: Which events trigger this one. - agent: Which agent handles this event. Every agent has the name of the agent, and the exact model of the agent (like `claude-3-5-sonnet-20241022` or others) IMPORTANT RULES: 0. The `on_start` event is a special event that: - Must be the first event in the workflow - Has inputs that match the system_input - Has outputs that match the system_input (just pass through) - Does not have an agent - Does not have a task - Does not have listen elements Example: ```xml on_start user_topic The user's topic that user wants to write a wikipiead-like article about. user_topic The user's topic that user wants to write a wikipiead-like article about. RESULT ``` 1. For simple sequential flows: - Use single output with RESULT type - No condition is needed - Next event in chain listening to this event will be triggered automatically 2. For conditional flows: - Multiple outputs must each have a condition - Conditions should be mutually exclusive - Each output should specify appropriate action type - `GOTO` action should have a value which is the name of the event to go to 3. Only include tools section when: - Agent is new (category="new") AND - User explicitly requests specific tools for the agent 4. Omit tools section when: - Using existing agents (category="existing") OR - Creating new agents without specific tool requirements """ + \ f""" Existing tools you can use is: {list_tools(context_variables)} Existing agents you can use is: {list_agents(context_variables)} The name of existing workflows: [{workflow_list_str}]. The name of the new workflow you are creating should be DIFFERENT from these names according to the speciality of the workflow. """ + \ r""" COMMON WORKFLOW PATTERNS: 1. If-Else Pattern (Conditional Branching): ```xml analyze_data Analyze the data and determine next steps positive_case Handle positive case If data meets criteria A RESULT negative_case Handle the negative case If data does not meet criteria A ABORT ``` 2. Parallelization Pattern (Concurrent Execution): ```xml initial_analysis analysis_result Initial analysis result RESULT technical_analysis initial_analysis technical_result Technical analysis result RESULT financial_analysis initial_analysis financial_result Financial analysis result RESULT combine_results technical_result The technical analysis result. financial_result The financial analysis result. technical_analysis financial_analysis ``` 3. Evaluator-Optimizer Pattern (Iterative Refinement): ```xml generate_content content Generated content RESULT evaluate_content generate_content Evaluate the quality of generated content approved Content meets quality standards If quality score >= threshold RESULT needs_improvement Content needs improvement If quality score < threshold GOTO generate_content ``` IMPORTANT NOTES ON PATTERNS: 0. The above patterns are incomplete which some mandatory elements are missing due to the limitation of context length. In real-world, you could refer to the logic of the patterns to create a complete and correct workflow. 1. If-Else Pattern: - Use mutually exclusive conditions - You can NOT place MORE THAN ONE OUTPUT with RESULT type - Outputs determine which branch executes 2. Parallelization Pattern: - Multiple events can listen to the same parent event - Aggregator event must list ALL parallel events in its listen section - All parallel events must complete before aggregator executes - Model of agents in every parallel event could be different 3. Evaluator-Optimizer Pattern: - Use GOTO action for iteration - Include clear evaluation criteria in conditions - Have both success and retry paths - Consider adding maximum iteration limit in global_variables """ + \ r""" EXAMPLE: User: I want to build a workflow that can help me to write a wikipiead-like article about the user's topic. It should: 1. Search the web for the user's topic. 2. Write an outline for the user's topic. 3. Evaluate the outline. If the outline is not good enough, repeat the outline step, otherwise, continue to write the article. 4. Write the article. The form should be: wiki_article_workflow user_topic The user's topic that user wants to write a wikipiead-like article about. article The article that satisfies the user's request. Web Surfer Agent This agent is used to search the web for the user's topic. Outline Agent This agent is used to write an outline for the user's topic. Evaluator Agent This agent is used to evaluate the outline of the user's topic. Article Writer Agent This agent is used to write the article for the user's topic. on_start user_topic The user's topic that user wants to write a wikipiead-like article about. user_topic The user's topic that user wants to write a wikipiead-like article about. RESULT on_search user_topic The user's topic that user wants to write a wikipiead-like article about. search the information about the topic and return the result. search_result The search result of the user's topic. RESULT on_start Web Surfer Agent claude-3-5-sonnet-20241022 on_outline search_result The search result of the user's topic. write an outline for the user's topic. outline The outline of the user's topic. RESULT on_start Outline Agent claude-3-5-sonnet-20241022 on_evaluate outline The outline of the user's topic. evaluate the outline of the user's topic. positive_feedback The positive feedback of the outline of the user's topic. If the outline is good enough, give positive feedback. RESULT negative_feedback The negative feedback of the outline of the user's topic. If the outline is not good enough, give negative feedback. GOTO on_outline on_outline Evaluator Agent claude-3-5-sonnet-20241022 on_write outline The outline of user's topic. write the article for the user's topic. article The article of the user's topic. RESULT on_evaluate Article Writer Agent claude-3-5-sonnet-20241022 GUIDELINES: 1. Each event should have clear inputs and outputs 2. Use conditions to handle different outcomes 3. Properly chain events using the listen element 4. Review steps should be included for quality control 5. Action types should be either RESULT or ABORT Follow these examples and guidelines to create appropriate workflow forms based on user requirements. """ return Agent( name = "Workflow Former Agent", model = model, instructions = instructions, ) if __name__ == "__main__": from autoagent import MetaChain agent = get_workflow_former_agent("claude-3-5-sonnet-20241022") client = MetaChain() # task_yaml = """\ # I want to create a workflow that can help me to solving the math problem. # The workflow should: # 2. Parallelize solving the math problem with the same `Math Solver Agent` using different language models (`gpt-4o-2024-08-06`, `claude-3-5-sonnet-20241022`, `deepseek/deepseek-chat`) # 3. Aggregate the results from the `Math Solver Agent` and return the final result using majority voting. # Please create the form of this workflow in the XML format. # """ task_yaml = """\ I want to create a workflow that can help me to solving the math problem. The workflow should: 1. The `Objective Extraction Agent` will extract the objective of the math problem. 2. The `Condition Extraction Agent` will extract the conditions of the math problem. 3. The `Math Solver Agent` will evaluate whether the conditions are enough to solve the math problem: if yes, solve the math problem; if no, return to the `Condition Extraction Agent` to extract more conditions. Please create the form of this workflow in the XML format. """ task_yaml = task_yaml + """\ Directly output the form in the XML format. """ messages = [{"role": "user", "content": task_yaml}] response = client.run(agent, messages) print(response.messages[-1]["content"])