-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (58 loc) · 1.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"context"
"fmt"
"os"
"runtime"
"strings"
"github.com/atotto/clipboard"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/openai"
)
var version string
func main() {
if (os.Args[1] == "version") {
fmt.Println(version)
return
}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
debug := os.Getenv("DEBUG")
if debug == "true" {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
apiKey := os.Getenv("GROQ_API_KEY")
if apiKey == "" {
log.Fatal().Msg("GROQ_API_KEY is not set. Get one from https://console.groq.com/keys.")
}
shell := os.Getenv("SHELL")
if shell == "" {
log.Fatal().Msg("Could not determine shell. Set SHELL environment variable.")
}
_shell := strings.Split(shell, "/")
shell = _shell[len(_shell)-1]
kernel := runtime.GOOS
prompt := fmt.Sprintf(`Only reply with the single line command surrounded by three backticks. It must be able to be directly run in the target shell. Do not include any other text.
Make sure the command runs on %s shell on %s kernel.
The prompt: %s`, shell, kernel, strings.Join(os.Args[1:], " "))
log.Debug().Msg(prompt)
llm, err := openai.New(
openai.WithModel("gemma2-9b-it"),
openai.WithBaseURL("https://api.groq.com/openai/v1"),
openai.WithToken(apiKey),
)
if err != nil {
log.Fatal().Err(err).Msg("Failed to create llm")
}
completion, err := llms.GenerateFromSinglePrompt(context.Background(), llm, prompt)
if err != nil {
log.Fatal().Err(err).Msg("Failed to generate completion")
}
fmt.Println(strings.TrimSpace(strings.Trim(completion, "`")))
if err := clipboard.WriteAll(string(completion)); err != nil {
log.Debug().Err(err).Msg("Failed to copy to clipboard")
}
}