-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
100 lines (90 loc) · 2.42 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
99
100
package main
import (
"fmt"
"log"
"os"
"github.com/go-ini/ini"
"github.com/google/generative-ai-go/genai"
)
const (
CONFIG_PATH = "/.local/share/gemini/"
CONFIG_NAME = "config.ini"
)
type GeminiChatConfig struct {
ModelName string
GoogleAIKey string
SafetySetting []*genai.SafetySetting
}
func GetConfig() GeminiChatConfig {
cfg := loadConfig()
if cfg == nil {
log.Fatal("load config failed")
}
level := cfg.Section("SafetySetting").Key("level").MustInt(0)
googleAIKey := cfg.Section("Gemini").Key("googleAIKey").Value()
if googleAIKey == "" {
log.Fatalf("\x1b[31mPlease set your own google ai key in %s \n\x1b[0m",
HOME_PATH+CONFIG_PATH+CONFIG_NAME)
}
var safetyLevel genai.HarmBlockThreshold = genai.HarmBlockThreshold(level)
return GeminiChatConfig{
ModelName: cfg.Section("Gemini").Key("model").Value(),
GoogleAIKey: googleAIKey,
SafetySetting: []*genai.SafetySetting{
{
Category: genai.HarmCategorySexuallyExplicit,
Threshold: safetyLevel,
},
{
Category: genai.HarmCategoryHarassment,
Threshold: safetyLevel,
},
{
Category: genai.HarmCategoryHateSpeech,
Threshold: safetyLevel,
},
{
Category: genai.HarmCategoryDangerousContent,
Threshold: safetyLevel,
},
},
}
}
func loadConfig() *ini.File {
fullPath := HOME_PATH + CONFIG_PATH + CONFIG_NAME
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
os.MkdirAll(HOME_PATH+CONFIG_PATH, os.ModePerm)
initConfig := []byte(`
[Gemini]
# set your own google ai key
googleAIKey=
# set gemini model name
model=gemini-pro
[SafetySetting]
# HarmBlockUnspecified HarmBlockThreshold = 0
# HarmBlockLowAndAbove means content with NEGLIGIBLE will be allowed.
# HarmBlockLowAndAbove HarmBlockThreshold = 1
# HarmBlockMediumAndAbove means content with NEGLIGIBLE and LOW will be allowed.
# HarmBlockMediumAndAbove HarmBlockThreshold = 2
# HarmBlockOnlyHigh means content with NEGLIGIBLE, LOW, and MEDIUM will be allowed.
# HarmBlockOnlyHigh HarmBlockThreshold = 3
# HarmBlockNone means all content will be allowed.
# HarmBlockNone HarmBlockThreshold = 4
level=4
`)
err := os.WriteFile(fullPath, initConfig, 0644)
if err != nil {
fmt.Println("初始化配置文件失败:", err)
log.Fatal(err)
}
log.Fatalf("\x1b[31mPlease set your own google ai key in %s \n\x1b[0m", fullPath)
} else {
cfg, err := ini.Load(fullPath)
if err != nil {
// 处理错误
log.Fatal(err)
}
return cfg
}
return nil
}