crates/code2prompt-python/python-sdk/README.md
Python bindings for code2prompt - A tool to generate LLM prompts from codebases.
git clone https://github.com/mufeedvh/code2prompt.git
cd code2prompt
python3 -m venv .venv
source .venv/bin/activate
pip install maturin pytest
cd code2prompt/ # root repo directory
maturin develop -r
Try out the example script:
python examples/basic_usage.py
from code2prompt import CodePrompt
# Create a new CodePrompt instance
prompt = CodePrompt(
path="./my_project",
include_patterns=["*.py", "*.rs"], # Optional: Only include Python and Rust files
exclude_patterns=["**/tests/*"], # Optional: Exclude test files
line_numbers=True, # Optional: Add line numbers to code
)
# Generate a prompt
result = prompt.generate(
template=None, # Optional: Custom Handlebars template
encoding="cl100k" # Optional: Token encoding (for token counting)
)
# Access the generated prompt and metadata
print(f"Generated prompt: {result['prompt']}")
print(f"Token count: {result['token_count']}")
print(f"Model info: {result['model_info']}")
# Git operations
git_diff = prompt.get_git_diff()
branch_diff = prompt.get_git_diff_between_branches("main", "feature")
git_log = prompt.get_git_log("main", "feature")
CodePromptMain class for generating prompts from code.
CodePrompt(
path: str,
include_patterns: List[str] = [],
exclude_patterns: List[str] = [],
include_priority: bool = False,
line_numbers: bool = False,
relative_paths: bool = False,
exclude_from_tree: bool = False,
no_codeblock: bool = False,
follow_symlinks: bool = False
)
path: Path to the codebase directoryinclude_patterns: List of glob patterns for files to includeexclude_patterns: List of glob patterns for files to excludeinclude_priority: Give priority to include patterns in case of conflictsline_numbers: Add line numbers to code blocksrelative_paths: Use relative paths instead of absoluteexclude_from_tree: Exclude files from source tree based on patternsno_codeblock: Don't wrap code in markdown code blocksfollow_symlinks: Follow symbolic links when traversing directoriesgenerate(template: Optional[str] = None, encoding: Optional[str] = None) -> DictGenerate a prompt from the codebase.
template: Optional custom Handlebars templateencoding: Optional token encoding (cl100k, p50k, p50k_edit, r50k, gpt2)Returns a dictionary containing:
prompt: The generated promptdirectory: The processed directory pathtoken_count: Number of tokens (if encoding was specified)model_info: Information about the model (if encoding was specified)get_git_diff() -> strGet git diff for the repository.
get_git_diff_between_branches(branch1: str, branch2: str) -> strGet git diff between two branches.
get_git_log(branch1: str, branch2: str) -> strGet git log between two branches.
MIT License - see LICENSE file for details.