config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "sync"
  8. "time"
  9. "github.com/eryajf/chatgpt-dingtalk/public/logger"
  10. )
  11. // Configuration 项目配置
  12. type Configuration struct {
  13. // gtp apikey
  14. ApiKey string `json:"api_key"`
  15. // 请求的 URL 地址
  16. BaseURL string `json:"base_url"`
  17. // 使用模型
  18. Model string `json:"model"`
  19. // 会话超时时间
  20. SessionTimeout time.Duration `json:"session_timeout"`
  21. // 默认对话模式
  22. DefaultMode string `json:"default_mode"`
  23. // 代理地址
  24. HttpProxy string `json:"http_proxy"`
  25. // 用户单日最大请求次数
  26. MaxRequest int `json:"max_request"`
  27. // 指定服务的地址,就是钉钉机器人配置的回调地址,比如: http://chat.eryajf.net
  28. ServiceURL string `json:"service_url"`
  29. }
  30. var config *Configuration
  31. var once sync.Once
  32. // LoadConfig 加载配置
  33. func LoadConfig() *Configuration {
  34. once.Do(func() {
  35. // 从文件中读取
  36. config = &Configuration{}
  37. f, err := os.Open("config.json")
  38. if err != nil {
  39. logger.Danger(fmt.Errorf("open config err: %+v", err))
  40. return
  41. }
  42. defer f.Close()
  43. encoder := json.NewDecoder(f)
  44. err = encoder.Decode(config)
  45. if err != nil {
  46. logger.Warning(fmt.Errorf("decode config err: %v", err))
  47. return
  48. }
  49. // 如果环境变量有配置,读取环境变量
  50. apiKey := os.Getenv("APIKEY")
  51. baseURL := os.Getenv("BASE_URL")
  52. model := os.Getenv("MODEL")
  53. sessionTimeout := os.Getenv("SESSION_TIMEOUT")
  54. defaultMode := os.Getenv("DEFAULT_MODE")
  55. httpProxy := os.Getenv("HTTP_PROXY")
  56. maxRequest := os.Getenv("MAX_REQUEST")
  57. serviceURL := os.Getenv("SERVICE_URL")
  58. if apiKey != "" {
  59. config.ApiKey = apiKey
  60. }
  61. if baseURL != "" {
  62. config.BaseURL = baseURL
  63. }
  64. if sessionTimeout != "" {
  65. duration, err := strconv.ParseInt(sessionTimeout, 10, 64)
  66. if err != nil {
  67. logger.Danger(fmt.Sprintf("config session timeout err: %v ,get is %v", err, sessionTimeout))
  68. return
  69. }
  70. config.SessionTimeout = time.Duration(duration) * time.Second
  71. } else {
  72. config.SessionTimeout = time.Duration(config.SessionTimeout) * time.Second
  73. }
  74. if defaultMode != "" {
  75. config.DefaultMode = defaultMode
  76. }
  77. if httpProxy != "" {
  78. config.HttpProxy = httpProxy
  79. }
  80. if model != "" {
  81. config.Model = model
  82. }
  83. if maxRequest != "" {
  84. newMR, _ := strconv.Atoi(maxRequest)
  85. config.MaxRequest = newMR
  86. }
  87. if serviceURL != "" {
  88. config.ServiceURL = serviceURL
  89. }
  90. })
  91. if config.Model == "" {
  92. config.DefaultMode = "gpt-3.5-turbo"
  93. }
  94. if config.DefaultMode == "" {
  95. config.DefaultMode = "单聊"
  96. }
  97. if config.ApiKey == "" {
  98. logger.Danger("config err: api key required")
  99. }
  100. if config.ServiceURL == "" {
  101. logger.Danger("config err: service url required")
  102. }
  103. return config
  104. }