8000 Feat/commit message by jsburckhardt · Pull Request #6 · jsburckhardt/gic · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feat/commit message #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"name": "Getting Started",
"name": "gic",
"image": "mcr.microsoft.com/devcontainers/go:dev-1.23",
"runArgs": [
"--gpus", "all"
],
"hostRequirements": {
"cpus": 4
"cpus": 4,
// "memory": "16gb"
},
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
Expand All @@ -13,11 +17,10 @@
"ghcr.io/stuartleeks/dev-container-features/shell-history:0": {},
"ghcr.io/devcontainers/features/azure-cli:1": {
"installBicep": true
}
},
// "ghcr.io/prulloac/devcontainer-features/ollama:1": {
// "pull": "phi3"
// "pull": "phi3.5"
// }

// "ghcr.io/stuartleeks/dev-container-features/azure-cli-persistence:0": {},
},
"waitFor": "onCreateCommand",
Expand Down
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,55 @@ A tool that helps developers generate git commit messages based on the `git diff
## AzureAD

Remember to assign the `Azure Cognitive Openai User` role to any user that is going to consume the resource.

In your `.gic` config you would add:

```yaml
connection_type: "azure_ad"
azure_endpoint: "https://<endpoint>.openai.azure.com/"
model_deployment_name: "<model name>"
```

## Ollama Locally in your devcontainer (or any machine)

Here is an example to run ollama in your devcontainer and pulling phi3.5 image.

```json
...
"features": {
"ghcr.io/prulloac/devcontainer-features/ollama:1": {
"pull": "phi3.5"
},
},
...
```

In your `.gic` config you would add:

```yaml
connection_type: "ollama"
azure_endpoint: "http://127.0.0.1:11434/"
model_deployment_name: "phi3.5"
```

>[!CAUTION]
>When choosing a model validate the instructions and generation matches what you expect. As an example, noticed phi3.5 didn't really generated a commit message with the requested instructions. More details in [here](#different-outputs-per-model).

## Different outputs per model

Instructions:

```yaml
llm_instructions: |

