Pre-commit hooks collection that utilizes ChatGPT and OpenAI platform to validate changes made to the codebase.
- π£ Hooks
- π₯ Prerequisites setup
- π¦ Hooks setup
- π οΈ Advanced configuration
- πΈ Payments
- π₯ Contributing
- π License
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.
- βοΈ Read about hook's specific configuration.
Hooks support OpenAI Platform and Azure OpenAI Service.
OpenAI API Key is mandatory to run hooks and has to be setup via an environment variable.
-
Create your API Key, and get your Organization ID from Organization settings
-
Store values as an environment variables:
OPENAI_API_KEY
for API KeyOPENAI_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
-
Go to Azure Portal, and get
API Key
,Endpoint
,Model deployment name
, andapi-version
.The latest supported
api-version
you can get from Azure OpenAI Service -
Store values as an environment variables:
OPENAI_API_TYPE
putazure
to specified OpenAI providerOPENAI_API_KEY
for API KeyOPENAI_API_BASE
for EndpointOPENAI_API_VERSION
forapi-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
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
β οΈ NOTEThe above example stores the environment variable temporarily for the current session. To store it permanently, please follow Best Practices for API Key Safety
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
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
β οΈ NOTEFor the
rev:
always try to use the latest version. You can check the latest release under GitHub Releases
-
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 yourpyproject.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
-
-
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
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 |
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"
- ...
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"
- hard-coded arguments, for example
--openai-max-tokens
- prefixed environment variable, for example
PERSONAL__OPENAI_MAX_TOKENS
- global environment variable, for example
OPENAI_MAX_TOKENS
Project by default uses gpt-3.5-turbo
model because of its lower cost. You have to pay for your own OpenAI API requests.
Contributions to the project are welcome! Please follow Contributing Guide.
This project is distributed under the terms of the MIT license.