oneaiguru / chat2code

0 stars 0 forks source link

Prompts #1

Open oneaiguru opened 14 hours ago

oneaiguru commented 14 hours ago

Understanding Your Task and Requirements

To ensure I fully grasp your task and address all your points, I'll summarize your requirements and key details from your messages:

  1. Objective: Develop a tool that automates updating your codebase with code generated by LLMs (e.g., Claude.ai, ChatGPT with Code Interpreter), with minimal human intervention.

  2. Current Challenges:

    • LLM outputs vary in format:
      • Some outputs are complete files.
      • Others are incomplete, with placeholders like "rest of methods are not changed."
      • Multiple files might be included in one code block, separated by comments with filenames.
    • Filenames or file paths are usually included in comment lines but may not always be precise.
    • Inconsistent formatting makes automation challenging.
    • You sometimes get code that isn't directly usable and needs cleaning or manual review.
  3. Constraints and Considerations:

    • The tool should focus on updating files with minimal manual effort.
    • Complex features like change approvals or rollbacks are not priorities for the first version.
    • You're willing to adjust your process to simplify the tool's development (e.g., providing only code blocks without full messages).
    • Tools like ChatGPT's Code Interpreter and Claude 3.5 have been effective in generating code and understanding tasks.
    • Modularizing tasks is important due to prompt size limitations with some models.
    • You have previous experience using custom GPT instructions to automate complex tasks, including handling zip files, using SQLite databases to track tasks, and performing file operations.
  4. Desired Approach:

    • Break down the task into manageable pieces.
    • In each step, know how to instruct ChatGPT and get separate working modules step by step.
    • Develop an outline of a development plan that includes prompts and code snippets to use with ChatGPT, ensuring you get working code.
    • Understand how to convert the code when it passes all tests to run it locally.
    • Use previous custom GPT instructions as inspiration for structuring the task.
  5. Resources Provided:

    • Sample outputs from LLMs (numbered .py files) that include code updates.
    • Your project's code structure for context.
    • Examples of custom GPT instructions you've used in the past for complex tasks.

Implementation Plan

To achieve your objective with minimal human intervention, we'll develop a step-by-step plan that leverages ChatGPT with Code Interpreter and possibly Claude 3.5. The plan will focus on:


Step 1: Define the Workflow and Requirements

Goal: Clearly outline each component of the tool and the overall workflow.

Actions:

Deliverables:


Step 2: Break Down the Task into Modules

Goal: Identify and define the individual modules/functions needed.

Modules:

  1. File Input Module:

    • Purpose: Read LLM outputs from files (e.g., numbered .py files).
    • Functionality:
      • Read code blocks from files.
      • Handle different formats (single file, multiple files separated by comments).
      • Ignore non-code content if necessary.
  2. Parsing Module:

    • Purpose: Parse the code blocks to extract filenames and corresponding code.
    • Functionality:
      • Identify file separators (e.g., comments with filenames).
      • Handle cases where filenames are incomplete or missing.
      • Manage incomplete code blocks (detect placeholders like "rest of methods are not changed").
  3. Mapping Module:

    • Purpose: Map extracted code blocks to the correct files in your codebase.
    • Functionality:
      • Resolve filenames to actual file paths in your project directory.
      • Handle potential ambiguities or discrepancies in filenames.
  4. Backup Module:

    • Purpose: Create backups of original files before updating.
    • Functionality:
      • Copy original files to a backup directory.
      • Ensure backups are properly versioned or timestamped.
  5. Update Module:

    • Purpose: Update the codebase with the new code.
    • Functionality:
      • Overwrite existing files with new code blocks.
      • Handle partial updates intelligently (e.g., only replace certain sections).
      • Ensure code syntax is valid after updates.
  6. Validation Module:

    • Purpose: Validate that the updates have been applied correctly.
    • Functionality:
      • Compare updated files with original versions.
      • Run automated tests (if available).
      • Report any issues or discrepancies.
  7. Task Tracking Module:

    • Purpose: Keep track of the update tasks and their statuses.
    • Functionality:
      • Use a simple data structure (e.g., a list or a SQLite database) to record tasks.
      • Update statuses as tasks are completed.

Step 3: Craft Prompts for ChatGPT

Goal: Develop clear and specific prompts to guide ChatGPT in generating code for each module.

Actions:

