config.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package config
  2. import (
  3. "slices"
  4. "strconv"
  5. "sync/atomic"
  6. "github.com/labring/aiproxy/core/common/env"
  7. )
  8. var (
  9. disableServe atomic.Bool
  10. logStorageHours int64 // default 0 means no limit
  11. retryLogStorageHours int64 // default 0 means no limit
  12. saveAllLogDetail atomic.Bool
  13. logDetailRequestBodyMaxSize int64 = 128 * 1024 // 128KB
  14. logDetailResponseBodyMaxSize int64 = 128 * 1024 // 128KB
  15. logDetailStorageHours int64 = 3 * 24 // 3 days
  16. cleanLogBatchSize int64 = 2000
  17. notifyNote atomic.Value
  18. ipGroupsThreshold int64
  19. ipGroupsBanThreshold int64
  20. retryTimes atomic.Int64
  21. defaultChannelModels atomic.Value
  22. defaultChannelModelMapping atomic.Value
  23. groupMaxTokenNum atomic.Int64
  24. groupConsumeLevelRatio atomic.Value
  25. defaultMCPHost atomic.Value
  26. publicMCPHost atomic.Value
  27. groupMCPHost atomic.Value
  28. )
  29. func init() {
  30. defaultChannelModels.Store(make(map[int][]string))
  31. defaultChannelModelMapping.Store(make(map[int]map[string]string))
  32. groupConsumeLevelRatio.Store(make(map[float64]float64))
  33. notifyNote.Store("")
  34. defaultMCPHost.Store("")
  35. publicMCPHost.Store("")
  36. groupMCPHost.Store("")
  37. }
  38. func GetRetryTimes() int64 {
  39. return retryTimes.Load()
  40. }
  41. func SetRetryTimes(times int64) {
  42. times = env.Int64("RETRY_TIMES", times)
  43. retryTimes.Store(times)
  44. }
  45. func GetLogStorageHours() int64 {
  46. return atomic.LoadInt64(&logStorageHours)
  47. }
  48. func SetLogStorageHours(hours int64) {
  49. hours = env.Int64("LOG_STORAGE_HOURS", hours)
  50. atomic.StoreInt64(&logStorageHours, hours)
  51. }
  52. func GetRetryLogStorageHours() int64 {
  53. return atomic.LoadInt64(&retryLogStorageHours)
  54. }
  55. func SetRetryLogStorageHours(hours int64) {
  56. hours = env.Int64("RETRY_LOG_STORAGE_HOURS", hours)
  57. atomic.StoreInt64(&retryLogStorageHours, hours)
  58. }
  59. func GetLogDetailStorageHours() int64 {
  60. return atomic.LoadInt64(&logDetailStorageHours)
  61. }
  62. func SetLogDetailStorageHours(hours int64) {
  63. hours = env.Int64("LOG_DETAIL_STORAGE_HOURS", hours)
  64. atomic.StoreInt64(&logDetailStorageHours, hours)
  65. }
  66. func GetCleanLogBatchSize() int64 {
  67. return atomic.LoadInt64(&cleanLogBatchSize)
  68. }
  69. func SetCleanLogBatchSize(size int64) {
  70. size = env.Int64("CLEAN_LOG_BATCH_SIZE", size)
  71. atomic.StoreInt64(&cleanLogBatchSize, size)
  72. }
  73. func GetIPGroupsThreshold() int64 {
  74. return atomic.LoadInt64(&ipGroupsThreshold)
  75. }
  76. func SetIPGroupsThreshold(threshold int64) {
  77. threshold = env.Int64("IP_GROUPS_THRESHOLD", threshold)
  78. atomic.StoreInt64(&ipGroupsThreshold, threshold)
  79. }
  80. func GetIPGroupsBanThreshold() int64 {
  81. return atomic.LoadInt64(&ipGroupsBanThreshold)
  82. }
  83. func SetIPGroupsBanThreshold(threshold int64) {
  84. threshold = env.Int64("IP_GROUPS_BAN_THRESHOLD", threshold)
  85. atomic.StoreInt64(&ipGroupsBanThreshold, threshold)
  86. }
  87. func GetSaveAllLogDetail() bool {
  88. return saveAllLogDetail.Load()
  89. }
  90. func SetSaveAllLogDetail(enabled bool) {
  91. enabled = env.Bool("SAVE_ALL_LOG_DETAIL", enabled)
  92. saveAllLogDetail.Store(enabled)
  93. }
  94. func GetLogDetailRequestBodyMaxSize() int64 {
  95. return atomic.LoadInt64(&logDetailRequestBodyMaxSize)
  96. }
  97. func SetLogDetailRequestBodyMaxSize(size int64) {
  98. size = env.Int64("LOG_DETAIL_REQUEST_BODY_MAX_SIZE", size)
  99. atomic.StoreInt64(&logDetailRequestBodyMaxSize, size)
  100. }
  101. func GetLogDetailResponseBodyMaxSize() int64 {
  102. return atomic.LoadInt64(&logDetailResponseBodyMaxSize)
  103. }
  104. func SetLogDetailResponseBodyMaxSize(size int64) {
  105. size = env.Int64("LOG_DETAIL_RESPONSE_BODY_MAX_SIZE", size)
  106. atomic.StoreInt64(&logDetailResponseBodyMaxSize, size)
  107. }
  108. func GetDisableServe() bool {
  109. return disableServe.Load()
  110. }
  111. func SetDisableServe(disabled bool) {
  112. disabled = env.Bool("DISABLE_SERVE", disabled)
  113. disableServe.Store(disabled)
  114. }
  115. func GetDefaultChannelModels() map[int][]string {
  116. d, _ := defaultChannelModels.Load().(map[int][]string)
  117. return d
  118. }
  119. func SetDefaultChannelModels(models map[int][]string) {
  120. models = env.JSON("DEFAULT_CHANNEL_MODELS", models)
  121. for key, ms := range models {
  122. slices.Sort(ms)
  123. models[key] = slices.Compact(ms)
  124. }
  125. defaultChannelModels.Store(models)
  126. }
  127. func GetDefaultChannelModelMapping() map[int]map[string]string {
  128. d, _ := defaultChannelModelMapping.Load().(map[int]map[string]string)
  129. return d
  130. }
  131. func SetDefaultChannelModelMapping(mapping map[int]map[string]string) {
  132. mapping = env.JSON("DEFAULT_CHANNEL_MODEL_MAPPING", mapping)
  133. defaultChannelModelMapping.Store(mapping)
  134. }
  135. func GetGroupConsumeLevelRatio() map[float64]float64 {
  136. r, _ := groupConsumeLevelRatio.Load().(map[float64]float64)
  137. return r
  138. }
  139. func GetGroupConsumeLevelRatioStringKeyMap() map[string]float64 {
  140. ratio := GetGroupConsumeLevelRatio()
  141. stringMap := make(map[string]float64)
  142. for k, v := range ratio {
  143. stringMap[strconv.FormatFloat(k, 'f', -1, 64)] = v
  144. }
  145. return stringMap
  146. }
  147. func SetGroupConsumeLevelRatio(ratio map[float64]float64) {
  148. ratio = env.JSON("GROUP_CONSUME_LEVEL_RATIO", ratio)
  149. groupConsumeLevelRatio.Store(ratio)
  150. }
  151. // GetGroupMaxTokenNum returns max number of tokens per group, 0 means unlimited
  152. func GetGroupMaxTokenNum() int64 {
  153. return groupMaxTokenNum.Load()
  154. }
  155. func SetGroupMaxTokenNum(num int64) {
  156. num = env.Int64("GROUP_MAX_TOKEN_NUM", num)
  157. groupMaxTokenNum.Store(num)
  158. }
  159. func GetNotifyNote() string {
  160. n, _ := notifyNote.Load().(string)
  161. return n
  162. }
  163. func SetNotifyNote(note string) {
  164. note = env.String("NOTIFY_NOTE", note)
  165. notifyNote.Store(note)
  166. }
  167. func GetDefaultMCPHost() string {
  168. h, _ := defaultMCPHost.Load().(string)
  169. return h
  170. }
  171. func SetDefaultMCPHost(host string) {
  172. host = env.String("DEFAULT_MCP_HOST", host)
  173. defaultMCPHost.Store(host)
  174. }
  175. func GetPublicMCPHost() string {
  176. h, _ := publicMCPHost.Load().(string)
  177. return h
  178. }
  179. func SetPublicMCPHost(host string) {
  180. host = env.String("PUBLIC_MCP_HOST", host)
  181. publicMCPHost.Store(host)
  182. }
  183. func GetGroupMCPHost() string {
  184. h, _ := groupMCPHost.Load().(string)
  185. return h
  186. }
  187. func SetGroupMCPHost(host string) {
  188. host = env.String("GROUP_MCP_HOST", host)
  189. groupMCPHost.Store(host)
  190. }