Skip to main content

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 tiktoken to count tokens, ensuring compatibility with various LLM models.
  • Command-Line Interface (CLI): User-friendly CLI built with Click for 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 (full or slim). Defaults to slim.
  • --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 to None to skip token counting.
  • --verbose: Logging level (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL). Defaults to WARNING.

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

  1. Clone the Repository:

    git clone https://gitlab.com/notorious-community/slimcontext.git
    cd slimcontext
    
  2. Install Dependencies:

    Ensure you have poetry installed. 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:

  1. Fork the Repository:

    Click the "Fork" button at the top right of the repository page.

  2. Clone Your Fork:

    git clone https://gitlab.com/your-username/slimcontext.git
    cd slimcontext
    
  3. Create a New Branch:

    git checkout -b feature/your-feature-name
    
  4. Make Your Changes:

    Implement your feature or bug fix.

  5. Commit Your Changes:

    git commit -m "Title of your changes" -m "Describe **Why** you made this change."
    
  6. Push to Your Fork:

    git push origin feature/your-feature-name
    
  7. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slimcontext-0.3.0.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slimcontext-0.3.0-py3-none-any.whl (26.9 kB view details)

Uploaded Python 3

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

Hashes for slimcontext-0.3.0.tar.gz
Algorithm Hash digest
SHA256 0c1a2b316d4d21e6ce38b1d3aafc60b4ca4b4727497abf5a8676303081f55c6c
MD5 6194a92fde37a3c386c6ca5cc972d0e5
BLAKE2b-256 857fe8139852e9dc92b450322ad2862f3a29ba0d64c7f77b2579c7831780c09e

See more details on using hashes here.

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

Hashes for slimcontext-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 565d5f72a13e0ddcbe1a740aa46f5ecba6d0894f77ec3a31d61130f32c9594d0
MD5 9f584e81c9d38a2e08cd485d3c1fcc1a
BLAKE2b-256 093428836fd7021c039dd727965e03a11b11fae1c35b7c2bc0b439f6843a10e0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page