constants.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package common
  2. import (
  3. "os"
  4. "strconv"
  5. "sync"
  6. "time"
  7. "github.com/google/uuid"
  8. )
  9. var StartTime = time.Now().Unix() // unit: second
  10. var Version = "v0.0.0" // this hard coding will be replaced automatically when building, no need to manually change
  11. var SystemName = "New API"
  12. var Footer = ""
  13. var Logo = ""
  14. var TopUpLink = ""
  15. // var ChatLink = ""
  16. // var ChatLink2 = ""
  17. var QuotaPerUnit = 500 * 1000.0 // $0.002 / 1K tokens
  18. var DisplayInCurrencyEnabled = true
  19. var DisplayTokenStatEnabled = true
  20. var DrawingEnabled = true
  21. var TaskEnabled = true
  22. var DataExportEnabled = true
  23. var DataExportInterval = 5 // unit: minute
  24. var DataExportDefaultTime = "hour" // unit: minute
  25. var DefaultCollapseSidebar = false // default value of collapse sidebar
  26. // Any options with "Secret", "Token" in its key won't be return by GetOptions
  27. var SessionSecret = uuid.New().String()
  28. var CryptoSecret = uuid.New().String()
  29. var OptionMap map[string]string
  30. var OptionMapRWMutex sync.RWMutex
  31. var ItemsPerPage = 10
  32. var MaxRecentItems = 100
  33. var PasswordLoginEnabled = true
  34. var PasswordRegisterEnabled = true
  35. var EmailVerificationEnabled = false
  36. var GitHubOAuthEnabled = false
  37. var OIDCEnabled = false
  38. var LinuxDOOAuthEnabled = false
  39. var WeChatAuthEnabled = false
  40. var TelegramOAuthEnabled = false
  41. var TurnstileCheckEnabled = false
  42. var RegisterEnabled = true
  43. var EmailDomainRestrictionEnabled = false // 是否启用邮箱域名限制
  44. var EmailAliasRestrictionEnabled = false // 是否启用邮箱别名限制
  45. var EmailDomainWhitelist = []string{
  46. "gmail.com",
  47. "163.com",
  48. "126.com",
  49. "qq.com",
  50. "outlook.com",
  51. "hotmail.com",
  52. "icloud.com",
  53. "yahoo.com",
  54. "foxmail.com",
  55. }
  56. var DebugEnabled = os.Getenv("DEBUG") == "true"
  57. var MemoryCacheEnabled = os.Getenv("MEMORY_CACHE_ENABLED") == "true"
  58. var LogConsumeEnabled = true
  59. var SMTPServer = ""
  60. var SMTPPort = 587
  61. var SMTPSSLEnabled = false
  62. var SMTPAccount = ""
  63. var SMTPFrom = ""
  64. var SMTPToken = ""
  65. var GitHubClientId = ""
  66. var GitHubClientSecret = ""
  67. var OIDCClientId = ""
  68. var OIDCClientSecret = ""
  69. var OIDCWellKnown = ""
  70. var OIDCAuthorizationEndpoint = ""
  71. var OIDCTokenEndpoint = ""
  72. var OIDCUserInfoEndpoint = ""
  73. var LinuxDOClientId = ""
  74. var LinuxDOClientSecret = ""
  75. var WeChatServerAddress = ""
  76. var WeChatServerToken = ""
  77. var WeChatAccountQRCodeImageURL = ""
  78. var TurnstileSiteKey = ""
  79. var TurnstileSecretKey = ""
  80. var TelegramBotToken = ""
  81. var TelegramBotName = ""
  82. var QuotaForNewUser = 0
  83. var QuotaForInviter = 0
  84. var QuotaForInvitee = 0
  85. var ChannelDisableThreshold = 5.0
  86. var AutomaticDisableChannelEnabled = false
  87. var AutomaticEnableChannelEnabled = false
  88. var QuotaRemindThreshold = 1000
  89. var PreConsumedQuota = 500
  90. var RetryTimes = 0
  91. //var RootUserEmail = ""
  92. var IsMasterNode = os.Getenv("NODE_TYPE") != "slave"
  93. var requestInterval, _ = strconv.Atoi(os.Getenv("POLLING_INTERVAL"))
  94. var RequestInterval = time.Duration(requestInterval) * time.Second
  95. var SyncFrequency = GetEnvOrDefault("SYNC_FREQUENCY", 60) // unit is second
  96. var BatchUpdateEnabled = false
  97. var BatchUpdateInterval = GetEnvOrDefault("BATCH_UPDATE_INTERVAL", 5)
  98. var RelayTimeout = GetEnvOrDefault("RELAY_TIMEOUT", 0) // unit is second
  99. var GeminiSafetySetting = GetEnvOrDefaultString("GEMINI_SAFETY_SETTING", "BLOCK_NONE")
  100. // https://docs.cohere.com/docs/safety-modes Type; NONE/CONTEXTUAL/STRICT
  101. var CohereSafetySetting = GetEnvOrDefaultString("COHERE_SAFETY_SETTING", "NONE")
  102. const (
  103. RequestIdKey = "X-Oneapi-Request-Id"
  104. )
  105. const (
  106. RoleGuestUser = 0
  107. RoleCommonUser = 1
  108. RoleAdminUser = 10
  109. RoleRootUser = 100
  110. )
  111. func IsValidateRole(role int) bool {
  112. return role == RoleGuestUser || role == RoleCommonUser || role == RoleAdminUser || role == RoleRootUser
  113. }
  114. var (
  115. FileUploadPermission = RoleGuestUser
  116. FileDownloadPermission = RoleGuestUser
  117. ImageUploadPermission = RoleGuestUser
  118. ImageDownloadPermission = RoleGuestUser
  119. )
  120. // All duration's unit is seconds
  121. // Shouldn't larger then RateLimitKeyExpirationDuration
  122. var (
  123. GlobalApiRateLimitEnable = GetEnvOrDefaultBool("GLOBAL_API_RATE_LIMIT_ENABLE", true)
  124. GlobalApiRateLimitNum = GetEnvOrDefault("GLOBAL_API_RATE_LIMIT", 180)
  125. GlobalApiRateLimitDuration = int64(GetEnvOrDefault("GLOBAL_API_RATE_LIMIT_DURATION", 180))
  126. GlobalWebRateLimitEnable = GetEnvOrDefaultBool("GLOBAL_WEB_RATE_LIMIT_ENABLE", true)
  127. GlobalWebRateLimitNum = GetEnvOrDefault("GLOBAL_WEB_RATE_LIMIT", 60)
  128. GlobalWebRateLimitDuration = int64(GetEnvOrDefault("GLOBAL_WEB_RATE_LIMIT_DURATION", 180))
  129. UploadRateLimitNum = 10
  130. UploadRateLimitDuration int64 = 60
  131. DownloadRateLimitNum = 10
  132. DownloadRateLimitDuration int64 = 60
  133. CriticalRateLimitNum = 20
  134. CriticalRateLimitDuration int64 = 20 * 60
  135. )
  136. var RateLimitKeyExpirationDuration = 20 * time.Minute
  137. const (
  138. UserStatusEnabled = 1 // don't use 0, 0 is the default value!
  139. UserStatusDisabled = 2 // also don't use 0
  140. )
  141. const (
  142. TokenStatusEnabled = 1 // don't use 0, 0 is the default value!
  143. TokenStatusDisabled = 2 // also don't use 0
  144. TokenStatusExpired = 3
  145. TokenStatusExhausted = 4
  146. )
  147. const (
  148. RedemptionCodeStatusEnabled = 1 // don't use 0, 0 is the default value!
  149. RedemptionCodeStatusDisabled = 2 // also don't use 0
  150. RedemptionCodeStatusUsed = 3 // also don't use 0
  151. )
  152. const (
  153. ChannelStatusUnknown = 0
  154. ChannelStatusEnabled = 1 // don't use 0, 0 is the default value!
  155. ChannelStatusManuallyDisabled = 2 // also don't use 0
  156. ChannelStatusAutoDisabled = 3
  157. )
  158. const (
  159. ChannelTypeUnknown = 0
  160. ChannelTypeOpenAI = 1
  161. ChannelTypeMidjourney = 2
  162. ChannelTypeAzure = 3
  163. ChannelTypeOllama = 4
  164. ChannelTypeMidjourneyPlus = 5
  165. ChannelTypeOpenAIMax = 6
  166. ChannelTypeOhMyGPT = 7
  167. ChannelTypeCustom = 8
  168. ChannelTypeAILS = 9
  169. ChannelTypeAIProxy = 10
  170. ChannelTypePaLM = 11
  171. ChannelTypeAPI2GPT = 12
  172. ChannelTypeAIGC2D = 13
  173. ChannelTypeAnthropic = 14
  174. ChannelTypeBaidu = 15
  175. ChannelTypeZhipu = 16
  176. ChannelTypeAli = 17
  177. ChannelTypeXunfei = 18
  178. ChannelType360 = 19
  179. ChannelTypeOpenRouter = 20
  180. ChannelTypeAIProxyLibrary = 21
  181. ChannelTypeFastGPT = 22
  182. ChannelTypeTencent = 23
  183. ChannelTypeGemini = 24
  184. ChannelTypeMoonshot = 25
  185. ChannelTypeZhipu_v4 = 26
  186. ChannelTypePerplexity = 27
  187. ChannelTypeLingYiWanWu = 31
  188. ChannelTypeAws = 33
  189. ChannelTypeCohere = 34
  190. ChannelTypeMiniMax = 35
  191. ChannelTypeSunoAPI = 36
  192. ChannelTypeDify = 37
  193. ChannelTypeJina = 38
  194. ChannelCloudflare = 39
  195. ChannelTypeSiliconFlow = 40
  196. ChannelTypeVertexAi = 41
  197. ChannelTypeMistral = 42
  198. ChannelTypeDeepSeek = 43
  199. ChannelTypeMokaAI = 44
  200. ChannelTypeVolcEngine = 45
  201. ChannelTypeBaiduV2 = 46
  202. ChannelTypeDummy // this one is only for count, do not add any channel after this
  203. )
  204. var ChannelBaseURLs = []string{
  205. "", // 0
  206. "https://api.openai.com", // 1
  207. "https://oa.api2d.net", // 2
  208. "", // 3
  209. "http://localhost:11434", // 4
  210. "https://api.openai-sb.com", // 5
  211. "https://api.openaimax.com", // 6
  212. "https://api.ohmygpt.com", // 7
  213. "", // 8
  214. "https://api.caipacity.com", // 9
  215. "https://api.aiproxy.io", // 10
  216. "", // 11
  217. "https://api.api2gpt.com", // 12
  218. "https://api.aigc2d.com", // 13
  219. "https://api.anthropic.com", // 14
  220. "https://aip.baidubce.com", // 15
  221. "https://open.bigmodel.cn", // 16
  222. "https://dashscope.aliyuncs.com", // 17
  223. "", // 18
  224. "https://api.360.cn", // 19
  225. "https://openrouter.ai/api", // 20
  226. "https://api.aiproxy.io", // 21
  227. "https://fastgpt.run/api/openapi", // 22
  228. "https://hunyuan.tencentcloudapi.com", //23
  229. "https://generativelanguage.googleapis.com", //24
  230. "https://api.moonshot.cn", //25
  231. "https://open.bigmodel.cn", //26
  232. "https://api.perplexity.ai", //27
  233. "", //28
  234. "", //29
  235. "", //30
  236. "https://api.lingyiwanwu.com", //31
  237. "", //32
  238. "", //33
  239. "https://api.cohere.ai", //34
  240. "https://api.minimax.chat", //35
  241. "", //36
  242. "https://api.dify.ai", //37
  243. "https://api.jina.ai", //38
  244. "https://api.cloudflare.com", //39
  245. "https://api.siliconflow.cn", //40
  246. "", //41
  247. "https://api.mistral.ai", //42
  248. "https://api.deepseek.com", //43
  249. "https://api.moka.ai", //44
  250. "https://ark.cn-beijing.volces.com", //45
  251. "https://qianfan.baidubce.com", //46
  252. }