Introducing Code2Prompt
Code2Prompt is an innovative command-line tool designed to transform an entire codebase into a single Large Language Model (LLM) prompt. Its primary purpose is to streamline and automate the process of creating well-structured prompts from potentially large and complex code repositories. By incorporating features such as source tree mapping, prompt templating, and token counting, Code2Prompt becomes an ideal tool for developers looking to integrate their code with AI models like GPT or Claude.
Key Features
Code2Prompt stands out with several remarkable features:
- Full Directory Analysis: Run the tool on any directory to produce a markdown prompt complete with source tree details and full code listing.
- Customizable Prompts: Utilize Handlebars templates to tailor prompts. This flexibility allows developers to meet diverse needs with ease.
- Compatibility with .gitignore: It respects files and directories set to be ignored by version control.
- File Filtering: Use of glob patterns allows for specific file inclusion or exclusion, enhancing customizable analysis.
- Token Counting: The tool provides a token count of the generated prompt using specified encodings.
- Git Integration: Option to include Git diffs for staged files in resulting prompts.
- Clipboard and File Output: Automatically copy generated prompts to clipboard or save them as files.
- Exclusion by Name/Path: Exclude specified files and folders to refine the output.
- Line Numbers in Code: Add line numbers to increase code readability in the prompts.
Installation Options
Code2Prompt can be installed easily using various methods:
- Binary Releases: Download from the releases on its GitHub repository.
- Source Build: Clone the repository and build it using Cargo.
- Using Cargo: Install directly from crates.io or GitHub.
- AUR for Arch Linux: Install via any AUR helper.
- Nix Package Manager: For Nix OS users, install with or without flakes.
Practical Usage
Utilizing Code2Prompt begins by pointing it to the codebase of interest using the command:
code2prompt path/to/codebase
Developers can further customize their experience by:
- Using custom templates.
- Applying file inclusion/exclusion patterns.
- Selecting specific tokenizers for prompt generation.
- Output options including saving to files or displaying as JSON.
Built-in Templates
Code2Prompt comes equipped with diverse templates for common tasks like documenting code, finding bugs, enhancing performance, and writing GitHub pull requests and commit messages. These templates are easily accessible and can be used to generate tailored prompts based on specific coding needs.
User Defined Variables
A unique feature of Code2Prompt is support for user-defined variables in templates. As templates are processed, users can provide additional information, making the generated prompts highly adaptable to varying contexts.
Tokenizer Support
From token counting to encoding, Code2Prompt leverages tiktoken-rs
for compatibility with several OpenAI models, providing flexibility depending on the project's specific requirements.
Contribution and Support
The project is open for contribution where developers can suggest features, report bugs, fix issues, or help with documentation. It's licensed under the MIT License, encouraging widespread use and adaptation. Moreover, supporting the authors and spreading the word about Code2Prompt is greatly appreciated by the community.
In summary, Code2Prompt bridges the gap between extensive codebases and LLMs by providing a comprehensive toolset for generating insightful and contextually aware prompts from any code repository. It's a must-have tool for developers seeking to integrate AI capabilities into their workflows efficiently.