A tool to turn a git repository into context for LLMs.
Project description
slimcontext
slimcontext is a Python package that transforms a Git repository into contextual information suitable for Large Language Models (LLMs). It extracts comprehensive information from Python source files, generates structured context for LLMs, and provides utilities for token counting and logging.
Features
- Context Extraction: Extracts either full or slim textual contexts based on the repository content, tailored for effective LLM interactions.
- Token Counting: Integrates with
tiktokento count tokens, ensuring compatibility with various LLM models. - Command-Line Interface (CLI): User-friendly CLI built with
Clickfor easy interaction and automation. - Git Integration: Manages and interacts with Git repositories, handling file extraction and repository structure efficiently.
Installation
You can install the package via PyPI:
pip install slimcontext
Or install directly from GitLab:
pip install git+https://gitlab.com/notorious-community/slimcontext.git
Usage
Command-Line Interface (CLI)
The package provides a CLI tool for easy interaction. Below are the available commands and options.
Basic Usage
slimcontext main --path /path/to/your/git/repo
Options
--path: Path to the Git repository (required).--context-level: Level of context generation (fullorslim). Defaults toslim.--output: Path to the output file. If not specified, outputs to stdout.--token-model: Specify the model for token counting (e.g.,gpt-4). Set toNoneto skip token counting.--verbose: Logging level (e.g.,DEBUG,INFO,WARNING,ERROR,CRITICAL). Defaults toWARNING.
Examples
slimcontext main \
--path /path/to/your/git/repo \
--context-level full \
--output /path/to/output/context.txt \
--token-model gpt-4 \
--verbose 0
or
slimcontext main \
-p /path/to/your/git/repo \
-c full \
-o /path/to/output/context.txt \
-t gpt-4 \
-v
Python API
from slimcontext import (
ProjectParser,
initialize_git_repo,
generate_context,
count_tokens,
write_output,
setup_logger
)
from pathlib import Path
# Initialize logger
logger = setup_logger(__name__)
# Initialize Git repository
repo_path = Path('/path/to/your/git/repo')
git_repo = initialize_git_repo(repo_dir=repo_path)
files = git_repo.get_abolute_file_paths()
# Initialize Python parser
parser = ProjectParser(root_dir=git_repo.get_git_root())
# Generate context
context = generate_context(parser=parser, files=files, context_level='slim')
# Count tokens
token_count = count_tokens(context=context)
print(f"Total tokens: {token_count}")
# Write output
output_path = Path('/path/to/output/context.txt')
write_output(context=context, output_path=output_path)
Development
Setting Up the Development Environment
-
Clone the Repository:
git clone https://gitlab.com/notorious-community/slimcontext.git cd slimcontext
-
Install Dependencies:
Ensure you have
poetryinstalled. Then run:poetry install
Running Tests
The package includes a comprehensive test suite using pytest.
poetry run pytest
Linting and Formatting
Ensure code quality and consistency with ruff.
poetry run ruff check .
Running Nox Sessions
Automate development tasks across multiple Python environments using nox.
nox -s tests
Project Structure
slimcontext/
├── slimcontext/
│ ├── main.py
│ ├── managers/ # Houses various managers for languages.
│ ├── parsers/ # Houses the code to parse and extract key context by language.
│ └── utils/ # Various utilities used across code base.
├── tests/
├── gitlab-ci.yml
├── CHANGELOG
├── LICENSE
├── noxfile.py
├── pyproject.toml
└── README.md
Contributing
Contributions are welcome! Whether it's reporting bugs, suggesting features, or submitting pull requests, your help is appreciated. Please follow these steps to contribute:
-
Fork the Repository:
Click the "Fork" button at the top right of the repository page.
-
Clone Your Fork:
git clone https://gitlab.com/your-username/slimcontext.git cd slimcontext
-
Create a New Branch:
git checkout -b feature/your-feature-name
-
Make Your Changes:
Implement your feature or bug fix.
-
Commit Your Changes:
git commit -m "Title of your changes" -m "Describe **Why** you made this change."
-
Push to Your Fork:
git push origin feature/your-feature-name
-
Create a Merge Request:
Go to the original repository and create a merge request from your fork.
Please ensure all tests pass and adhere to the project's coding standards before submitting your merge request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Copyright (c) 2024 Neil Schneider
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file slimcontext-0.3.0.tar.gz.
File metadata
- Download URL: slimcontext-0.3.0.tar.gz
- Upload date:
- Size: 19.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: poetry/2.0.0 CPython/3.12.8 Linux/5.15.154+
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c1a2b316d4d21e6ce38b1d3aafc60b4ca4b4727497abf5a8676303081f55c6c
|
|
| MD5 |
6194a92fde37a3c386c6ca5cc972d0e5
|
|
| BLAKE2b-256 |
857fe8139852e9dc92b450322ad2862f3a29ba0d64c7f77b2579c7831780c09e
|
File details
Details for the file slimcontext-0.3.0-py3-none-any.whl.
File metadata
- Download URL: slimcontext-0.3.0-py3-none-any.whl
- Upload date:
- Size: 26.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: poetry/2.0.0 CPython/3.12.8 Linux/5.15.154+
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
565d5f72a13e0ddcbe1a740aa46f5ecba6d0894f77ec3a31d61130f32c9594d0
|
|
| MD5 |
9f584e81c9d38a2e08cd485d3c1fcc1a
|
|
| BLAKE2b-256 |
093428836fd7021c039dd727965e03a11b11fae1c35b7c2bc0b439f6843a10e0
|