-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (93 loc) · 2.06 KB
/
main.go
File metadata and controls
108 lines (93 loc) · 2.06 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/gitsang/configer"
"github.com/spf13/cobra"
)
type Config struct {
Adapter string
Adapters map[string]AdapterConfig
Timeout string `default:"30s"`
}
var rootCmd = &cobra.Command{
Use: "goldendict-llm",
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
var rootFlags = struct {
ConfigPaths []string
UserInput string
}{}
var cfger *configer.Configer
func joinArgs(args []string) string {
return strings.Join(args, " ")
}
func init() {
rootCmd.PersistentFlags().StringSliceVarP(&rootFlags.ConfigPaths, "config", "c", nil, "config file path")
rootCmd.PersistentFlags().StringVarP(&rootFlags.UserInput, "content", "m", "", "user input content")
rootCmd.Run = func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
rootFlags.UserInput = joinArgs(args)
}
run()
}
cfger = configer.New(
configer.WithTemplate(new(Config)),
configer.WithEnvBind(
configer.WithEnvPrefix("GOLDENDICT_LLM"),
configer.WithEnvDelim("_"),
),
configer.WithFlagBind(
configer.WithCommand(rootCmd),
configer.WithFlagPrefix(""),
configer.WithFlagDelim("."),
),
)
}
func run() {
var c Config
err := cfger.Load(&c, rootFlags.ConfigPaths...)
if err != nil {
panic(err)
}
// adapter
adapterConfig, ok := c.Adapters[c.Adapter]
if !ok {
panic(fmt.Errorf("adapter %s not found", c.Adapter))
}
adapterConfig.Name = c.Adapter
// http client
timeout, err := time.ParseDuration(c.Timeout)
if err != nil {
panic(fmt.Sprintf("Invalid timeout: %v", err))
}
httpClient := &http.Client{
Timeout: timeout,
}
// translator
translator := NewTranslator(adapterConfig,
WithHTTPClient(httpClient),
)
if input, found := strings.CutPrefix(rootFlags.UserInput, "S:"); !found {
result, err := translator.TranslateWord(input)
if err != nil {
panic(err)
}
fmt.Println(result)
} else {
result, err := translator.TranslateSentense(input)
if err != nil {
panic(err)
}
fmt.Println(result)
}
}
func main() {
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}