config.go 6.8 KB

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