-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
config.go
98 lines (86 loc) · 4.33 KB
/
config.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
96
97
98
package cmd
import (
"errors"
"strings"
"time"
"github.com/appleboy/com/array"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var availableKeys = []string{
"git.diff_unified",
"git.exclude_list",
"git.template_file",
"git.template_string",
"openai.socks",
"openai.api_key",
"openai.model",
"openai.org_id",
"openai.proxy",
"output.lang",
"openai.base_url",
"openai.timeout",
"openai.max_tokens",
"openai.temperature",
"openai.provider",
"openai.model_name",
}
func init() {
configCmd.PersistentFlags().StringP("base_url", "b", "", "what API base url to use.")
configCmd.PersistentFlags().StringP("api_key", "k", "", "openai api key")
configCmd.PersistentFlags().StringP("model", "m", "gpt-3.5-turbo", "openai model")
configCmd.PersistentFlags().StringP("lang", "l", "en", "summarizing language uses English by default")
configCmd.PersistentFlags().StringP("org_id", "o", "", "openai requesting organization")
configCmd.PersistentFlags().StringP("proxy", "", "", "http proxy")
configCmd.PersistentFlags().StringP("socks", "", "", "socks proxy")
configCmd.PersistentFlags().DurationP("timeout", "t", 10*time.Second, "http timeout")
configCmd.PersistentFlags().StringP("template_file", "", "", "git commit message file")
configCmd.PersistentFlags().StringP("template_string", "", "", "git commit message string")
configCmd.PersistentFlags().IntP("diff_unified", "", 3, "generate diffs with <n> lines of context, default is 3")
configCmd.PersistentFlags().IntP("max_tokens", "", 300, "the maximum number of tokens to generate in the chat completion.")
configCmd.PersistentFlags().Float32P("temperature", "", 0.7, "What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.")
configCmd.PersistentFlags().StringSliceP("exclude_list", "", []string{}, "exclude file from `git diff` command")
configCmd.PersistentFlags().StringP("provider", "", "openai", "srvice provider, only support 'openai' or 'azure'")
configCmd.PersistentFlags().StringP("model_name", "", "", "model deployment name for Azure cognitive service")
_ = viper.BindPFlag("openai.base_url", configCmd.PersistentFlags().Lookup("base_url"))
_ = viper.BindPFlag("openai.org_id", configCmd.PersistentFlags().Lookup("org_id"))
_ = viper.BindPFlag("openai.api_key", configCmd.PersistentFlags().Lookup("api_key"))
_ = viper.BindPFlag("openai.model", configCmd.PersistentFlags().Lookup("model"))
_ = viper.BindPFlag("openai.proxy", configCmd.PersistentFlags().Lookup("proxy"))
_ = viper.BindPFlag("openai.socks", configCmd.PersistentFlags().Lookup("socks"))
_ = viper.BindPFlag("openai.timeout", configCmd.PersistentFlags().Lookup("timeout"))
_ = viper.BindPFlag("openai.max_tokens", configCmd.PersistentFlags().Lookup("max_tokens"))
_ = viper.BindPFlag("openai.temperature", configCmd.PersistentFlags().Lookup("temperature"))
_ = viper.BindPFlag("output.lang", configCmd.PersistentFlags().Lookup("lang"))
_ = viper.BindPFlag("git.diff_unified", configCmd.PersistentFlags().Lookup("diff_unified"))
_ = viper.BindPFlag("git.exclude_list", configCmd.PersistentFlags().Lookup("exclude_list"))
_ = viper.BindPFlag("git.template_file", configCmd.PersistentFlags().Lookup("template_file"))
_ = viper.BindPFlag("git.template_string", configCmd.PersistentFlags().Lookup("template_string"))
_ = viper.BindPFlag("openai.provider", configCmd.PersistentFlags().Lookup("provider"))
_ = viper.BindPFlag("openai.model_name", configCmd.PersistentFlags().Lookup("model_name"))
}
var configCmd = &cobra.Command{
Use: "config",
Short: "Add openai config (openai.api_key, openai.model ...)",
Args: cobra.MinimumNArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
// Check if command is 'set'
if args[0] != "set" {
return errors.New("config set key value. ex: config set openai.api_key sk-...")
}
// Check if key is available
if !array.InSlice(args[1], availableKeys) {
return errors.New("available key list: " + strings.Join(availableKeys, ", "))
}
// Set config value in viper
viper.Set(args[1], args[2])
// Write config to file
if err := viper.WriteConfig(); err != nil {
return err
}
// Print success message with config file location
color.Green("you can see the config file: %s", viper.ConfigFileUsed())
return nil
},
}