Skip to content

Commit 2661b9e

Browse files
authored
Merge pull request #5 from abdulessa/feature/add-openai-chatgpt-support
Feat: Integrate ChatGPT support for commit message generation
2 parents 5b7f78d + f09db31 commit 2661b9e

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ require (
2828
github.com/google/uuid v1.6.0 // indirect
2929
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
3030
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
31+
github.com/openai/openai-go/v3 v3.0.1 // indirect
32+
github.com/tidwall/gjson v1.14.4 // indirect
33+
github.com/tidwall/match v1.1.1 // indirect
34+
github.com/tidwall/pretty v1.2.1 // indirect
35+
github.com/tidwall/sjson v1.2.5 // indirect
36+
go.opencensus.io v0.24.0 // indirect
3137
github.com/gookit/color v1.5.4 // indirect
3238
github.com/lithammer/fuzzysearch v1.1.8 // indirect
3339
github.com/mattn/go-runewidth v0.0.16 // indirect

go.sum

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.4 h1:XYIDZApgAnrN1c855gT
5555
github.com/googleapis/enterprise-certificate-proxy v0.3.4/go.mod h1:YKe7cfqYXjKGpGvmSg28/fFvhNzinZQm8DGnaburhGA=
5656
github.com/googleapis/gax-go/v2 v2.14.1 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrkurSS/Q=
5757
github.com/googleapis/gax-go/v2 v2.14.1/go.mod h1:Hb/NubMaVM88SrNkvl8X/o8XWwDJEPqouaLeN2IUxoA=
58+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
59+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
60+
github.com/openai/openai-go/v3 v3.0.1 h1:cub/K1g5RJwYFqgvq81/ByLHnLJ+CsdSs1QSKaVA2WA=
61+
github.com/openai/openai-go/v3 v3.0.1/go.mod h1:UOpNxkqC9OdNXNUfpNByKOtB4jAL0EssQXq5p8gO0Xs=
5862
github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ=
5963
github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo=
6064
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
@@ -89,6 +93,23 @@ github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
8993
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
9094
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
9195
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
96+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
97+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
98+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
99+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
100+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
101+
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
102+
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
103+
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
104+
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
105+
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
106+
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
107+
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
108+
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
109+
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
110+
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
111+
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
112+
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
92113
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
93114
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
94115
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

src/chatgpt/chatgpt.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package chatgpt
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
openai "github.com/openai/openai-go/v3"
8+
"github.com/openai/openai-go/v3/option"
9+
10+
"github.com/dfanso/commit-msg/src/types"
11+
)
12+
13+
func GenerateCommitMessage(config *types.Config, changes string, apiKey string) (string, error) {
14+
15+
client := openai.NewClient(option.WithAPIKey(apiKey))
16+
17+
prompt := fmt.Sprintf("%s\n\n%s", types.CommitPrompt, changes)
18+
19+
resp, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
20+
Messages: []openai.ChatCompletionMessageParamUnion{
21+
openai.UserMessage(prompt),
22+
},
23+
Model: openai.ChatModelGPT4o,
24+
})
25+
if err != nil {
26+
return "", fmt.Errorf("OpenAI error: %w", err)
27+
}
28+
29+
// Extract and return the commit message
30+
commitMsg := resp.Choices[0].Message.Content
31+
return commitMsg, nil
32+
}

src/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/dfanso/commit-msg/src/gemini"
1212
"github.com/dfanso/commit-msg/src/grok"
1313
"github.com/dfanso/commit-msg/src/types"
14+
"github.com/dfanso/commit-msg/src/chatgpt"
1415
"github.com/pterm/pterm"
1516
)
1617

@@ -37,6 +38,11 @@ func main() {
3738
if apiKey == "" {
3839
log.Fatalf("GROK_API_KEY is not set")
3940
}
41+
} else if os.Getenv("COMMIT_LLM") == "chatgpt" {
42+
apiKey = os.Getenv("OPENAI_API_KEY")
43+
if apiKey == "" {
44+
log.Fatalf("OPENAI_API_KEY is not set")
45+
}
4046
} else {
4147
log.Fatalf("Invalid COMMIT_LLM value: %s", os.Getenv("COMMIT_LLM"))
4248
}
@@ -82,9 +88,12 @@ func main() {
8288
var commitMsg string
8389
if os.Getenv("COMMIT_LLM") == "google" {
8490
commitMsg, err = gemini.GenerateCommitMessage(config, changes, apiKey)
91+
} else if os.Getenv("COMMIT_LLM") == "chatgpt" {
92+
commitMsg, err = chatgpt.GenerateCommitMessage(config, changes, apiKey)
8593
} else {
8694
commitMsg, err = grok.GenerateCommitMessage(config, changes, apiKey)
8795
}
96+
8897

8998
if err != nil {
9099
log.Fatalf("Failed to generate commit message: %v", err)

0 commit comments

Comments
 (0)