/ examples / di / data_analyst_write_code.py
data_analyst_write_code.py
 1  import fire
 2  
 3  from metagpt.logs import logger
 4  from metagpt.roles.di.data_analyst import DataAnalyst
 5  
 6  
 7  async def main():
 8      # Create an instance of DataAnalyst role
 9      analyst = DataAnalyst()
10  
11      # Set the main goal for the planner - constructing a 2D array
12      analyst.planner.plan.goal = "construct a two-dimensional array"
13  
14      # Add a specific task to the planner with detailed parameters:
15      # - task_id: Unique identifier for the task
16      # - dependent_task_ids: List of tasks that need to be completed before this one (empty in this case)
17      # - instruction: Description of what needs to be done
18      # - assignee: Who will execute the task (David)
19      # - task_type: Category of the task (DATA_ANALYSIS)
20      analyst.planner.plan.append_task(
21          task_id="1",
22          dependent_task_ids=[],
23          instruction="construct a two-dimensional array",
24          assignee="David",
25          task_type="DATA_ANALYSIS",
26      )
27  
28      # Execute the code generation and execution for creating a 2D array
29      # The write_and_exec_code method will:
30      # 1. Generate the necessary code for creating a 2D array
31      # 2. Execute the generated code
32      # 3. Return the result
33      result = await analyst.write_and_exec_code("construct a two-dimensional array")
34  
35      # Log the result of the code execution
36      logger.info(result)
37  
38  
39  if __name__ == "__main__":
40      fire.Fire(main)