-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
131 lines (121 loc) · 3.83 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"os"
)
type OneBot11Config struct {
AccessToken string `yaml:"accessToken"`
ServerUrl string `yaml:"serverUrl"`
SelfId int64 `yaml:"-"`
HeartbeatTimeOut int `yaml:"heartbeatTimeOut" comment:"heartbeat的超时时间"`
}
type OpenAIConfig struct {
ChatAIUrl string `yaml:"chatAIUrl" comment:"调用API的URL"`
APIKey string `yaml:"APIKey"`
Model string `yaml:"model"`
ResponseMaxTokens int `yaml:"responseMaxTokens" comment:"AI回复内容的最大token数量"`
GroupChatMaxTokens int `yaml:"groupChatMaxTokens" comment:"群聊模式下全部prompts的最大token数量"`
PrivateChatMaxTokens int `yaml:"privateChatMaxTokens" comment:"非群聊模式下全部prompts的最大token数量"`
EnableGroupChat map[int64]bool `yaml:"-"`
DefaultTemperature float64 `yaml:"defaultTemperature"`
InitialPrompts string `yaml:"initialPrompts" comment:"初始化AI设定的prompts"`
MinInterval float64 `yaml:"minInterval" comment:"最短API调用间隔"`
}
type RedisConfig struct {
Address string `yaml:"address"`
Password string `yaml:"password"`
Database int `yaml:"database"`
}
type GreetingConfig struct {
EnableGreeting bool `yaml:"enableGreeting"`
GreetingMessage Message `yaml:"greetingMessage" comment:"打招呼信息,遵循Onebot的message标准"`
}
type ServerConfig struct {
Address string `yaml:"address"`
AdminIds []int64 `yaml:"adminIds" comment:"管理员帐号ID"`
}
type OpenWechatConfig struct {
AppID string `yaml:"app_id"`
AppSecret string `yaml:"app_secret"`
Token string `yaml:"token"`
EncodingAESKey string `yaml:"encoding_aes_key"`
}
type Config struct {
Server ServerConfig `yaml:"server"`
OneBot11 OneBot11Config `yaml:"oneBot11"`
AI OpenAIConfig `yaml:"openAI"`
Redis RedisConfig `yaml:"redis"`
Greeting GreetingConfig `yaml:"greeting"`
OpenWechat OpenWechatConfig `yaml:"open_wechat"`
ServeMode string `yaml:"serve_mode"`
Debug bool `yaml:"debug"`
}
var GlobalConfig *Config
func InitGlobalConfig() error {
GlobalConfig = &Config{
Debug: false,
ServeMode: "",
OpenWechat: OpenWechatConfig{
AppID: "",
AppSecret: "",
Token: "",
EncodingAESKey: "",
},
Server: ServerConfig{
Address: "0.0.0.0:5701",
AdminIds: []int64{123456},
},
OneBot11: OneBot11Config{
AccessToken: "",
ServerUrl: "http://0.0.0.0:5700/",
HeartbeatTimeOut: 20,
},
AI: OpenAIConfig{
ChatAIUrl: "https://api.openai.com/v1/completions",
APIKey: "YOUR_API_KEY",
Model: "gpt-3.5-turbo",
ResponseMaxTokens: 4000,
GroupChatMaxTokens: 4000,
PrivateChatMaxTokens: 4000,
DefaultTemperature: 0.9,
InitialPrompts: "",
MinInterval: 1,
},
Redis: RedisConfig{
Address: "127.0.0.1:6379",
Password: "",
Database: 0,
},
Greeting: GreetingConfig{
EnableGreeting: false,
GreetingMessage: Message{
Type: "text",
Data: map[string]interface{}{
"text": "",
},
},
},
}
yamlFile, err := os.ReadFile("config.yaml")
if err == nil {
err = yaml.Unmarshal(yamlFile, GlobalConfig)
if err != nil {
return err
}
GlobalConfig.AI.EnableGroupChat = make(map[int64]bool)
} else {
// Save default config to YAML file if it does not exist
yamlData, err := yaml.Marshal(GlobalConfig)
if err != nil {
return err
}
err = os.WriteFile("config.yaml", yamlData, 0644)
if err != nil {
return err
}
logrus.Warning("Config file does not exist: Creating a default one. Please edit configuration and restart.")
os.Exit(0)
}
return nil
}