Skip to content

Commit

Permalink
Reduce diff even more
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-helmich committed Jul 29, 2024
1 parent 4576022 commit 1733e15
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
20 changes: 13 additions & 7 deletions src/Command/GenerateCommitMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@

class GenerateCommitMessage extends Command
{
private OpenAI\Client $client;

protected function configure()
{
$this->setName('generate-commit-message');
$this->addOption('from-uncommitted', 'u', InputOption::VALUE_NONE, 'Generate commit message from uncommitted changes');
}

protected function initialize(InputInterface $input, OutputInterface $output): void
{
$yourApiKey = getenv('OPENAI_API_KEY');
$this->client = OpenAI::client($yourApiKey);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$diffLines = [];
Expand All @@ -28,15 +36,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$diff = join("\n", $diffLines);

// When the diff is too long, simply truncate it to <1MB and hope for the best
if (strlen($diff) > 1048576) {
$diff = substr($diff, 0, 1048576);
// When the diff is too long, simply truncate it to <256K and hope for the best
// The theoretical message limit is 1MB, but there's also the maximum token limit to consider
if (strlen($diff) > 256 * 1024) {
$diff = substr($diff, 0, 256 * 1024);
}

$yourApiKey = getenv('OPENAI_API_KEY');
$client = OpenAI::client($yourApiKey);

$result = $client->chat()->create([
$result = $this->client->chat()->create([
'model' => 'gpt-4o-mini',
'messages' => [
['role' => 'system', 'content' => 'You will be provided a Git diff. Generate a commit message from it, following the conventional commit message format. Provide the output in plain text, without any additional formatting.'],
Expand Down
7 changes: 4 additions & 3 deletions src/Command/GenerateReleaseNotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$diff = join("\n", $diffLines);

// When the diff is too long, simply truncate it to <1MB and hope for the best
if (strlen($diff) > 1048576) {
$diff = substr($diff, 0, 1048576);
// When the diff is too long, simply truncate it to <256K and hope for the best
// The theoretical message limit is 1MB, but there's also the maximum token limit to consider
if (strlen($diff) > 256 * 1024) {
$diff = substr($diff, 0, 256 * 1024);
}

$yourApiKey = getenv('OPENAI_API_KEY');
Expand Down

0 comments on commit 1733e15

Please sign in to comment.