Chatcopy - A ChatGPT Assisted Coding Utility

Chatcopy - A ChatGPT Assisted Coding Utility

June 27, 2023
OpenAI, Crystal Lang, ChatCopy, AI Assisted Coding, Avdi Grimm, Martin Fowler, Hao's Prompting Method, Chain of Thought Prompting, guppi, Code Assistant

I was helping a colleague get setup with my semi-autonomous coding assistant utility called guppi recently and realized that he didn’t have access to GPT-4 through the API. GPT-4 is a lot better at coding. This got me thinking that I need to create a solution that works for him that he can use in Chat GPT directly (for now). So, inspired by the work I did on guppi, I made ChatCopy. Several clipboard writing utilities appeared soon after Chat GPT 4 was released, but mine has a little unique flair to it I think. When you feed in the prompt, chatcopy will send the file tree of your current directory to the Chat GPT API and ask it which files it thinks are relevant to the prompt. It gets back a list of files, reads the contents of those files and writes them into your clipboard. I’ve also appended the original prompt for convenience.

I’m hoping this tool serves to streamline the coding process, especially for developers who have access to GPT-4 through Chat GPT Pro but not yet the GPT-4 API.

Chain of Thought Prompting #

At the heart of this utility’s effectiveness is “chain of thought prompting”. It’s an iterative process where the AI is prompted multiple times, each time refining the instruction based on the AI’s previous output. The exact process I use was inspired by Martin Fowler’s blog post. My process has three steps:

  1. Generate Master Plan: Begin by prompting the AI with a high-level description of the task to get a broad plan.
System: A tiny command line utility for copying relevant parts of your code base into your clipboard related to a prompt for the purpose of pasting it into Chat GPT.
Tech Stack: Crystal Lang
Dependencies: OpenAI.cr

All code should be written in the tech stack above. Requirements should be implemented as Crystal Lang classes or modules.

Right now the -h help command option runs the chatcopy application, which it isn't supposed to do. It's only supposed to print the help options. Please refactor the cli.cr file to handle this.

Provide an overall solution following the guidance mentioned above. Don’t generate code. Describe the solution, and breaking the solution down as a task list based on the guidance mentioned above. And we will refer this task list as our master plan.

The only thing I change about this prompt is the penultimate sentence. Whatever feature I want to implement is specified there in as much detail as I have the patience for. When using chatcopy, I just place this in prompt.md at the root of my project, and run the chatcopy command. I then take the result, copy it into Chat GPT and run it.

Sometimes the master plan needs refinement so I might prompt it a few times to make modifications. If the master plan looks good, I continue on to the next step.

  1. Rewrite the Master Plan and Generate High Level Code: Refine the plan based on the AI’s output, and prompt the AI to generate high-level code. This may require multiple iterations and revisions of the master plan.

I then ask Chat GPT to:

Rewrite the master plan, this time with detailed Classes, methods, and arguments

The output might look something like this:

Sure, here is the updated master plan with detailed descriptions of classes, methods, and arguments:

1. Update `Chatcopy::CLI` class in `cli.cr`:

   a. `initialize`: This method should instantiate the class with default values for `file_path` and `model`.

   b. `parse_arguments`: This method will handle parsing of command line arguments. It will create an instance of `OptionParser` and set up the options as follows:
   
      i. `-m MODEL`, `--model=MODEL`: This option allows the user to specify the OpenAI model to use. It sets the `model` instance variable to the provided value.

      ii. `-h`, `--help`: This option shows the help message. When this option is detected, it prints the help message and immediately exits the application.

   c. `run`: This method executes the application. It should first call `parse_arguments` to parse any command line arguments, and then execute the main logic of the application.

2. Update `Chatcopy::FileSearcher` class in `file_searcher.cr`:

   a. `initialize`: The constructor for this class should be updated to accept an additional argument, `model`, which represents the OpenAI model to use.

   b. `relevant_files`: This method should call the OpenAI Chat API using the specified `model`.

3. Update `Chatcopy::FileTree` class in `file_tree.cr`:

   The `FileTree` class doesn't need any changes based on the current master plan.

4. Update `Chatcopy::ClipboardManager` class in `clipboard_manager.cr`:

   The `ClipboardManager` class doesn't need any changes based on the current master plan.

Once these changes are made, the application should behave as expected: parse the command line arguments correctly, print the help options when the `-h` or `--help` command is given, use the specified OpenAI model if provided, and run the main application logic for other commands or the default case.

This master plan usually needs updating, for example I didn’t want anything but the cli.cr file to be modified so I followed up with:

I actually think only task #1 needs to be completed in order to fulfill what I'm looking for. Rewrite the master plan to only include that make sure the code will work for that

And it gave me a more detailed response.

  1. Implement Each Step in the Master Plan: Finally, for each step in the high-level plan, prompt the AI to generate the detailed implementation code.

So this step might be a bit controversial but it’s been VERY useful to me. I’ve been a follower of Avdi Grimm’s work for awhile and have read and purchased all of his writing on the topic of writing Ruby code well (I suggest you do the same). His coding style happens to translate well to Crystal Code as well (with exceptions), and it’s absolutely amazing that Chat GPT can consolidate so much of his coding style into the code it writes. Here is the prompt:

Ok, now using Avdi Grimm's code writing style as if he was a professional Crystal Lang programmer, write the code for task #1

I do this for every step that was generated by the high level plan and copy & paste the relevant bits that make sense.

Other Use Cases #

I’ve also found that chatcopy works well for copying knowledgebase content too if it’s hosted locally!

Challenges and Future Enhancements #

Occassionally it will grab too many files and you may need to run it again if your clipboard has too much in it. I’m still thinking about ways I can insert better limits.

Conclusion #

I’m also working on a semi-autonomous code assistant called guppi. You’ll need GPT-4 API access to maximize its potential.

Until then, chatcopy combined with “Hao prompting” is seriously powerful and I highly recommend giving it a try!