config.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package config
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "time"
  11. "github.com/eryajf/chatgpt-dingtalk/pkg/logger"
  12. "gopkg.in/yaml.v2"
  13. )
  14. type Credential struct {
  15. ClientID string `yaml:"client_id"`
  16. ClientSecret string `yaml:"client_secret"`
  17. }
  18. // Configuration 项目配置
  19. type Configuration struct {
  20. // 日志级别,info或者debug
  21. LogLevel string `yaml:"log_level"`
  22. // gpt apikey
  23. ApiKey string `yaml:"api_key"`
  24. // 运行模式
  25. RunMode string `yaml:"run_mode"`
  26. // 请求的 URL 地址
  27. BaseURL string `yaml:"base_url"`
  28. // 使用模型
  29. Model string `yaml:"model"`
  30. // 会话超时时间
  31. SessionTimeout time.Duration `yaml:"session_timeout"`
  32. // 最大问题长度
  33. MaxQuestionLen int `yaml:"max_question_len"`
  34. // 最大答案长度
  35. MaxAnswerLen int `yaml:"max_answer_len"`
  36. // 最大文本 = 问题 + 回答, 接口限制
  37. MaxText int `yaml:"max_text"`
  38. // 默认对话模式
  39. DefaultMode string `yaml:"default_mode"`
  40. // 代理地址
  41. HttpProxy string `yaml:"http_proxy"`
  42. // 用户单日最大请求次数
  43. MaxRequest int `yaml:"max_request"`
  44. // 指定服务启动端口,默认为 8090
  45. Port string `yaml:"port"`
  46. // 指定服务的地址,就是钉钉机器人配置的回调地址,比如: http://chat.eryajf.net
  47. ServiceURL string `yaml:"service_url"`
  48. // 限定对话类型 0:不限 1:单聊 2:群聊
  49. ChatType string `yaml:"chat_type"`
  50. // 哪些群组可以进行对话
  51. AllowGroups []string `yaml:"allow_groups"`
  52. // 哪些outgoing群组可以进行对话
  53. AllowOutgoingGroups []string `yaml:"allow_outgoing_groups"`
  54. // 哪些用户可以进行对话
  55. AllowUsers []string `yaml:"allow_users"`
  56. // 哪些用户不可以进行对话
  57. DenyUsers []string `yaml:"deny_users"`
  58. // 哪些Vip用户可以进行无限对话
  59. VipUsers []string `yaml:"vip_users"`
  60. // 指定哪些人为此系统的管理员,必须指定,否则所有人都是
  61. AdminUsers []string `yaml:"admin_users"`
  62. // 钉钉机器人在应用信息中的AppSecret,为了校验回调的请求是否合法,如果你的服务对接给多个机器人,这里可以配置多个机器人的secret
  63. AppSecrets []string `yaml:"app_secrets"`
  64. // 敏感词,提问时触发,则不允许提问,回答的内容中触发,则以 🚫 代替
  65. SensitiveWords []string `yaml:"sensitive_words"`
  66. // 自定义帮助信息
  67. Help string `yaml:"help"`
  68. // AzureOpenAI 配置
  69. AzureOn bool `yaml:"azure_on"`
  70. AzureApiVersion string `yaml:"azure_api_version"`
  71. AzureResourceName string `yaml:"azure_resource_name"`
  72. AzureDeploymentName string `yaml:"azure_deployment_name"`
  73. AzureOpenAIToken string `yaml:"azure_openai_token"`
  74. // 钉钉应用鉴权凭据
  75. Credentials []Credential `yaml:"credentials"`
  76. }
  77. var config *Configuration
  78. var once sync.Once
  79. // LoadConfig 加载配置
  80. func LoadConfig() *Configuration {
  81. once.Do(func() {
  82. // 从文件中读取
  83. config = &Configuration{}
  84. data, err := ioutil.ReadFile("config.yml")
  85. if err != nil {
  86. log.Fatal(err)
  87. }
  88. err = yaml.Unmarshal(data, &config)
  89. if err != nil {
  90. log.Fatal(err)
  91. }
  92. // 如果环境变量有配置,读取环境变量
  93. logLevel := os.Getenv("LOG_LEVEL")
  94. if logLevel != "" {
  95. config.LogLevel = logLevel
  96. }
  97. apiKey := os.Getenv("APIKEY")
  98. if apiKey != "" {
  99. config.ApiKey = apiKey
  100. }
  101. runMode := os.Getenv("RUN_MODE")
  102. if runMode != "" {
  103. config.RunMode = runMode
  104. }
  105. baseURL := os.Getenv("BASE_URL")
  106. if baseURL != "" {
  107. config.BaseURL = baseURL
  108. }
  109. model := os.Getenv("MODEL")
  110. if model != "" {
  111. config.Model = model
  112. }
  113. sessionTimeout := os.Getenv("SESSION_TIMEOUT")
  114. if sessionTimeout != "" {
  115. duration, err := strconv.ParseInt(sessionTimeout, 10, 64)
  116. if err != nil {
  117. logger.Fatal(fmt.Sprintf("config session timeout err: %v ,get is %v", err, sessionTimeout))
  118. return
  119. }
  120. config.SessionTimeout = time.Duration(duration) * time.Second
  121. } else {
  122. config.SessionTimeout = time.Duration(config.SessionTimeout) * time.Second
  123. }
  124. maxQuestionLen := os.Getenv("MAX_QUESTION_LEN")
  125. if maxQuestionLen != "" {
  126. newLen, _ := strconv.Atoi(maxQuestionLen)
  127. config.MaxQuestionLen = newLen
  128. }
  129. maxAnswerLen := os.Getenv("MAX_ANSWER_LEN")
  130. if maxAnswerLen != "" {
  131. newLen, _ := strconv.Atoi(maxAnswerLen)
  132. config.MaxAnswerLen = newLen
  133. }
  134. maxText := os.Getenv("MAX_TEXT")
  135. if maxText != "" {
  136. newLen, _ := strconv.Atoi(maxText)
  137. config.MaxText = newLen
  138. }
  139. defaultMode := os.Getenv("DEFAULT_MODE")
  140. if defaultMode != "" {
  141. config.DefaultMode = defaultMode
  142. }
  143. httpProxy := os.Getenv("HTTP_PROXY")
  144. if httpProxy != "" {
  145. config.HttpProxy = httpProxy
  146. }
  147. maxRequest := os.Getenv("MAX_REQUEST")
  148. if maxRequest != "" {
  149. newMR, _ := strconv.Atoi(maxRequest)
  150. config.MaxRequest = newMR
  151. }
  152. port := os.Getenv("PORT")
  153. if port != "" {
  154. config.Port = port
  155. }
  156. serviceURL := os.Getenv("SERVICE_URL")
  157. if serviceURL != "" {
  158. config.ServiceURL = serviceURL
  159. }
  160. chatType := os.Getenv("CHAT_TYPE")
  161. if chatType != "" {
  162. config.ChatType = chatType
  163. }
  164. allowGroups := os.Getenv("ALLOW_GROUPS")
  165. if allowGroups != "" {
  166. config.AllowGroups = strings.Split(allowGroups, ",")
  167. }
  168. allowOutgoingGroups := os.Getenv("ALLOW_OUTGOING_GROUPS")
  169. if allowOutgoingGroups != "" {
  170. config.AllowOutgoingGroups = strings.Split(allowOutgoingGroups, ",")
  171. }
  172. allowUsers := os.Getenv("ALLOW_USERS")
  173. if allowUsers != "" {
  174. config.AllowUsers = strings.Split(allowUsers, ",")
  175. }
  176. denyUsers := os.Getenv("DENY_USERS")
  177. if denyUsers != "" {
  178. config.DenyUsers = strings.Split(denyUsers, ",")
  179. }
  180. vipUsers := os.Getenv("VIP_USERS")
  181. if vipUsers != "" {
  182. config.VipUsers = strings.Split(vipUsers, ",")
  183. }
  184. adminUsers := os.Getenv("ADMIN_USERS")
  185. if adminUsers != "" {
  186. config.AdminUsers = strings.Split(adminUsers, ",")
  187. }
  188. appSecrets := os.Getenv("APP_SECRETS")
  189. if appSecrets != "" {
  190. config.AppSecrets = strings.Split(appSecrets, ",")
  191. }
  192. sensitiveWords := os.Getenv("SENSITIVE_WORDS")
  193. if sensitiveWords != "" {
  194. config.SensitiveWords = strings.Split(sensitiveWords, ",")
  195. }
  196. help := os.Getenv("HELP")
  197. if help != "" {
  198. config.Help = help
  199. }
  200. azureOn := os.Getenv("AZURE_ON")
  201. if azureOn != "" {
  202. config.AzureOn = azureOn == "true"
  203. }
  204. azureApiVersion := os.Getenv("AZURE_API_VERSION")
  205. if azureApiVersion != "" {
  206. config.AzureApiVersion = azureApiVersion
  207. }
  208. azureResourceName := os.Getenv("AZURE_RESOURCE_NAME")
  209. if azureResourceName != "" {
  210. config.AzureResourceName = azureResourceName
  211. }
  212. azureDeploymentName := os.Getenv("AZURE_DEPLOYMENT_NAME")
  213. if azureDeploymentName != "" {
  214. config.AzureDeploymentName = azureDeploymentName
  215. }
  216. azureOpenaiToken := os.Getenv("AZURE_OPENAI_TOKEN")
  217. if azureOpenaiToken != "" {
  218. config.AzureOpenAIToken = azureOpenaiToken
  219. }
  220. credentials := os.Getenv("DINGTALK_CREDENTIALS")
  221. if credentials != "" {
  222. if config.Credentials == nil {
  223. config.Credentials = []Credential{}
  224. }
  225. for _, idSecret := range strings.Split(credentials, ",") {
  226. items := strings.SplitN(idSecret, ":", 2)
  227. if len(items) == 2 {
  228. config.Credentials = append(config.Credentials, Credential{ClientID: items[0], ClientSecret: items[1]})
  229. }
  230. }
  231. }
  232. })
  233. // 一些默认值
  234. if config.LogLevel == "" {
  235. config.LogLevel = "info"
  236. }
  237. if config.RunMode == "" {
  238. config.LogLevel = "http"
  239. }
  240. if config.Model == "" {
  241. config.Model = "gpt-3.5-turbo"
  242. }
  243. if config.DefaultMode == "" {
  244. config.DefaultMode = "单聊"
  245. }
  246. if config.Port == "" {
  247. config.Port = "8090"
  248. }
  249. if config.ChatType == "" {
  250. config.ChatType = "0"
  251. }
  252. if config.ApiKey == "" {
  253. logger.Fatal("config err: api key required")
  254. }
  255. if config.ServiceURL == "" {
  256. config.ServiceURL = "http://127.0.0.1:8090"
  257. }
  258. if config.MaxQuestionLen == 0 {
  259. config.MaxQuestionLen = 4096
  260. }
  261. if config.MaxAnswerLen == 0 {
  262. config.MaxAnswerLen = 4096
  263. }
  264. if config.MaxText == 0 {
  265. config.MaxText = 4096
  266. }
  267. return config
  268. }