```

### phi3.5

```bash
```

### gpt-4o-mini

```bash
```
53 changes: 23 additions & 30 deletions cmd/root.go
ED4F
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,33 @@ func Execute(version, commit string) error {

setVersion()

rootCmd = &cobra.Command{
Use: "gic",
Short: "gic generates git commit messages based on staged changes.",
RunE: func(cmd *cobra.Command, args []string) error {
_ = cmd
_ = args
cfg, err := config.LoadConfig()
if err != nil {
return err
}

gitDiff, err := git.GetStagedChanges()
if err != nil {
return err
}

// retrieve the commit message
commitMessage, err := llm.GenerateCommitMessage(cfg, gitDiff)
if err != nil {
return err
}

_, _ = fmt.Println("Suggested Commit Message:", commitMessage)
return nil
},
rootCmd.RunE = executeCmd

return rootCmd.Execute()
}

func executeCmd(cmd *cobra.Command, args []string) error {
_ = cmd
_ = args
cfg, err := config.LoadConfig()
if err != nil {
return err
}

// Execute the root command
if err := rootCmd.Execute(); err != nil {
// log.Fatal(err)
gitDiff, err := git.GetStagedChanges()
if err != nil {
return err
// os.Exit(exitCodeFailure)
}
return nil

commitMessage, err := llm.GenerateCommitMessage(cfg, gitDiff)
if err != nil {
return err
}
if commitMessage == "### NO STAGED CHAGES ###" {
return nil
}

return git.Commit(commitMessage, cfg)
}

func setVersion() {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0
github.com/joho/godotenv v1.5.1
github.com/jsburckhardt/gic v1.0.0
github.com/ollama/ollama v0.3.8
github.com/openai/openai-go v0.1.0-alpha.9
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
Expand Down Expand Up @@ -40,7 +40,7 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand All @@ -29,8 +29,6 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jsburckhardt/gic v1.0.0 h1:0cV5NiWUJtjVNl3YEYT3ipBUKEDl/5/3x5MnLUIZs8w=
github.com/jsburckhardt/gic v1.0.0/go.mod h1:jM3MILCAIdDF4uftsIBDGEHFepFXKLrjYEFFlY96+Ts=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -41,6 +39,8 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/ollama/ollama v0.3.8 h1:UV7uaOlawJIdi96epXACBgEDZ0LyEi3MA9rquV0BVQ0=
github.com/ollama/ollama v0.3.8/go.mod h1:YrWoNkFnPOYsnDvsf/Ztb1wxU9/IXrNsQHqcxbY2r94=
github.com/openai/openai-go v0.1.0-alpha.9 h1:4Qh+InKO0PpEUw3YMVTybLa2jdETILxEnWQMvLa4lZ4=
github.com/openai/openai-go v0.1.0-alpha.9/go.mod h1:3SdE6BffOX9HPEQv8IL/fi3LYZ5TUpRYaqGQZbyk11A=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
Expand Down Expand Up @@ -97,8 +97,8 @@ go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ=
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
4 changes: 1 addition & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Config struct {
LLMInstructions string `mapstructure:"llm_instructions"`
ConnectionType string `mapstructure:"connection_type"`
AzureEndpoint string `mapstructure:"azure_endpoint"`
Commit bool `mapstructure:"commit"`
ShouldCommit bool `mapstructure:"should_commit"`
Tokens int `mapstructure:"tokens"`
}

Expand Down Expand Up @@ -49,9 +49,7 @@ func LoadConfig() (Config, error) {
// ValidateConfig validates the configuration and returns an
// error if any validation fails.
func validateConfig(cfg Config) error {
_, _ = fmt.Println("Validating configuration...")
if cfg.LLMInstructions == emptyString {
_, _ = fmt.Println("LLMInstructions not set in config. Using default instructions.")
cfg.LLMInstructions = "You are a helpful assistant, " +
"that helps generating commit messages based on git diffs."
}
Expand Down
17 changes: 17 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package git

import (
"fmt"
"gic/internal/config"
"os/exec"
)

Expand All @@ -16,3 +18,18 @@ func GetStagedChanges() (string, error) {
}
return string(out), nil
}

// Commit commits the staged changes with the generated message.
// it will only print the message unless commit is set to true.
func Commit(message string, cfg config.Config) error {
var err error
cmd := exec.Command("git", "commit", "-m", message)
if cfg.ShouldCommit {
if err = cmd.Run(); err != nil {
return err
}
} else {
_, err = fmt.Println("Suggested commit message:\n" + message)
}
return err
}
41 changes: 41 additions & 0 deletions internal/llm/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"

"github.com/ollama/ollama/api"
)

const emptyString = ""
Expand All @@ -41,11 +43,50 @@ func GenerateCommitMessage(cfg config.Config, diff string) (string, error) {
return GenerateCommitMessageAzureAD(cfg, diff)
case "openai":
return GenerateCommitMessageOpenAI(apikey, cfg, diff)
case "ollama":
return GenerateCommitMessageOllama(cfg, diff)
default:
return emptyString, fmt.Errorf("unsupported connection type: %s", cfg.ConnectionType)
}
}

// GenerateCommitMessageOllama generates a commit message using an LLM hosted in Ollama.
func GenerateCommitMessageOllama(cfg config.Config, diff string) (string, error) {
client, err := api.ClientFromEnvironment()
if err != nil {
return emptyString, err
}

messages := []api.Message{
{
Role: "system",
Content: cfg.LLMInstructions,
},
{
Role: "user",
Content: "git commit diff: " + diff,
},
}

ctx := context.Background()
req := &api.ChatRequest{
Model: cfg.ModelDeploymentName,
Messages: messages,
Stream: func(b bool) *bool { return &b }(false),
}

var commitMessage string
respFunc := func(resp api.ChatResponse) error {
commitMessage = resp.Message.Content
return nil
}
err = client.Chat(ctx, req, respFunc)
if err != nil {
return emptyString, err
}
return commitMessage, nil
}

// GenerateCommitMessageAzure generates a commit message using the Azure Language Learning Model.
// It takes an API key, a config.Config object, and a string representing the diff as input.
func GenerateCommitMessageAzure(apikey string, cfg config.Config, diff string) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

var (
version = "edge"
version = "local"
commit = "n/a"
)

Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ lint:
golangci-lint run ./...

fmt:
gofmt -l -w -s
gofmt -l -s .
Loading
0