-
-
Notifications
You must be signed in to change notification settings - Fork 387
/
config.go
278 lines (268 loc) ยท 7.72 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package config
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"time"
"gopkg.in/yaml.v3"
"github.com/eryajf/chatgpt-dingtalk/pkg/logger"
)
type Credential struct {
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
}
// Configuration ้กน็ฎ้
็ฝฎ
type Configuration struct {
// ๆฅๅฟ็บงๅซ๏ผinfoๆ่
debug
LogLevel string `yaml:"log_level"`
// gpt apikey
ApiKey string `yaml:"api_key"`
// ่ฟ่กๆจกๅผ
RunMode string `yaml:"run_mode"`
// ่ฏทๆฑ็ URL ๅฐๅ
BaseURL string `yaml:"base_url"`
// ไฝฟ็จๆจกๅ
Model string `yaml:"model"`
// ไฝฟ็จ็ป็ปๆจกๅ
ImageModel string `yaml:"image_model"`
// ไผ่ฏ่ถ
ๆถๆถ้ด
SessionTimeout time.Duration `yaml:"session_timeout"`
// ๆๅคง้ฎ้ข้ฟๅบฆ
MaxQuestionLen int `yaml:"max_question_len"`
// ๆๅคง็ญๆก้ฟๅบฆ
MaxAnswerLen int `yaml:"max_answer_len"`
// ๆๅคงๆๆฌ = ้ฎ้ข + ๅ็ญ, ๆฅๅฃ้ๅถ
MaxText int `yaml:"max_text"`
// ้ป่ฎคๅฏน่ฏๆจกๅผ
DefaultMode string `yaml:"default_mode"`
// ไปฃ็ๅฐๅ
HttpProxy string `yaml:"http_proxy"`
// ็จๆทๅๆฅๆๅคง่ฏทๆฑๆฌกๆฐ
MaxRequest int `yaml:"max_request"`
// ๆๅฎๆๅกๅฏๅจ็ซฏๅฃ๏ผ้ป่ฎคไธบ 8090
Port string `yaml:"port"`
// ๆๅฎๆๅก็ๅฐๅ๏ผๅฐฑๆฏ้้ๆบๅจไบบ้
็ฝฎ็ๅ่ฐๅฐๅ๏ผๆฏๅฆ: http://chat.eryajf.net
ServiceURL string `yaml:"service_url"`
// ้ๅฎๅฏน่ฏ็ฑปๅ 0๏ผไธ้ 1๏ผๅ่ 2๏ผ็พค่
ChatType string `yaml:"chat_type"`
// ๅชไบ็พค็ปๅฏไปฅ่ฟ่กๅฏน่ฏ
AllowGroups []string `yaml:"allow_groups"`
// ๅชไบoutgoing็พค็ปๅฏไปฅ่ฟ่กๅฏน่ฏ
AllowOutgoingGroups []string `yaml:"allow_outgoing_groups"`
// ๅชไบ็จๆทๅฏไปฅ่ฟ่กๅฏน่ฏ
AllowUsers []string `yaml:"allow_users"`
// ๅชไบ็จๆทไธๅฏไปฅ่ฟ่กๅฏน่ฏ
DenyUsers []string `yaml:"deny_users"`
// ๅชไบVip็จๆทๅฏไปฅ่ฟ่กๆ ้ๅฏน่ฏ
VipUsers []string `yaml:"vip_users"`
// ๆๅฎๅชไบไบบไธบๆญค็ณป็ป็็ฎก็ๅ๏ผๅฟ
้กปๆๅฎ๏ผๅฆๅๆๆไบบ้ฝๆฏ
AdminUsers []string `yaml:"admin_users"`
// ้้ๆบๅจไบบๅจๅบ็จไฟกๆฏไธญ็AppSecret๏ผไธบไบๆ ก้ชๅ่ฐ็่ฏทๆฑๆฏๅฆๅๆณ๏ผๅฆๆไฝ ็ๆๅกๅฏนๆฅ็ปๅคไธชๆบๅจไบบ๏ผ่ฟ้ๅฏไปฅ้
็ฝฎๅคไธชๆบๅจไบบ็secret
AppSecrets []string `yaml:"app_secrets"`
// ๆๆ่ฏ๏ผๆ้ฎๆถ่งฆๅ๏ผๅไธๅ
่ฎธๆ้ฎ๏ผๅ็ญ็ๅ
ๅฎนไธญ่งฆๅ๏ผๅไปฅ ๐ซ ไปฃๆฟ
SensitiveWords []string `yaml:"sensitive_words"`
// ่ชๅฎไนๅธฎๅฉไฟกๆฏ
Help string `yaml:"help"`
// AzureOpenAI ้
็ฝฎ
AzureOn bool `yaml:"azure_on"`
AzureApiVersion string `yaml:"azure_api_version"`
AzureResourceName string `yaml:"azure_resource_name"`
AzureDeploymentName string `yaml:"azure_deployment_name"`
AzureOpenAIToken string `yaml:"azure_openai_token"`
// ้้ๅบ็จ้ดๆๅญๆฎ
Credentials []Credential `yaml:"credentials"`
}
var (
config *Configuration
once sync.Once
)
// LoadConfig ๅ ่ฝฝ้
็ฝฎ
func LoadConfig() *Configuration {
once.Do(func() {
// ไปๆไปถไธญ่ฏปๅ
config = &Configuration{}
data, err := os.ReadFile("config.yml")
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(data, &config)
if err != nil {
log.Fatal(err)
}
// ๅฆๆ็ฏๅขๅ้ๆ้
็ฝฎ๏ผ่ฏปๅ็ฏๅขๅ้
logLevel := os.Getenv("LOG_LEVEL")
if logLevel != "" {
config.LogLevel = logLevel
}
apiKey := os.Getenv("APIKEY")
if apiKey != "" {
config.ApiKey = apiKey
}
runMode := os.Getenv("RUN_MODE")
if runMode != "" {
config.RunMode = runMode
}
baseURL := os.Getenv("BASE_URL")
if baseURL != "" {
config.BaseURL = baseURL
}
model := os.Getenv("MODEL")
if model != "" {
config.Model = model
}
sessionTimeout := os.Getenv("SESSION_TIMEOUT")
if sessionTimeout != "" {
duration, err := strconv.ParseInt(sessionTimeout, 10, 64)
if err != nil {
logger.Fatal(fmt.Sprintf("config session timeout err: %v ,get is %v", err, sessionTimeout))
return
}
config.SessionTimeout = time.Duration(duration) * time.Second
} else {
config.SessionTimeout = time.Duration(config.SessionTimeout) * time.Second
}
maxQuestionLen := os.Getenv("MAX_QUESTION_LEN")
if maxQuestionLen != "" {
newLen, _ := strconv.Atoi(maxQuestionLen)
config.MaxQuestionLen = newLen
}
maxAnswerLen := os.Getenv("MAX_ANSWER_LEN")
if maxAnswerLen != "" {
newLen, _ := strconv.Atoi(maxAnswerLen)
config.MaxAnswerLen = newLen
}
maxText := os.Getenv("MAX_TEXT")
if maxText != "" {
newLen, _ := strconv.Atoi(maxText)
config.MaxText = newLen
}
defaultMode := os.Getenv("DEFAULT_MODE")
if defaultMode != "" {
config.DefaultMode = defaultMode
}
httpProxy := os.Getenv("HTTP_PROXY")
if httpProxy != "" {
config.HttpProxy = httpProxy
}
maxRequest := os.Getenv("MAX_REQUEST")
if maxRequest != "" {
newMR, _ := strconv.Atoi(maxRequest)
config.MaxRequest = newMR
}
port := os.Getenv("PORT")
if port != "" {
config.Port = port
}
serviceURL := os.Getenv("SERVICE_URL")
if serviceURL != "" {
config.ServiceURL = serviceURL
}
chatType := os.Getenv("CHAT_TYPE")
if chatType != "" {
config.ChatType = chatType
}
allowGroups := os.Getenv("ALLOW_GROUPS")
if allowGroups != "" {
config.AllowGroups = strings.Split(allowGroups, ",")
}
allowOutgoingGroups := os.Getenv("ALLOW_OUTGOING_GROUPS")
if allowOutgoingGroups != "" {
config.AllowOutgoingGroups = strings.Split(allowOutgoingGroups, ",")
}
allowUsers := os.Getenv("ALLOW_USERS")
if allowUsers != "" {
config.AllowUsers = strings.Split(allowUsers, ",")
}
denyUsers := os.Getenv("DENY_USERS")
if denyUsers != "" {
config.DenyUsers = strings.Split(denyUsers, ",")
}
vipUsers := os.Getenv("VIP_USERS")
if vipUsers != "" {
config.VipUsers = strings.Split(vipUsers, ",")
}
adminUsers := os.Getenv("ADMIN_USERS")
if adminUsers != "" {
config.AdminUsers = strings.Split(adminUsers, ",")
}
appSecrets := os.Getenv("APP_SECRETS")
if appSecrets != "" {
config.AppSecrets = strings.Split(appSecrets, ",")
}
sensitiveWords := os.Getenv("SENSITIVE_WORDS")
if sensitiveWords != "" {
config.SensitiveWords = strings.Split(sensitiveWords, ",")
}
help := os.Getenv("HELP")
if help != "" {
config.Help = help
}
azureOn := os.Getenv("AZURE_ON")
if azureOn != "" {
config.AzureOn = azureOn == "true"
}
azureApiVersion := os.Getenv("AZURE_API_VERSION")
if azureApiVersion != "" {
config.AzureApiVersion = azureApiVersion
}
azureResourceName := os.Getenv("AZURE_RESOURCE_NAME")
if azureResourceName != "" {
config.AzureResourceName = azureResourceName
}
azureDeploymentName := os.Getenv("AZURE_DEPLOYMENT_NAME")
if azureDeploymentName != "" {
config.AzureDeploymentName = azureDeploymentName
}
azureOpenaiToken := os.Getenv("AZURE_OPENAI_TOKEN")
if azureOpenaiToken != "" {
config.AzureOpenAIToken = azureOpenaiToken
}
credentials := os.Getenv("DINGTALK_CREDENTIALS")
if credentials != "" {
config.Credentials = []Credential{}
for _, idSecret := range strings.Split(credentials, ",") {
items := strings.SplitN(idSecret, ":", 2)
if len(items) == 2 {
config.Credentials = append(config.Credentials, Credential{ClientID: items[0], ClientSecret: items[1]})
}
}
}
})
// ไธไบ้ป่ฎคๅผ
if config.LogLevel == "" {
config.LogLevel = "info"
}
if config.RunMode == "" {
config.RunMode = "http"
}
if config.Model == "" {
config.Model = "gpt-3.5-turbo"
}
if config.DefaultMode == "" {
config.DefaultMode = "ๅ่"
}
if config.Port == "" {
config.Port = "8090"
}
if config.ChatType == "" {
config.ChatType = "0"
}
if !config.AzureOn {
if config.ApiKey == "" {
panic("config err: api key required")
}
}
if config.MaxQuestionLen == 0 {
config.MaxQuestionLen = 4096
}
if config.MaxAnswerLen == 0 {
config.MaxAnswerLen = 4096
}
if config.MaxText == 0 {
config.MaxText = 4096
}
return config
}