-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (55 loc) · 1.42 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
68
69
package main
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/pteich/configstruct"
"github.com/pterm/pterm"
"github.com/pterm/pterm/putils"
"github.com/pteich/clai/ai"
"github.com/pteich/clai/config"
)
func main() {
var aiRes ai.Response
var err error
cfg := config.Config{
Endpoint: "https://api.groq.com/openai/v1",
Model: "llama3-70b-8192",
}
opts := make([]configstruct.Option, 0)
configPath, err := config.FindConfigFile()
if err == nil {
opts = append(opts, configstruct.WithYamlConfig(configPath))
}
err = configstruct.Parse(&cfg, opts...)
if err != nil {
fmt.Println("Failed to parse config:", err)
fmt.Println("Usage: clai <prompt>")
os.Exit(1)
}
if len(os.Args) < 2 {
fmt.Println("Usage: clai <prompt>")
os.Exit(1)
}
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
defer cancel()
aiClient := ai.New(cfg.Token, cfg.Endpoint, cfg.Model, cfg.Shell, cfg.OS)
phrase := strings.Join(os.Args[1:], " ")
err = putils.RunWithDefaultSpinner("🤖 Thinking...", func(spinner *pterm.SpinnerPrinter) error {
aiRes, err = aiClient.Ask(ctx, phrase)
if err != nil {
return err
}
return nil
})
if err != nil {
pterm.Println(err)
os.Exit(1)
}
pterm.Println(pterm.Magenta(aiRes.Explanation))
pterm.Println()
pterm.Println(pterm.Green("$ ") + aiRes.Command)
}