Who doesn’t want an AI software engineer?

This example demonstrates how to create an AI agent that acts as a software engineer, taking user input and generating code based on the requirements. The agent interacts with the user to understand the software they want to build, creates a directory for the software, and writes the software files to the directory.

This agent will be able to read, write, and delete files on your system. Make sure you understand the code before running it!

from pathlib import Path
import controlflow as cf
from controlflow.tools import filesystem, code
from pydantic import BaseModel

class DesignDoc(BaseModel):
    goals: str
    design: str
    implementation_details: str
    criteria: str

# Load the instructions
# instructions = Path(__file__).parent.joinpath("instructions.md").read_text()
instructions = Path('/tmp/instructions.md').read_text()

# Create the agent
engineer = cf.Agent(
    name="Engineer",
    instructions=instructions,
    tools=[
        *filesystem.ALL_TOOLS,
        code.python,
        code.shell,
    ],
)

@cf.flow(default_agent=engineer, instructions='Do not give up until the software works.')
def software_engineer_flow():
    # Task 1: Create design document
    design_doc = cf.run(
        "Learn about the software the user wants to build",
        instructions="""
            Interact with the user to understand the software they want to build.
            What is its purpose? What language should you use? What does it need to do?
            Engage in a natural conversation to collect information.
            Once you have enough, write out a design document to complete the task.
            """,
        interactive=True,
        result_type=DesignDoc,
    )

    # Task 2: Create project directory
    project_dir = cf.run(
        "Create a directory for the software",
        instructions="""
            Create a directory to store the software and related files.
            The directory should be named after the software. Return the path.
            """,
        result_type=str,
        tools=[filesystem.mkdir],
    )

    # Task 3: Implement the software
    cf.run(
        "Implement the software",
        instructions="""
            Implement the software based on the design document.
            All files must be written to the provided project directory.
            Continue building and refining until the software runs as expected and meets all requirements.
            Update the user on your progress regularly.
            """,
        context=dict(design_doc=design_doc, project_dir=project_dir),
        result_type=None,
    )

if __name__ == "__main__":
    software_engineer_flow()