Skip to main content

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

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.

chatgpt-commit-message

📥 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 environment variables? see: Setting environment variables

Azure OpenAI Service

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

    Azure OpenAI API Key and Endpoint

    Azure OpenAI Model

    The latest supported api-version you can get from Azure OpenAI Service

  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_API_VERSION for api-version
    • OPENAI_MODEL for Model deployment name

    Example:

    export OPENAI_API_TYPE="azure"
    export OPENAI_API_KEY="xxxxxx"
    export OPENAI_API_BASE="https://xxxxxx.openai.azure.com/"
    export OPENAI_API_VERSION="2023-03-15-preview"
    export OPENAI_MODEL="xxxxx-gpt-35-turbo"
    

💡 HINT

How to setup environment variables? 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

Add to your .pre-commit-config.yaml top level default_install_hook_types section (for more information, follow Confining hooks to run at certain stages)

default_install_hook_types:
  - pre-commit # this is default hook type, equivalent to classic `pre-commit install` command
  - prepare-commit-msg # this type is not enabled by default, please enable it - equivalent to `pre-commit install --hook-type prepare-commit-msg` command
  - ... # rest of hook types what are you using, if any

next:

# setup the git repo for hooks
pre-commit install

# (optional) 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 repository reference (preferred)

Add to your .pre-commit-config.yaml

default_install_hook_types:
  - pre-commit
  - prepare-commit-msg
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: <id1> # follow 🎣 Hooks section to see available hooks IDs
      - id: <id2> # follow 🎣 Hooks section to see available hooks IDs
      - id: ...

Example:

default_install_hook_types:
  - pre-commit
  - prepare-commit-msg
repos:
  - repo: https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks
    rev: v0.1.3
    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 repository 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.3
      

      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

    default_install_hook_types:
      - pre-commit
      - prepare-commit-msg
    repos:
      - repo: local
        hooks:
          - id: <id> # follow 🎣 Hooks section to see available hooks IDs
            name: <name> # any name you'd like to set
            entry: chatgpt-pre-commit-hooks
            args:
              - "--hook"
              - "<id>" # follow 🎣 Hooks section to see available hooks IDs
              - "..." # rest of args what you'd like to set (optional)
            language: system
    

    Example:

    default_install_hook_types:
      - pre-commit
      - prepare-commit-msg
    repos:
    - repo: local
        hooks:
        - id: chatgpt-commit-message
          name: ChatGPT commit message
          entry: chatgpt-pre-commit-hooks
          args:
            - "--hook"
            - "chatgpt-commit-message"
            - "--description"
            - "--emoji"
          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 arguments 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:

default_install_hook_types:
  - pre-commit
  - prepare-commit-msg
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 argument 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 isn't 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 arguments, for example --openai-max-tokens
  2. prefixed environment variable, for example PERSONAL__OPENAI_MAX_TOKENS
  3. global environment variable, for example 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 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.3-py3-none-any.whl (13.6 kB view hashes)

Uploaded Python 3

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