channel-billing.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. package controller
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "one-api/common"
  9. "one-api/model"
  10. "one-api/service"
  11. "strconv"
  12. "time"
  13. "github.com/gin-gonic/gin"
  14. )
  15. // https://github.com/songquanpeng/one-api/issues/79
  16. type OpenAISubscriptionResponse struct {
  17. Object string `json:"object"`
  18. HasPaymentMethod bool `json:"has_payment_method"`
  19. SoftLimitUSD float64 `json:"soft_limit_usd"`
  20. HardLimitUSD float64 `json:"hard_limit_usd"`
  21. SystemHardLimitUSD float64 `json:"system_hard_limit_usd"`
  22. AccessUntil int64 `json:"access_until"`
  23. }
  24. type OpenAIUsageDailyCost struct {
  25. Timestamp float64 `json:"timestamp"`
  26. LineItems []struct {
  27. Name string `json:"name"`
  28. Cost float64 `json:"cost"`
  29. }
  30. }
  31. type OpenAICreditGrants struct {
  32. Object string `json:"object"`
  33. TotalGranted float64 `json:"total_granted"`
  34. TotalUsed float64 `json:"total_used"`
  35. TotalAvailable float64 `json:"total_available"`
  36. }
  37. type OpenAIUsageResponse struct {
  38. Object string `json:"object"`
  39. //DailyCosts []OpenAIUsageDailyCost `json:"daily_costs"`
  40. TotalUsage float64 `json:"total_usage"` // unit: 0.01 dollar
  41. }
  42. type OpenAISBUsageResponse struct {
  43. Msg string `json:"msg"`
  44. Data *struct {
  45. Credit string `json:"credit"`
  46. } `json:"data"`
  47. }
  48. type AIProxyUserOverviewResponse struct {
  49. Success bool `json:"success"`
  50. Message string `json:"message"`
  51. ErrorCode int `json:"error_code"`
  52. Data struct {
  53. TotalPoints float64 `json:"totalPoints"`
  54. } `json:"data"`
  55. }
  56. type API2GPTUsageResponse struct {
  57. Object string `json:"object"`
  58. TotalGranted float64 `json:"total_granted"`
  59. TotalUsed float64 `json:"total_used"`
  60. TotalRemaining float64 `json:"total_remaining"`
  61. }
  62. type APGC2DGPTUsageResponse struct {
  63. //Grants interface{} `json:"grants"`
  64. Object string `json:"object"`
  65. TotalAvailable float64 `json:"total_available"`
  66. TotalGranted float64 `json:"total_granted"`
  67. TotalUsed float64 `json:"total_used"`
  68. }
  69. type SiliconFlowUsageResponse struct {
  70. Code int `json:"code"`
  71. Message string `json:"message"`
  72. Status bool `json:"status"`
  73. Data struct {
  74. ID string `json:"id"`
  75. Name string `json:"name"`
  76. Image string `json:"image"`
  77. Email string `json:"email"`
  78. IsAdmin bool `json:"isAdmin"`
  79. Balance string `json:"balance"`
  80. Status string `json:"status"`
  81. Introduction string `json:"introduction"`
  82. Role string `json:"role"`
  83. ChargeBalance string `json:"chargeBalance"`
  84. TotalBalance string `json:"totalBalance"`
  85. Category string `json:"category"`
  86. } `json:"data"`
  87. }
  88. type DeepSeekUsageResponse struct {
  89. IsAvailable bool `json:"is_available"`
  90. BalanceInfos []struct {
  91. Currency string `json:"currency"`
  92. TotalBalance string `json:"total_balance"`
  93. GrantedBalance string `json:"granted_balance"`
  94. ToppedUpBalance string `json:"topped_up_balance"`
  95. } `json:"balance_infos"`
  96. }
  97. // GetAuthHeader get auth header
  98. func GetAuthHeader(token string) http.Header {
  99. h := http.Header{}
  100. h.Add("Authorization", fmt.Sprintf("Bearer %s", token))
  101. return h
  102. }
  103. func GetResponseBody(method, url string, channel *model.Channel, headers http.Header) ([]byte, error) {
  104. req, err := http.NewRequest(method, url, nil)
  105. if err != nil {
  106. return nil, err
  107. }
  108. for k := range headers {
  109. req.Header.Add(k, headers.Get(k))
  110. }
  111. res, err := service.GetHttpClient().Do(req)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if res.StatusCode != http.StatusOK {
  116. return nil, fmt.Errorf("status code: %d", res.StatusCode)
  117. }
  118. body, err := io.ReadAll(res.Body)
  119. if err != nil {
  120. return nil, err
  121. }
  122. err = res.Body.Close()
  123. if err != nil {
  124. return nil, err
  125. }
  126. return body, nil
  127. }
  128. func updateChannelCloseAIBalance(channel *model.Channel) (float64, error) {
  129. url := fmt.Sprintf("%s/dashboard/billing/credit_grants", channel.GetBaseURL())
  130. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  131. if err != nil {
  132. return 0, err
  133. }
  134. response := OpenAICreditGrants{}
  135. err = json.Unmarshal(body, &response)
  136. if err != nil {
  137. return 0, err
  138. }
  139. channel.UpdateBalance(response.TotalAvailable)
  140. return response.TotalAvailable, nil
  141. }
  142. func updateChannelOpenAISBBalance(channel *model.Channel) (float64, error) {
  143. url := fmt.Sprintf("https://api.openai-sb.com/sb-api/user/status?api_key=%s", channel.Key)
  144. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  145. if err != nil {
  146. return 0, err
  147. }
  148. response := OpenAISBUsageResponse{}
  149. err = json.Unmarshal(body, &response)
  150. if err != nil {
  151. return 0, err
  152. }
  153. if response.Data == nil {
  154. return 0, errors.New(response.Msg)
  155. }
  156. balance, err := strconv.ParseFloat(response.Data.Credit, 64)
  157. if err != nil {
  158. return 0, err
  159. }
  160. channel.UpdateBalance(balance)
  161. return balance, nil
  162. }
  163. func updateChannelAIProxyBalance(channel *model.Channel) (float64, error) {
  164. url := "https://aiproxy.io/api/report/getUserOverview"
  165. headers := http.Header{}
  166. headers.Add("Api-Key", channel.Key)
  167. body, err := GetResponseBody("GET", url, channel, headers)
  168. if err != nil {
  169. return 0, err
  170. }
  171. response := AIProxyUserOverviewResponse{}
  172. err = json.Unmarshal(body, &response)
  173. if err != nil {
  174. return 0, err
  175. }
  176. if !response.Success {
  177. return 0, fmt.Errorf("code: %d, message: %s", response.ErrorCode, response.Message)
  178. }
  179. channel.UpdateBalance(response.Data.TotalPoints)
  180. return response.Data.TotalPoints, nil
  181. }
  182. func updateChannelAPI2GPTBalance(channel *model.Channel) (float64, error) {
  183. url := "https://api.api2gpt.com/dashboard/billing/credit_grants"
  184. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  185. if err != nil {
  186. return 0, err
  187. }
  188. response := API2GPTUsageResponse{}
  189. err = json.Unmarshal(body, &response)
  190. if err != nil {
  191. return 0, err
  192. }
  193. channel.UpdateBalance(response.TotalRemaining)
  194. return response.TotalRemaining, nil
  195. }
  196. func updateChannelSiliconFlowBalance(channel *model.Channel) (float64, error) {
  197. url := "https://api.siliconflow.cn/v1/user/info"
  198. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  199. if err != nil {
  200. return 0, err
  201. }
  202. response := SiliconFlowUsageResponse{}
  203. err = json.Unmarshal(body, &response)
  204. if err != nil {
  205. return 0, err
  206. }
  207. if response.Code != 20000 {
  208. return 0, fmt.Errorf("code: %d, message: %s", response.Code, response.Message)
  209. }
  210. balance, err := strconv.ParseFloat(response.Data.TotalBalance, 64)
  211. if err != nil {
  212. return 0, err
  213. }
  214. channel.UpdateBalance(balance)
  215. return balance, nil
  216. }
  217. func updateChannelDeepSeekBalance(channel *model.Channel) (float64, error) {
  218. url := "https://api.deepseek.com/user/balance"
  219. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  220. if err != nil {
  221. return 0, err
  222. }
  223. response := DeepSeekUsageResponse{}
  224. err = json.Unmarshal(body, &response)
  225. if err != nil {
  226. return 0, err
  227. }
  228. index := -1
  229. for i, balanceInfo := range response.BalanceInfos {
  230. if balanceInfo.Currency == "CNY" {
  231. index = i
  232. break
  233. }
  234. }
  235. if index == -1 {
  236. return 0, errors.New("currency CNY not found")
  237. }
  238. balance, err := strconv.ParseFloat(response.BalanceInfos[index].TotalBalance, 64)
  239. if err != nil {
  240. return 0, err
  241. }
  242. channel.UpdateBalance(balance)
  243. return balance, nil
  244. }
  245. func updateChannelAIGC2DBalance(channel *model.Channel) (float64, error) {
  246. url := "https://api.aigc2d.com/dashboard/billing/credit_grants"
  247. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  248. if err != nil {
  249. return 0, err
  250. }
  251. response := APGC2DGPTUsageResponse{}
  252. err = json.Unmarshal(body, &response)
  253. if err != nil {
  254. return 0, err
  255. }
  256. channel.UpdateBalance(response.TotalAvailable)
  257. return response.TotalAvailable, nil
  258. }
  259. func updateChannelBalance(channel *model.Channel) (float64, error) {
  260. baseURL := common.ChannelBaseURLs[channel.Type]
  261. if channel.GetBaseURL() == "" {
  262. channel.BaseURL = &baseURL
  263. }
  264. switch channel.Type {
  265. case common.ChannelTypeOpenAI:
  266. if channel.GetBaseURL() != "" {
  267. baseURL = channel.GetBaseURL()
  268. }
  269. case common.ChannelTypeAzure:
  270. return 0, errors.New("尚未实现")
  271. case common.ChannelTypeCustom:
  272. baseURL = channel.GetBaseURL()
  273. //case common.ChannelTypeOpenAISB:
  274. // return updateChannelOpenAISBBalance(channel)
  275. case common.ChannelTypeAIProxy:
  276. return updateChannelAIProxyBalance(channel)
  277. case common.ChannelTypeAPI2GPT:
  278. return updateChannelAPI2GPTBalance(channel)
  279. case common.ChannelTypeAIGC2D:
  280. return updateChannelAIGC2DBalance(channel)
  281. case common.ChannelTypeSiliconFlow:
  282. return updateChannelSiliconFlowBalance(channel)
  283. case common.ChannelTypeDeepSeek:
  284. return updateChannelDeepSeekBalance(channel)
  285. default:
  286. return 0, errors.New("尚未实现")
  287. }
  288. url := fmt.Sprintf("%s/v1/dashboard/billing/subscription", baseURL)
  289. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  290. if err != nil {
  291. return 0, err
  292. }
  293. subscription := OpenAISubscriptionResponse{}
  294. err = json.Unmarshal(body, &subscription)
  295. if err != nil {
  296. return 0, err
  297. }
  298. now := time.Now()
  299. startDate := fmt.Sprintf("%s-01", now.Format("2006-01"))
  300. endDate := now.Format("2006-01-02")
  301. if !subscription.HasPaymentMethod {
  302. startDate = now.AddDate(0, 0, -100).Format("2006-01-02")
  303. }
  304. url = fmt.Sprintf("%s/v1/dashboard/billing/usage?start_date=%s&end_date=%s", baseURL, startDate, endDate)
  305. body, err = GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  306. if err != nil {
  307. return 0, err
  308. }
  309. usage := OpenAIUsageResponse{}
  310. err = json.Unmarshal(body, &usage)
  311. if err != nil {
  312. return 0, err
  313. }
  314. balance := subscription.HardLimitUSD - usage.TotalUsage/100
  315. channel.UpdateBalance(balance)
  316. return balance, nil
  317. }
  318. func UpdateChannelBalance(c *gin.Context) {
  319. id, err := strconv.Atoi(c.Param("id"))
  320. if err != nil {
  321. c.JSON(http.StatusOK, gin.H{
  322. "success": false,
  323. "message": err.Error(),
  324. })
  325. return
  326. }
  327. channel, err := model.GetChannelById(id, true)
  328. if err != nil {
  329. c.JSON(http.StatusOK, gin.H{
  330. "success": false,
  331. "message": err.Error(),
  332. })
  333. return
  334. }
  335. balance, err := updateChannelBalance(channel)
  336. if err != nil {
  337. c.JSON(http.StatusOK, gin.H{
  338. "success": false,
  339. "message": err.Error(),
  340. })
  341. return
  342. }
  343. c.JSON(http.StatusOK, gin.H{
  344. "success": true,
  345. "message": "",
  346. "balance": balance,
  347. })
  348. return
  349. }
  350. func updateAllChannelsBalance() error {
  351. channels, err := model.GetAllChannels(0, 0, true, false)
  352. if err != nil {
  353. return err
  354. }
  355. for _, channel := range channels {
  356. if channel.Status != common.ChannelStatusEnabled {
  357. continue
  358. }
  359. // TODO: support Azure
  360. //if channel.Type != common.ChannelTypeOpenAI && channel.Type != common.ChannelTypeCustom {
  361. // continue
  362. //}
  363. balance, err := updateChannelBalance(channel)
  364. if err != nil {
  365. continue
  366. } else {
  367. // err is nil & balance <= 0 means quota is used up
  368. if balance <= 0 {
  369. service.DisableChannel(channel.Id, channel.Name, "余额不足")
  370. }
  371. }
  372. time.Sleep(common.RequestInterval)
  373. }
  374. return nil
  375. }
  376. func UpdateAllChannelsBalance(c *gin.Context) {
  377. // TODO: make it async
  378. err := updateAllChannelsBalance()
  379. if err != nil {
  380. c.JSON(http.StatusOK, gin.H{
  381. "success": false,
  382. "message": err.Error(),
  383. })
  384. return
  385. }
  386. c.JSON(http.StatusOK, gin.H{
  387. "success": true,
  388. "message": "",
  389. })
  390. return
  391. }
  392. func AutomaticallyUpdateChannels(frequency int) {
  393. for {
  394. time.Sleep(time.Duration(frequency) * time.Minute)
  395. common.SysLog("updating all channels")
  396. _ = updateAllChannelsBalance()
  397. common.SysLog("channels update done")
  398. }
  399. }