Build AI Code Generation Tools For Large Scale Project in Python? Part 1 - Development Diary and Discussion

Building AI Code Generation Tools from Scratch in Python 3: A Journey from Zero to Everything (Part 1)

Can we create AI code generation tools in Python 3 from the ground up? This is an open-ended question, and I don't claim to have all the answers. I welcome discussion and idea exchange, as I'm always open to learning. This exploration also reveals how I built the ITDOGTICS application for first prototype in just three days.

While I believe there are limitations to current AI technology, I'll address those in a future blog post where I'll delve into how AI works and why it has become so prominent. If you're familiar with basic AI concepts, feel free to skip the introduction to Scope of this Blog Post.

This blog post assumes you have a basic understanding of Python 3. If you don't, it might be challenging to follow along.

Disclaimer:

I won't provide the complete project code, as I encourage discussion and collaboration.

  • For beginners: Use this as a reference to build your own project and join the conversation. I'm happy to help with any learning challenges.
  • For experienced developers: Please share your insights and discuss better approaches.

Objective:

  • We know what we want to build, but we prefer a template to starting from scratch.
  • Automate code writing for a large project (multiple files, not just a single function) using Python 3.
  • Ideally, eliminate the need to pay for AI code generation tools.

Understanding AI Code Generation:

AI code generation involves three key components:

  1. Prompt: The input to the Large Language Model (LLM).
  2. LLM: Models like ChatGPT, Gemini, and Claude.
  3. Code Generation Tools: Tools like Co-pilot, Windsurf, and Cursor, which use LLMs to automate code output.

Building an LLM is expensive, requiring vast amounts of data and processing power. For this project, I'll use Claude (I lack the resources to train a new LLM). Therefore, we'll focus on the Prompt and how to leverage an existing LLM.

Observations:

Most AI code generation tools share these features:

  1. Codebase analysis.
  2. Code generation.

Scope of this Blog Post:

  1. We define the requirements for what we want to build.
  2. The tool generates a project (multiple files, readable and maintainable code).
  3. Developers can then refine the generated code (further automation will be discussed in a later post).

Implementation Details:

Required Libraries:

  1. anthropic (for the Claude API)
  2. os (for automation)
  3. subprocess (for automation)
  4. shutil (for file handling)
  5. re (regular expressions)

Step 1: Defining the Requirements:

What information does the AI need to build and run the project?

  1. System overview
  2. Framework, tools, and libraries
  3. Features
  4. File structure
  5. Implementation details


Instead of writing all this manually, why not use AI to generate it? You can use your existing knowledge to create an initial prompt. An example, used for my expense tracker, can be found here: https://github.com/jeff214103/expense-tracker/blob/main/prompt.txt

Step 2: Building the Code:

This simulates a software engineer's daily workflow, which can be broken down into five procedures:

  1. Determine workflow
  2. Project initialization and dependency installation
  3. Code file generation
  4. Compilation
  5. Execution

We'll need basic functions to automate file creation, setup, and other commands:

  1. run_shell_command: Controls the OS for automation.
  2. run_command_with_resolution: Handles potential errors from run_shell_command. A non-zero return code indicates an error, which needs resolution.
  3. resolve_error: Sends the error to the LLM for suggested solutions.
  4. strip_code_markdown: Extracts code from markdown.

Determining the Workflow (Step 0):

This crucial step drives the entire project and generates the commands needed later. The workflow is defined in JSON format for easy parsing by the program.


The workflow can then be implemented as follows. Project initialization, dependency installation, compilation, and execution use the run_command_with_resolution function for automation.


Code Generation (The Hardest Part):

For large projects, code generation can be divided into two processes:

  1. Obtain the file structure and file descriptions.
  2. Generate the code for each file.

Let the LLM determine the file structure, including descriptions to inform later code generation.

For each file:

  1. Create the directory.
  2. Create the file.
  3. Generate the content.
spec: the spec string
description: the description of the file
file_path: which file it is generating
file_structure: a string representation of the structure

This finally creates the initial project structure.

Challenges:

  1. Token exhaustion: Large files can exceed token limits.
  2. Code skipping: The LLM might skip parts of the implementation.
  3. Inconsistent code generation: Function naming inconsistencies between files.

Initial Thoughts on Overcoming Challenges:

  1. Chunking: Break down files and use a diff-like approach.
  2. Code skipping: Refine the prompt.
  3. Contextual awareness: Include relevant code from other files in the prompt.

Next Steps:

In the next blog post, I'll share my testing results on overcoming compilation errors (at least making the project runnable).

Stay tuned! I welcome all comments and thoughts on this implementation. Please leave a comment so we can discuss further.

Btw, I don't know how many parts will be going to have with this blog.  Please give your support by sharing or comment if you are interested. 

Comments

Popular posts from this blog

Resume Generator - Software Intro

Expense Tracker - Software Intro