Skip to content

Commit

Permalink
Added support for choosing the OpenAI model
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 committed May 4, 2023
1 parent 568bc24 commit ab51fc1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `ai-commit-message` will be documented in this file

## v0.3.1 - 2023-05-04

- Added support for choosing the OpenAI model using the `OPENAI_MODEL` environment variable. Users can now choose between 'gpt-3.5-turbo', 'gpt-4', and 'gpt-4-32k'.

## v0.3.0 - 2023-05-04

- Refactored the code for better organization and readability
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ To use this action in your GitHub repository, follow these steps:

1. Add a secret named `OPENAI_API_KEY` in your GitHub repository containing your OpenAI API key.

2. Add the following workflow file to your repository in the .github/workflows directory, and name it ai_commit_message_generator.yml:
2. Add the following workflow file to your repository in the .github/workflows directory, and name it ai_commit_message.yml:

```bash
name: AI Commit Message Generator
Expand All @@ -40,14 +40,26 @@ jobs:
uses: salehhashemi1992/ai-commit-message@v0.3.0
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_MODEL: gpt-3.5-turbo # Optional, you can use 'gpt-4' or 'gpt-4-32k' as well.

```

Now, whenever you push a commit with the title [ai], this action will automatically generate a commit title and description using AI and update the commit message accordingly.

### Configuration
You can customize the behavior of this action using the following environment variables:

#### OPENAI_API_KEY
Required. The API key for accessing the OpenAI API.

#### OPENAI_MODEL
Optional. The OpenAI model you want to use.

Supported values are 'gpt-3.5-turbo', 'gpt-4', and 'gpt-4-32k'. Defaults to 'gpt-3.5-turbo'.

### Known Bugs
If you push a new commit before the title of the last commit has been updated, it will result in an issue where only the last commit title gets updated. To avoid this problem, please make sure to wait for the previous commit's title to be updated before pushing a new commit.


### Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand Down
23 changes: 19 additions & 4 deletions main.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ function main(): void
$committerEmail = exec("git log -1 --pretty=%ce $commitSha");

if ($commitTitle === '[ai]') {
$model = getenv('OPENAI_MODEL') ?: 'gpt-3.5-turbo'; // Default to gpt-3.5-turbo if no environment variable is set

if (!in_array($model, ['gpt-4', 'gpt-4-32k', 'gpt-3.5-turbo'])) {
echo "::error::Invalid model specified. Please use either gpt-3.5-turbo', 'gpt-4' or 'gpt-4-32k'." .
PHP_EOL;
exit(1);
}

list($newTitle, $newDescription) = fetchAiGeneratedTitleAndDescription(
getCommitChanges($commitSha),
getenv('OPENAI_API_KEY')
getenv('OPENAI_API_KEY'),
$model,
);

updateLastCommitMessage($newTitle, $newDescription, $committerEmail, $committerName);
Expand All @@ -29,15 +38,15 @@ function main(): void

main();

function fetchAiGeneratedTitleAndDescription(string $commitChanges, string $openAiApiKey): array
function fetchAiGeneratedTitleAndDescription(string $commitChanges, string $openAiApiKey, string $model): array
{
$prompt = generatePrompt($commitChanges);

$input_data = [
"temperature" => 0.7,
"max_tokens" => 300,
"frequency_penalty" => 0,
'model' => 'gpt-3.5-turbo',
'model' => $model,
"messages" => [
[
'role' => 'user',
Expand Down Expand Up @@ -134,7 +143,13 @@ function getCommitChanges(string $commitSha): string
exec($command, $output, $return_var);

if ($return_var == 0) {
$output = array_slice($output, 0, 400);
$length = getenv('OPENAI_MODEL') ? match (getenv('OPENAI_MODEL')) {
'gpt-3.5-turbo' => 400,
'gpt-4' => 800,
'gpt-4-32k' => 3200,
} : 400;

$output = array_slice($output, 0, $length);
return implode("\n", $output);
} else {
echo "Error: Could not run git diff. Return code: " . $return_var;
Expand Down

0 comments on commit ab51fc1

Please sign in to comment.