-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
95 lines (87 loc) · 2.13 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/greycodee/tmcmd/llm"
"github.com/greycodee/tmcmd/llm/google"
"github.com/greycodee/tmcmd/llm/ollama"
"github.com/greycodee/tmcmd/llm/openai"
"github.com/greycodee/tmcmd/util"
)
var (
defaultProvider = flag.String("default", "", "modify the default-provider of the configuration file")
requestProvider = flag.String("p", "", "set the llm provider for this request")
query = flag.String("q", "", "enter your prompt")
)
func init() {
flag.Parse()
if *defaultProvider != "" {
err := util.SetDefaultProvider(*defaultProvider)
if err != nil {
logError(err)
}
logInfo(fmt.Sprintf("set default provider: %s\n", *defaultProvider))
}
if *query == "" {
logError(errors.New("please use -q to enter your prompt"))
}
}
func main() {
s := spinner.New(spinner.CharSets[34], 100*time.Millisecond)
s.Start()
s.Color("green")
userPrompt := os.Args[1:]
if len(userPrompt) == 0 {
userPrompt = append(userPrompt, "how to query current information")
}
config, err := util.GetConfig()
if err != nil {
s.Stop()
logError(err)
return
}
if *requestProvider != "" {
if !util.IsSupportedProvider(*requestProvider) {
s.Stop()
logError(errors.New("unsupported provider: " + *requestProvider))
}
config.DefaultProvider = *requestProvider
}
var llm llm.LLMBaseInterface
switch config.DefaultProvider {
case "ollama":
// Initialize ollama
llm = new(ollama.Ollama)
case "google":
// Initialize google
llm = new(google.Gemini)
case "openai":
// Initialize openai
llm = new(openai.OpenAI)
default:
s.Stop()
logError(errors.New("default provider not supported"))
}
llm.Init(config.LLMProvider[config.DefaultProvider])
command, err := llm.GenerateCommand(strings.Join(userPrompt, ""))
if err != nil {
s.Stop()
logError(err)
}
s.Stop()
fmt.Printf("\033[42mRecommended command\033[0m\r\n\033[32m%s\033[0m\n", command)
}
func logError(err error) {
if err != nil {
log.Fatalf("\033[31mError: %v\033[0m\n", err)
}
}
func logInfo(info string) {
log.Fatalf("\033[32m%s\033[0m\n", info)
}