From 7d8eb88a6cd90abbc19f15eec8f103010cae08db Mon Sep 17 00:00:00 2001 From: abdul2020 Date: Thu, 2 Oct 2025 21:00:54 +0200 Subject: [PATCH 1/4] Feat: Integrate ChatGPT support for commit message generation - Add ChatGPT as a new LLM option configurable via COMMIT_LLM. - Update go.mod and go.sum to include necessary dependencies. - Implement `GenerateCommitMessage` in chatgpt package, utilizing OpenAI. --- src/chatgpt/chatgpt.go | 34 ++++++++++++++++++++++++++++++++++ src/main.go | 9 +++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/chatgpt/chatgpt.go diff --git a/src/chatgpt/chatgpt.go b/src/chatgpt/chatgpt.go new file mode 100644 index 0000000..3c11646 --- /dev/null +++ b/src/chatgpt/chatgpt.go @@ -0,0 +1,34 @@ +package chatgpt + +import ( + "context" + "fmt" + "log" + + openai "github.com/openai/openai-go/v3" + "github.com/openai/openai-go/v3/option" + + "github.com/dfanso/commit-msg/src/types" +) + +func GenerateCommitMessage(config *types.Config, changes string, apiKey string) (string, error) { + + client := openai.NewClient(option.WithAPIKey(apiKey)) + + prompt := fmt.Sprintf("%s\n\n%s", types.CommitPrompt, changes) + + resp, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{ + Messages: []openai.ChatCompletionMessageParamUnion{ + openai.UserMessage(prompt), + }, + Model: openai.ChatModelGPT4o, + }) + if err != nil { + return "", fmt.Errorf("OpenAI error: %w", err) + } + + // Extract and return the commit message + commitMsg := resp.Choices[0].Message.Content + log.Printf("Debug result: %s", commitMsg) + return commitMsg, nil +} \ No newline at end of file diff --git a/src/main.go b/src/main.go index 7504c75..a59c9a0 100644 --- a/src/main.go +++ b/src/main.go @@ -11,6 +11,7 @@ import ( "github.com/dfanso/commit-msg/src/gemini" "github.com/dfanso/commit-msg/src/grok" "github.com/dfanso/commit-msg/src/types" + "github.com/dfanso/commit-msg/src/chatgpt" ) // Normalize path to handle both forward and backslashes @@ -36,6 +37,11 @@ func main() { if apiKey == "" { log.Fatalf("GROK_API_KEY is not set") } + } else if os.Getenv("COMMIT_LLM") == "chatgpt" { + apiKey = os.Getenv("OPENAI_API_KEY") + if apiKey == "" { + log.Fatalf("OPENAI_API_KEY is not set") + } } else { log.Fatalf("Invalid COMMIT_LLM value: %s", os.Getenv("COMMIT_LLM")) } @@ -76,9 +82,12 @@ func main() { var commitMsg string if os.Getenv("COMMIT_LLM") == "google" { commitMsg, err = gemini.GenerateCommitMessage(config, changes, apiKey) + } else if os.Getenv("COMMIT_LLM") == "chatgpt" { + commitMsg, err = chatgpt.GenerateCommitMessage(config, changes, apiKey) } else { commitMsg, err = grok.GenerateCommitMessage(config, changes, apiKey) } + if err != nil { log.Fatalf("Failed to generate commit message: %v", err) } From 83a4426521e4e69888c63bcfd13da1b991598a59 Mon Sep 17 00:00:00 2001 From: abdul2020 Date: Thu, 2 Oct 2025 21:06:55 +0200 Subject: [PATCH 2/4] remove unnecessary debug statement --- src/chatgpt/chatgpt.go | 1 - 1 file changed, 1 deletion(-) diff --git a/src/chatgpt/chatgpt.go b/src/chatgpt/chatgpt.go index 3c11646..36305bb 100644 --- a/src/chatgpt/chatgpt.go +++ b/src/chatgpt/chatgpt.go @@ -29,6 +29,5 @@ func GenerateCommitMessage(config *types.Config, changes string, apiKey string) // Extract and return the commit message commitMsg := resp.Choices[0].Message.Content - log.Printf("Debug result: %s", commitMsg) return commitMsg, nil } \ No newline at end of file From b1ceef75ea796af5d4e0c42c22d76f40ae530e7d Mon Sep 17 00:00:00 2001 From: abdul2020 Date: Thu, 2 Oct 2025 21:07:21 +0200 Subject: [PATCH 3/4] remove unnecessary log dependancy --- src/chatgpt/chatgpt.go | 1 - 1 file changed, 1 deletion(-) diff --git a/src/chatgpt/chatgpt.go b/src/chatgpt/chatgpt.go index 36305bb..b3571e5 100644 --- a/src/chatgpt/chatgpt.go +++ b/src/chatgpt/chatgpt.go @@ -3,7 +3,6 @@ package chatgpt import ( "context" "fmt" - "log" openai "github.com/openai/openai-go/v3" "github.com/openai/openai-go/v3/option" From 356e05910204af96bcb6cb206c99930ab173e552 Mon Sep 17 00:00:00 2001 From: abdul2020 Date: Thu, 2 Oct 2025 21:15:55 +0200 Subject: [PATCH 4/4] Add multiple indirect dependencies to go.mod. --- go.mod | 5 +++++ go.sum | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/go.mod b/go.mod index e313679..1370bb0 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,11 @@ require ( github.com/google/uuid v1.6.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect github.com/googleapis/gax-go/v2 v2.14.1 // indirect + github.com/openai/openai-go/v3 v3.0.1 // indirect + github.com/tidwall/gjson v1.14.4 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect + github.com/tidwall/sjson v1.2.5 // indirect go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/auto/sdk v1.1.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0 // indirect diff --git a/go.sum b/go.sum index bb22aa5..b051a46 100644 --- a/go.sum +++ b/go.sum @@ -77,6 +77,8 @@ github.com/googleapis/gax-go/v2 v2.14.1 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrk github.com/googleapis/gax-go/v2 v2.14.1/go.mod h1:Hb/NubMaVM88SrNkvl8X/o8XWwDJEPqouaLeN2IUxoA= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/openai/openai-go/v3 v3.0.1 h1:cub/K1g5RJwYFqgvq81/ByLHnLJ+CsdSs1QSKaVA2WA= +github.com/openai/openai-go/v3 v3.0.1/go.mod h1:UOpNxkqC9OdNXNUfpNByKOtB4jAL0EssQXq5p8gO0Xs= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -85,6 +87,16 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= +github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= +github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=