Skip to main content

Pre-commit hooks collection that utilizes ChatGPT and OpenAI platform to validate modifications made to the codebase.

Reason this release was yanked:

bug on basic execution

Project description

🤖 ChatGPT / OpenAI pre-commit-hooks

pre-commit PyPI - version PyPI - python version PyPI - downloads GitHub - ci

Pre-commit hooks collection that utilizes ChatGPT and OpenAI platform to validate changes made to the codebase.

🎣 Hooks

chatgpt-commit-message

Hook that uses OpenAI's ChatGPT API to generate a summary of changes made to a codebase and use it to populate the commit message automatically.

📥 Prerequisites setup

Hooks support OpenAI Platform and Azure OpenAI Service.

OpenAI Platform

OpenAI API Key is mandatory to run hooks and has to be setup via an environment variable.

  1. Create your API Key, and get your Organization ID from Organization settings

    OpenAI API Key

    OpenAI Organization ID

  2. Store values as an environment variables:

    • OPENAI_API_KEY for API Key
    • OPENAI_ORGANIZATION for Organization ID

    Example:

    export OPENAI_API_KEY="sk-xxxxxx"
    export OPENAI_ORGANIZATION="org-xxxxxx"
    

💡 HINT

How to setup env vars? see: Setting environment variables

Azure OpenAI Service

  1. Go to Azure Portal, and get API Key, Endpoint and Model deployment name

    Azure OpenAI API Key and Endpoint

    Azure OpenAI Model

  2. Store values as an environment variables:

    • OPENAI_API_TYPE put azure to specified OpenAI provider
    • OPENAI_API_KEY for API Key
    • OPENAI_API_BASE for Endpoint
    • OPENAI_MODEL_NAME for Model deployment name

    Example:

    export OPENAI_API_TYPE="azure"
    export OPENAI_API_KEY="sk-xxxxxx"
    export OPENAI_API_BASE="https://xxxxxx.openai.azure.com/"
    export OPENAI_MODEL_NAME="xxxxx-gpt-35-turbo"
    

💡 HINT

How to setup env vars? see: Setting environment variables

Setting environment variables

Linux/MacOS example:

export OPENAI_API_KEY="sk-xxxxxx"

Windows powershell example:

$env:OPENAI_API_KEY="sk-xxxxxx"

Windows cmd example:

set OPENAI_API_KEY=sk-xxxxxx

⚠️ NOTE

The above example stores the environment variable temporarily for the current session. To store it permanently, please follow Best Practices for API Key Safety

pre-commit setup

Before you start, ensure you have pre-commit installed in your repository. Below is just an essential quick start. Follow official pre-commit install documentation for advanced scenarios.

# install using pip
pip install pre-commit

# check if working - expected print with version like `pre-commit 3.2.2`
pre-commit --version

# setup the git repo for hooks
pre-commit install

# periodically run updates to your pre-commit config to make sure you always have the latest version of the hooks
pre-commit autoupdate

📦 Hooks setup

Remote repo reference (preferred)

Add to your .pre-commit-config.yaml

repos:
  - repo: https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks
    rev: vX.Y.Z  # Use the ref you want to point at, see ⚠️ NOTE below!
    hooks:
      - id: ... # follow 🎣 Hooks section to see available hooks IDs

Example:

repos:
  - repo: https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks
    rev: v0.1.1
    hooks:
      - id: chatgpt-commit-message

⚠️ NOTE

For the rev: always try to use the latest version. You can check the latest release under GitHub Releases

