Skip to content

Commit

Permalink
Update ollama request to include diff string and fix json decoding error
Browse files Browse the repository at this point in the history
  • Loading branch information
zaidfadhil committed May 28, 2024
1 parent 3cf7673 commit ab4177e
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 14 deletions.
54 changes: 54 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package config

import (
"encoding/json"
"fmt"
"runtime"
)

var configKey = "kemit-config"

type Config struct {
OllamaHost string `json:"ollama_host"`
OllamaModel string `json:"ollama_model"`
}

func (cfg *Config) SaveConfig() error {
jsonData, err := json.Marshal(cfg)
if err != nil {
return err
}

switch runtime.GOOS {
case "darwin":
return saveConfigMacOS(string(jsonData), configKey)
case "linux":
return saveConfigLinux(string(jsonData), configKey)
default:
return fmt.Errorf("unsupported platform: %s\n", runtime.GOOS)
}
}

func (cfg *Config) LoadConfig() error {
var output []byte
var err error
switch runtime.GOOS {
case "darwin":
output, err = loadConfigMacOS(configKey)
case "linux":
output, err = loadConfigLinux(configKey)
default:
return fmt.Errorf("unsupported platform: %s", runtime.GOOS)
}

if err != nil {
return err
}

err = json.Unmarshal(output, cfg)
if err != nil {
return err
}

return nil
}
23 changes: 23 additions & 0 deletions config/keychain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package config

import "os/exec"

func saveConfigMacOS(value, key string) error {
cmd := exec.Command("security", "add-generic-password", "-a", key, "-s", key, "-w", value, "-U")
return cmd.Run()
}

func saveConfigLinux(value, key string) error {
cmd := exec.Command("secret-tool", "store", "--label=kemit", key, value)
return cmd.Run()
}

func loadConfigMacOS(key string) ([]byte, error) {
cmd := exec.Command("security", "find-generic-password", "-a", key, "-s", key, "-w")
return cmd.Output()
}

func loadConfigLinux(key string) ([]byte, error) {
cmd := exec.Command("secret-tool", "lookup", key)
return cmd.Output()
}
2 changes: 1 addition & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package engine

type Engine interface {
GetCommit() (string, error)
GetCommit(diff string) (string, error)
}
22 changes: 13 additions & 9 deletions engine/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)

Expand All @@ -11,26 +12,24 @@ var _ Engine = (*ollamaEngine)(nil)
type ollamaEngine struct {
Host string
Model string
Diff string
}

func NewOllama(diff string) *ollamaEngine {
func NewOllama(host, model string) *ollamaEngine {
return &ollamaEngine{
Host: "http://192.168.0.107:11435/api/generate",
Model: "llama3",
Diff: diff,
Host: host + "/api/generate",
Model: model,
}
}

func (ollama *ollamaEngine) GetCommit() (string, error) {
return ollama.request()
func (ollama *ollamaEngine) GetCommit(diff string) (string, error) {
return ollama.request(diff)
}

func (ollama *ollamaEngine) request() (string, error) {
func (ollama *ollamaEngine) request(diff string) (string, error) {

payload := map[string]any{
"model": ollama.Model,
"prompt": createPrompt(ollama.Diff),
"prompt": createPrompt(diff),
"format": "json",
"stream": false,
"options": map[string]any{
Expand All @@ -40,6 +39,7 @@ func (ollama *ollamaEngine) request() (string, error) {

jsonData, err := json.Marshal(payload)
if err != nil {

return "", err
}

Expand All @@ -53,6 +53,10 @@ func (ollama *ollamaEngine) request() (string, error) {
var res map[string]interface{}
json.NewDecoder(resp.Body).Decode(&res)

if res == nil || res["response"] == nil {
return "", fmt.Errorf("ollama %s", res["error"])
}

// `response` is a JSON string
// https://github.com/ollama/ollama/blob/main/docs/api.md#response-2
var message struct {
Expand Down
2 changes: 1 addition & 1 deletion engine/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var prePrompt = `
- Explain WHAT were the changes and mainly WHY the changes were done.
- Convert 'git diff --staged' command it into a git commit message.
- Use the present tense.
- Lines limit to 50 characters.
- Lines limit to 50 characters.
- the commit message should be one paragraph
- Respond using JSON.
- JSON scheme {"commit_message": string}
Expand Down
3 changes: 3 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

mv kemit /usr/local/bin/
54 changes: 51 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,75 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/zaidfadhil/kemit/config"
"github.com/zaidfadhil/kemit/engine"
"github.com/zaidfadhil/kemit/git"
)

func main() {
cfg := &config.Config{}
cfg.LoadConfig()

if len(os.Args) > 1 {
if os.Args[1] == "config" {
setConfig(os.Args[2:], cfg)
}
} else {
run(cfg)
}
}

func run(cfg *config.Config) {
diff, err := git.Diff()
if err != nil {
fmt.Println("error:", err)
}

if len(diff) == 0 {
if diff == "" {
fmt.Println("nothing to commit")
} else {
ollama := engine.NewOllama(diff)
message, err := ollama.GetCommit()
ollama := engine.NewOllama(cfg.OllamaHost, cfg.OllamaModel)
message, err := ollama.GetCommit(diff)
if err != nil {
fmt.Println("error:", err)
} else {
fmt.Println(message)
}
}
}

var flags = flag.NewFlagSet("config", flag.ExitOnError)

var configUsage = `Usage: kemit config command [options]
Options:
Commands:
--ollama_host Set ollama host. ex: http://localhost:11434
--ollama_model Set ollama host. ex: llama3`

func setConfig(args []string, cfg *config.Config) error {
flags.Usage = func() {
fmt.Fprintln(os.Stderr, configUsage)
}

flags.StringVar(&cfg.OllamaHost, "ollama_host", cfg.OllamaHost, "ollama host")
flags.StringVar(&cfg.OllamaModel, "ollama_model", cfg.OllamaModel, "ollama model")

flags.Parse(args)

if len(args) == 0 {
flags.Usage()
}

err := cfg.SaveConfig()
if err != nil {
return err
}

return nil
}

0 comments on commit ab4177e

Please sign in to comment.