config.go 7.7 KB

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