Command line tool to download code or functions to use as an AI prompt

Recently, I had an issue with two functions in my codebase that processed crosstabs and statistics to go along with them. One handled a parameter, stats = ['mean'] correctly, while the other failed.

I wanted to get the AI to compare these functions and suggest why one was working but the other one wasn’t.

Enter: codegrab

Codegrab

With the help of ChatGPT I wrote a command line script where the user points to a github repository and the script pulls the python module, or function, from the repo.

Codegrab also accepts data piped into it, in which case it adds the piped in code to the code being fetched. So, to compare the two functions and have an LLM assist me, I can write

codegrab module.location:function --repo [private repo] \
 | codegrab other_module.location:function --repo [private repo] \
 | llm "The first function does not work, the second one does. The first one fails when I supply stats=['mean'] whereas the second one successfully includes stats. Can you see what the difference could be? Can you suggest changes to the first implementation so that it accepts stats in the same way the second one does?"

It worked, the output pointed to why one function was working and the other wasn’t.

Writing a test for codegrab using codegrab

Here’s another use case, grabbing the code for this new tool, codegrab itself, and having AI create tests for it.

codegrab codegrab --repo http://github.com/geirfreysson/codegrab \
| uvx llm "Write tests for me for this module using pytest" -m 4o

The tests the AI came up with mostly worked, but it took me 90 minutes or so to fix them so they all passed successfully. I’ve never used mock in pytest this way before to test remote requests, so it was a learning experience. Now, codegrab has AI generated tests, courtesy of codegrab.

For installation and usage instructions, check out the repo for codegrab.

You can also try repomix if you don’t dislike the npm ecosystem as much as me, or files-to-prompt if you are happy to clone/download the repository before getting the code.