top of page
  • Writer's pictureRevanth Reddy Tondapu

Transform Your Codebase into LLM Prompts with Ease

Updated: Jun 19


Code2Prompt
Code2Prompt

Hey there! Today, Are you tired of manually copy-pasting multiple source files into prompts for your Large Language Model (LLM)? Are you looking for a tool that can automate this process while providing additional features like token counting and template customization? Look no further! Introducing a CLI tool that can convert your entire codebase into a single, well-structured LLM prompt.


Key Features

  1. Generate LLM Prompts from Any Codebase: This tool can handle codebases of any size, extracting all source files and combining them into a single prompt.

  2. Handlebars Templates: Customize your prompt generation using Handlebars templates. This allows you to format your prompt to fit any specific requirements you may have.

  3. Respects .gitignore: The tool will automatically exclude files and directories specified in your .gitignore file.

  4. File Filtering: Use glob patterns to filter and exclude specific files from your prompt.

  5. Token Counting: Display the token count of the generated prompt, which is essential for understanding the cost and feasibility of processing large prompts.

  6. Git Diff Output: Optionally include the Git diff output (staged files) in the generated prompt for more context.

  7. Clipboard and File Output: Automatically copy the generated prompt to your clipboard and save it to an output file.

  8. Exclusions by Name or Path: Exclude files and folders by name or path to further customize your prompt.

  9. Line Numbers: Add line numbers to source code blocks for easier reference.


How It Works

The tool traverses your codebase, aggregating all the source files into a single, well-formatted Markdown prompt. This eliminates the need for manually copy-pasting multiple files, saving you time and reducing the risk of errors. Additionally, it provides a token count to help you gauge the size and cost of processing the prompt.


Installation

The tool is written in Rust, so you'll need to have Rust installed on your machine. Here's how you can install it:

Install Rust:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
rustc --version 

Install the CLI Tool:

cargo install code2prompt
code2prompt --version

Usage

Once installed, you can run the tool on your codebase with a simple command. For example, if you have a local Git repository, you can generate a prompt like this:

code2prompt /path/to/your/project

This command will generate a prompt that includes the structure and content of your codebase and copy it to your clipboard. You can then paste it into your preferred text editor or directly into an LLM interface.

Customization with Handlebars Templates

The tool supports Handlebars templates, allowing you to customize how your prompt is generated. For example, you can use a template to add documentation comments to your functions or to find security vulnerabilities in your code. Here’s how you can use a template:

  • Clone the Repository:

  • Navigate to the Templates Directory:

cd code2prompt/templates
  • Run the Tool with a Template: code-to-prompt /path/to/your/project --template document-code.hbs

Examples

Documentation Template

This template adds documentation comments to your public functions:

code2prompt </path/to/your/project> --template <Download Templates Path>/document-code.hbs

Security Vulnerabilities Template

This template finds security vulnerabilities in your code:

code2prompt </path/to/your/project> --template <Download Templates Path>/find-security-vulnerabilities.hbs

Bug Fix Template

This template identifies and suggests fixes for potential bugs:

code2prompt </path/to/your/project> --template <Download Templates Path>/fix-bugs.hbs

Conclusion

This CLI tool is a powerful utility for anyone looking to streamline the process of generating LLM prompts from their codebase. Its ability to handle large codebases, respect .gitignore files, customize output with Handlebars templates, and provide useful features like token counting makes it an invaluable addition to your development toolkit.

Feel free to experiment with the different templates provided in the repository and see how they can fit into your workflow. Happy coding!

If you found this post helpful, please consider subscribing to our blog for more tips and tutorials. Share it with your network to help others discover this fantastic tool! Thank you for reading.

14 views0 comments

Comments


bottom of page