Local repo reference

  1. Install or add PyPI package to your project.

    • if you are using pip:

      pip install --upgrade chatgpt-pre-commit-hooks
      
    • or include it in a requirements.txt file in your project:

      chatgpt-pre-commit-hooks~=0.1.1
      

      and run:

      pip install -r requirements.txt
      
    • or, even better, in the dev section of your pyproject.toml file:

      [project.optional-dependencies]
      dev = ["chatgpt-pre-commit-hooks"]
      

      and run:

      pip install .[dev]
      
    • or, if you are using poetry as a package manager:

      poetry add chatgpt-pre-commit-hooks --group dev
      
  2. Add to your .pre-commit-config.yaml

    repos:
      - repo: local
        hooks:
          - id: ... # follow 🎣 Hooks section to see available hooks IDs
            name: Run chatgpt-pre-commit-hooks (<name>)
            entry: chatgpt-pre-commit-hooks.<id> # follow 🎣 Hooks section to see available hooks IDs
            language: system
    

    Example:

    repos:
      - repo: local
        hooks:
          - id: chatgpt-commit-message
            name: Run chatgpt-pre-commit-hooks (chatgpt-commit-message)
            entry: chatgpt-pre-commit-hooks.chatgpt-commit-message
            language: system
    

🛠️ Advanced configuration

Extra environment variables

In addition to the environment variables listed in the 📥 Prerequisites setup section, you can set several configurations using extra environment variables.

Name Type Default Description
OPENAI_MAX_TOKENS int 1024 What are tokens and how to count them?
OPENAI_MODEL string gpt-3.5-turbo Model endpoint compatibility - check /v1/chat/completions endpoint
OPENAI_PROXY string not set http/https client proxy

Arguments

Any environment variable can be overridden by hard-coded args in pre-commit-config.yaml, except OPENAI_API_KEY, OPENAI_ORGANIZATION.

Name Type Default Description
--env-prefix string not set Set prefix for environment variables allowing multiple configurations. Read more: --env-prefix
--openai-max-tokens int not set Overrides OPENAI_MAX_TOKENS
--openai-proxy string not set Overrides OPENAI_PROXY
--openai-model string not set Overrides OPENAI_MODEL
--openai-api-base string not set Overrides OPENAI_API_BASE
--openai-api-type string not set Overrides OPENAI_API_TYPE

Example:

repos:
  - repo: https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks
    rev: vX.Y.Z
    hooks:
      - id: ... # follow 🎣 Hooks section to see available hooks IDs
        args:
          - "--env-prefix"
          - "personal"
          - "--openai-max-tokens"
          - "512"
          - ...

--env-prefix

It's a special arg where you can mark prefixes for your environment variables. This allows you to set many configurations depending on the project, account, profile, etc., for example, personal, work. Fallback is a global environment variable if prefixed is not found.

For instance, if your prefix is personal, then the environment variable must be set PERSONAL__OPENAI_MAX_TOKENS, meaning the structure is <prefix>__<base_env_name> - two underscores __ between prefix and base_env_name.

Example:

export PERSONAL__OPENAI_API_KEY="sk-xxxxxx"
export WORK__OPENAI_API_KEY="sk-xxxxxx"

Variables precedence

  1. hard-coded args, e.g. --openai-max-tokens
  2. prefixed environment variable, e.g. PERSONAL__OPENAI_MAX_TOKENS
  3. global environment variable, e.g. OPENAI_MAX_TOKENS

💸 Payments

Project by default uses gpt-3.5-turbo model because of its lower cost. You have to pay for your own OpenAI API requests.

👥 Contributing

Contributions to the project are very welcome! Please follow Contributing Guide.

📄 License

This project is distributed under the terms of the MIT license.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

chatgpt_pre_commit_hooks-0.1.1-py3-none-any.whl (507.2 kB view details)

Uploaded Python 3

File details

Details for the file chatgpt_pre_commit_hooks-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for chatgpt_pre_commit_hooks-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ad4e1f0b4a043825e51fd4ed396444c155c8bdf6b89058a3fb6b473a1b81c47f
MD5 9d0e427d6fca1d3edbb67649707f8058
BLAKE2b-256 cbb0004adb15423b3ea196e5ed75332340ac24e315d2f759d329e67b5068415e

See more details on using hashes here.

Supported by

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