config.go 7.7 KB

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