Sample Prompts:

  1. File Input Module Prompt:

    I need a Python function that reads code blocks from multiple `.py` files. Each file contains code generated by an LLM and may include one or more code blocks. Code blocks may be separated by comments that contain filenames or file paths. The function should:
    
    - Read all `.py` files from a specified directory.
    - Extract code blocks, along with any preceding comments that indicate the filename.
    - Return a list of tuples containing the filename and the corresponding code block.
    
    Please include error handling for cases where the filename is missing or the code block is incomplete. Also, write unit tests to verify the function works as expected with various input formats.
  2. Parsing Module Prompt:

    Building upon the previous function that reads code blocks, I need to parse the extracted data to handle different cases:
    
    - If a code block includes a placeholder like "rest of methods are not changed," detect that the code is incomplete and mark it for manual review.
    - Normalize filenames from the comments to match actual file paths in the codebase.
    - Handle cases where multiple files are included in one code block.
    
    Please update the function accordingly and provide unit tests for these scenarios.
  3. Backup Module Prompt:

    I need a Python function that creates backups of files before they are updated. The function should:
    
    - Accept a list of file paths to back up.
    - Copy each file to a backup directory, preserving the directory structure.
    - Include a timestamp or version number in the backup filenames to prevent overwriting previous backups.
    
    Please ensure the function handles errors gracefully, such as when a file does not exist.
  4. Update Module Prompt:

    I need a Python function that updates files in the codebase with new code. The function should:
    
    - Accept a list of tuples containing file paths and new code blocks.
    - Overwrite the existing files with the new code.
    - If the code block is incomplete or marked for manual review, skip the update and log a warning.
    
    Include unit tests to verify that the function correctly updates files and handles skipped updates.
  5. Validation Module Prompt:

    I need a Python function that validates the updated files. The function should:
    
    - Compare each updated file with its original backup to ensure changes were applied.
    - Optionally run any existing unit tests in the codebase to ensure no functionality is broken.
    - Generate a report summarizing the results.
    
    Please include code that runs a simple test suite and outputs the results.
  6. Task Tracking Module Prompt:

    I need a simple task tracking system to monitor the status of file updates. The system should:
    
    - Use a SQLite database to record tasks, with fields for file name, status, and any error messages.
    - Update the task status as each file is processed (e.g., 'pending', 'updated', 'skipped', 'error').
    - Provide functions to add tasks, update statuses, and retrieve task summaries.
    
    Include code to initialize the database and unit tests for the task tracking functions.

Step 4: Use ChatGPT with Code Interpreter to Generate Code

Goal: Use the crafted prompts to have ChatGPT generate the code for each module.

Actions:


Step 5: Assemble and Integrate Modules

Goal: Combine the individual modules into a cohesive tool.

Actions:


Step 6: Adapt the Tool to Run Locally

Goal: Transfer the tool from the Code Interpreter environment to your local machine.

Actions:


Step 7: Implement Error Handling and Logging

Goal: Enhance the tool's robustness by adding error handling and logging.

Actions:


Step 8: Final Validation and Documentation

Goal: Ensure the tool meets all requirements and is properly documented.

Actions:


Step 9: Plan for Future Enhancements

Goal: Identify areas for improvement and plan for additional features.

Actions:


Sample Code and Instructions

Below are examples inspired by your previous custom GPT instructions, adapted to fit the current task.

  1. File Input and Parsing Module:

    import os
    import re
    
    def read_and_parse_llm_outputs(directory):
       file_data = []
       for filename in os.listdir(directory):
           if filename.endswith('.py'):
               filepath = os.path.join(directory, filename)
               with open(filepath, 'r', encoding='utf-8') as file:
                   content = file.read()
    
                   # Split content into code blocks based on filename comments
                   blocks = re.split(r'#{10,}\s*(.*?)\s*#{10,}', content)
                   for i in range(1, len(blocks), 2):
                       file_comment = blocks[i].strip()
                       code_block = blocks[i + 1].strip()
                       if code_block:
                           file_data.append((file_comment, code_block))
       return file_data
  2. Backup Module:

    import shutil
    import datetime
    
    def backup_files(file_paths, backup_root):
       timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
       for file_path in file_paths:
           backup_path = os.path.join(backup_root, timestamp, file_path)
           os.makedirs(os.path.dirname(backup_path), exist_ok=True)
           shutil.copy2(file_path, backup_path)
  3. Update Module:

    def update_files(file_updates):
       for file_path, new_code in file_updates:
           with open(file_path, 'w', encoding='utf-8') as file:
               file.write(new_code)
  4. Validation Module:

    import filecmp
    
    def validate_updates(original_files, updated_files):
       mismatches = []
       for orig, updated in zip(original_files, updated_files):
           if not filecmp.cmp(orig, updated, shallow=False):
               mismatches.append(orig)
       return mismatches

Guidelines for Instructing ChatGPT


Converting Code to Run Locally


Conclusion

By following this plan, you can systematically develop a tool that automates updating your codebase with LLM-generated code, minimizing manual intervention. Breaking the task into manageable modules allows you to leverage ChatGPT's strengths effectively, ensuring each component works correctly before integration.

Next Steps

Additional Considerations

I hope this detailed plan aligns with your needs and provides a clear path forward. Let me know if you'd like further assistance with any of the steps or if you have additional questions.