Skip to content

Commit

Permalink
Merge pull request #4 from 5n00py/feature/openai-upgrade
Browse files Browse the repository at this point in the history
Feature/openai upgrade
  • Loading branch information
5n00py authored Dec 13, 2023
2 parents 097ee0d + aa17e0d commit 8063504
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 33 deletions.
49 changes: 36 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ possible options, run `gc-smart --help`.

## Prerequisites

- Python 3.6 or higher
- Python 3.7 or higher
- An OpenAI API key, see: [Where do I find my Secret API Key?](https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key)
- Git installed on your system
- Staged changes in the current repo to be commited
Expand All @@ -42,25 +42,48 @@ possible options, run `gc-smart --help`.

```bash
git clone https://github.com/5n00py/SmartCommit.git
cd SmartCommit
```

Alternatively just copy the files `gc-smart` and `gpt-commit-prompter` to
the preferred location.

2. Install the necessary Python libraries:
2. Setting up the Python Environment:

The necessary Python libraries have to be installed and isolated for this
project in a virtual environment called `venv` within the `python` folder.
The `gc-smart` script will automatically activate and deactivate this
virtual environment, so manual activation is not required.

Navigate to the `python` directory in the `SmartCommit` project:

```bash
pip install openai==0.28
cd SmartCommit/python
```

If you want to isolate the libraries for this project, create a virtual
environment, activate it and run the python script within the virtual
environment.
Create a virtual environment named `venv`:

```bash
python3 -m venv venv
```

Activate the virtual environment:

```bash
source venv/bin/activate
```

Install the required Python libraries:

```bash
pip3 install -r requirements.txt
```

Deactivate the virtual environment:

```bash
deactivate
```

Note: OpenAI released a new version of their API with significant changes.
The script will be upgraded in the future. For the moment pin the OpenAI
package to the older version 0.28.
Now the python environment is set up with all the openai dependences, and
the `gp-smart` script will automatically use this environment when
executed.

3. Set your OpenAI API key as an environment variable:

Expand Down
16 changes: 13 additions & 3 deletions gc-smart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
# Filename : gc-smart
# Author(s) : David Schmid (david.schmid@mailbox.org)
# Version : 0.3.2
# Version : 0.4.0
#
# ------------------------------ Description -----------------------------------
# Leverages gpt-commit-prompter to auto-generate commit messages for staged
Expand Down Expand Up @@ -61,6 +61,13 @@ check_commit_template
# Create a diff file of the staged changes
$GIT_CMD diff --cached > "$GIT_ROOT/staged_changes.diff"

# Path to the virtual environment for the gpt-commit-prompter script
VENV_PATH="$GCS_ROOT/python/venv"

# Activate the virtual environment
# shellcheck disable=SC1091
source "$VENV_PATH/bin/activate"

# Generate the commit message using the AI helper
generate_commit_message

Expand All @@ -73,10 +80,13 @@ fi
$GIT_CMD commit 2> "$GIT_ROOT/git_error.log" # Redirect error messages to a log file

# Check the result of the git commit operation
if [ $? -ne 0 ]; then # Check for non-zero exit status indicating failure
handle_git_commit_error # Handle any commit errors
if ! $GIT_CMD commit 2> "$GIT_ROOT/git_error.log"; then
handle_git_commit_error # Handle any commit errors
fi

# Deactivate the virtual environment
deactivate

# Cleanup: Delete files unless the --keep-files flag is set
if ! $KEEP_FILES; then
rm -f "$GIT_ROOT/git_error.log" # Remove the error log file
Expand Down
69 changes: 52 additions & 17 deletions python/gpt-commit-prompter
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
================================================================================
Author(s) : David Schmid (david.schmid@mailbox.org)
Version : 0.2.1
Version : 0.3.0
------------------------------ Description -----------------------------------
Utilizes OpenAI's GPT-3 model to auto-generate git commit messages from
Expand All @@ -15,7 +15,7 @@ employs GPT-3 to offer a summarized description and detailed bullet points.
------------------------------ Dependencies -----------------------------------
- OpenAI Python SDK
- Python 3.6 or higher
- Python 3.7 or higher
--------------------------------- Usage --------------------------------------
Execute the script with changes in string format or by indicating a .diff file:
Expand All @@ -32,6 +32,8 @@ import json
import os
import openai

from openai import OpenAI


def load_config(file_path="../config.json"):
# Determine the directory where this script is located.
Expand Down Expand Up @@ -89,6 +91,32 @@ def get_changes(args):
else:
return args.changes

# def generate_commit_message(changes, instruction, style="imperative"):
# config = load_config()
#
# # Ensure the provided style is in the configuration
# if style not in config["style"]:
# raise ValueError(f"Style '{style}' not found in configuration.")
#
# system_prompt = config["style"][style]["system_prompt"]
#
# # Constructing the prompt for the AI
# prompt = f"{system_prompt}\n\nChanges:\n{changes}\n\n"
# if instruction:
# prompt += f"Instruction: {instruction}\n"
#
# # Call the OpenAI API with the constructed prompt
# response = openai.Completion.create(
# engine="text-davinci-003", # or another model of your choice
# prompt=prompt,
# max_tokens=150, # Adjust as needed
# n=1, # Number of completions to generate
# stop=None # String where the API will stop generating further content
# )
#
# # Extracting the commit message from the response
# commit_message = response.choices[0].text.strip()
# return commit_message

def generate_commit_message(changes, instruction, style="imperative"):
config = load_config()
Expand All @@ -99,25 +127,32 @@ def generate_commit_message(changes, instruction, style="imperative"):

system_prompt = config["style"][style]["system_prompt"]

changes = "Write a git commit message for the following changes: \n\n" + changes

# Add the instruction to the AI messages
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": changes},
]

# Constructing the prompt for the AI
prompt = f"{system_prompt}\n\nChanges:\n{changes}\n\n"
if instruction:
messages.append({"role": "user", "content": instruction})

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages, # type: ignore
prompt += f"Instruction: {instruction}\n"

# Instantiate the OpenAI client
client = OpenAI()

# Call the OpenAI API with the constructed prompt
response = client.chat.completions.create(
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
],
model="gpt-3.5-turbo",
)

commit_message = response.choices[0].message["content"] # type: ignore
return commit_message
# Extracting the commit message from the response
commit_message = response.choices[0].message.content
if commit_message is not None:
commit_message = commit_message.strip()
else:
# Handle the case where commit_message is None
commit_message = "No commit message generated."

return commit_message

def main():
set_openai_key()
Expand Down
1 change: 1 addition & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openai==1.3.9

0 comments on commit 8063504

Please sign in to comment.