diff --git a/go.mod b/go.mod index 987bfd7..7bba9cb 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,12 @@ 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 github.com/gookit/color v1.5.4 // indirect github.com/lithammer/fuzzysearch v1.1.8 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect diff --git a/go.sum b/go.sum index 4ea1a46..c4435ed 100644 --- a/go.sum +++ b/go.sum @@ -55,6 +55,10 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.4 h1:XYIDZApgAnrN1c855gT github.com/googleapis/enterprise-certificate-proxy v0.3.4/go.mod h1:YKe7cfqYXjKGpGvmSg28/fFvhNzinZQm8DGnaburhGA= github.com/googleapis/gax-go/v2 v2.14.1 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrkurSS/Q= 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/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ= github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo= 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 github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ= github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +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= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/src/chatgpt/chatgpt.go b/src/chatgpt/chatgpt.go new file mode 100644 index 0000000..b3571e5 --- /dev/null +++ b/src/chatgpt/chatgpt.go @@ -0,0 +1,32 @@ +package chatgpt + +import ( + "context" + "fmt" + + 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 + return commitMsg, nil +} \ No newline at end of file diff --git a/src/main.go b/src/main.go index 23e8a23..65fac9c 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" "github.com/pterm/pterm" ) @@ -37,6 +38,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")) } @@ -82,9 +88,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)