config.go 6.2 KB

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