How to Automate Writing Software with LangChain

Introduction to LangChain
  • The Strategy

  • Setting up the project

  • The Technical Requirements

  • The Class Structure

  • The File Structure

  • The File Paths

  • The Code

  • Iterating!


Below is the code used in the video!

For app.py:

from dotenv import load_dotenv

load_dotenv()

from utils import safe_write
from chains import (
    product_manager_chain,
    tech_lead_chain,
    file_structure_chain,
    file_path_chain,
    code_chain,
    missing_chain,
    new_classes_chain
)


request = """
Build a software for AutoML in Pytorch. 
The code should be able to search the best architecture and hyperparameters for a CNN network based on different metrics.
You can use all the necessary packages you need.
"""

design = product_manager_chain.run(request)
class_structure = tech_lead_chain.run(design)
file_structure = file_structure_chain.run(class_structure)
files = file_path_chain.run(file_structure)

files_list = files.split('\n')

missing = True
missing_dict = {
    file: True for file in files_list
}

code_dict = {}

while missing:

    missing = False
    new_classes_list = []

    for file in files_list:

        if not missing_dict[file]:
            safe_write(file, code_dict[file])
            continue

        code = code_chain.predict(
            class_structure=class_structure,
            structure=file_structure, 
            file=file,
        )

        code_dict[file] = code
        response = missing_chain.run(code=code)
        missing_dict[file] = '<TRUE>' in response
        missing = missing or missing_dict[file]

        if missing_dict[file]:
            new_classes = new_classes_chain.predict(
                class_structure=class_structure,
                code=code
            )
            new_classes_list.append(new_classes)

    class_structure += '\n\n' + '\n\n'.join(new_classes_list)

The code in chains.py:

from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI


llm = ChatOpenAI(model_name='gpt-3.5-turbo-16k')

prompt = """
System: You are a Product Manager, and your job is to design software. 
You are provided a rough description of the software. 
Expand on this description and generate the complete set of functionalities needed to get that software to work.
Don't hesitate to make design choices if the initial description doesn't provide enough information. 

Human: {input}

Complete software design:
"""

product_manager_chain = LLMChain.from_string(
    llm=llm,
    template=prompt
)


prompt = """
System: You are a Software Engineer writing code in Python. 
Your job is to come up with a detailled description of all the necessary functions, classes, methods and attributes for the following software description.
Make sure to design a software that incorporates all the best practices of software development.
Make sure you describe how all the different classes and function interact between each other. 
The resulting software should be a fully functional one. 

Software description: 
{input}

Software design:
"""

tech_lead_chain = LLMChain.from_string(
    llm=llm,
    template=prompt
)


prompt = """
System: You are a Software Engineer and your job is to design software in Python. 
Provide a detailled description of the file structure with the required folders and Python files.
Make sure to design a file structure that incorporates all the best practices of software development.
Make sure you explain in which folder each file belong to.
Folder and file names should not contain any white spaces. 
A human should be able to exactly recreate that file structure.
Make sure that those files account for the design of the software
Don't generate non-python files.

Software design: {input}

File structure:
"""

file_structure_chain = LLMChain.from_string(
    llm=llm,
    template=prompt
)


prompt = """
System: Return the complete list of the file paths, including the folder structure using the following list of python files.
Only return well formed file paths: ./<FOLDER_NAME>/<FILE_NAME>.py

Follow the following template:
<FILE_PATH 1>
<FILE_PATH 2>
...

Human: {input}

File paths list:
"""

file_path_chain = LLMChain.from_string(
    llm=llm,
    template=prompt
)


prompt = """
System: You are a Software Engineer. Your job is to write python code.
Write the code for the following file using the following description. Only return code! The code should be able to run in a python interpreter.
Make sure to implement all the methods and functions. 
Make sure to import all the necessary packages.
The code should be complete.

Files structure: {structure}

Software description: {class_structure}

File name: {file}

Python Code for this file:
"""

code_chain = LLMChain.from_string(
    llm=llm,
    template=prompt
)


prompt = """
Return `<TRUE>` If the following Python code contains non-implemented parts and return `<FALSE>` otherwise

If a python code contains `TODO` or `pass`, it means the code is not implemented.

code: {code}

Return `<TRUE>` if the code is not implemented and return `<FALSE>` otherwise:
"""

missing_chain = LLMChain.from_string(
    llm=llm,
    template=prompt
)

prompt = """
System: You are a Software Engineer. The following Python code may contain non-implemented functions.
If the code contains non-implemented functions, describe what additional functions or classes you would need to implement those missing implementations.
Provide a detailled description of those additional classes or functions that you need to implement. 
Make sure to design a software that incorporates all the best practices of software development.

Class description: {class_structure}

Code: {code}

Only return text if some functions are not implemented.
The new classes and functions needed:
"""

new_classes_chain = LLMChain.from_string(
    llm=llm,
    template=prompt
)

The code for utils.py:

import os

def safe_write(path, code):
    path = "./software/" + path
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, 'w+') as f:
        f.write(code)
0 Comments
The AiEdge Newsletter
The AiEdge Newsletter
Authors
Damien Benveniste