channel-billing.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package controller
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "io"
  8. "net/http"
  9. "one-api/common"
  10. "one-api/model"
  11. "strconv"
  12. "time"
  13. )
  14. // https://github.com/songquanpeng/one-api/issues/79
  15. type OpenAISubscriptionResponse struct {
  16. Object string `json:"object"`
  17. HasPaymentMethod bool `json:"has_payment_method"`
  18. SoftLimitUSD float64 `json:"soft_limit_usd"`
  19. HardLimitUSD float64 `json:"hard_limit_usd"`
  20. SystemHardLimitUSD float64 `json:"system_hard_limit_usd"`
  21. }
  22. type OpenAIUsageDailyCost struct {
  23. Timestamp float64 `json:"timestamp"`
  24. LineItems []struct {
  25. Name string `json:"name"`
  26. Cost float64 `json:"cost"`
  27. }
  28. }
  29. type OpenAIUsageResponse struct {
  30. Object string `json:"object"`
  31. //DailyCosts []OpenAIUsageDailyCost `json:"daily_costs"`
  32. TotalUsage float64 `json:"total_usage"` // unit: 0.01 dollar
  33. }
  34. func updateChannelBalance(channel *model.Channel) (float64, error) {
  35. baseURL := common.ChannelBaseURLs[channel.Type]
  36. switch channel.Type {
  37. case common.ChannelTypeAzure:
  38. return 0, errors.New("尚未实现")
  39. case common.ChannelTypeCustom:
  40. baseURL = channel.BaseURL
  41. }
  42. url := fmt.Sprintf("%s/v1/dashboard/billing/subscription", baseURL)
  43. client := &http.Client{}
  44. req, err := http.NewRequest("GET", url, nil)
  45. if err != nil {
  46. return 0, err
  47. }
  48. auth := fmt.Sprintf("Bearer %s", channel.Key)
  49. req.Header.Add("Authorization", auth)
  50. res, err := client.Do(req)
  51. if err != nil {
  52. return 0, err
  53. }
  54. body, err := io.ReadAll(res.Body)
  55. if err != nil {
  56. return 0, err
  57. }
  58. err = res.Body.Close()
  59. if err != nil {
  60. return 0, err
  61. }
  62. subscription := OpenAISubscriptionResponse{}
  63. err = json.Unmarshal(body, &subscription)
  64. if err != nil {
  65. return 0, err
  66. }
  67. now := time.Now()
  68. startDate := fmt.Sprintf("%s-01", now.Format("2006-01"))
  69. endDate := now.Format("2006-01-02")
  70. if !subscription.HasPaymentMethod {
  71. startDate = now.AddDate(0, 0, -100).Format("2006-01-02")
  72. }
  73. url = fmt.Sprintf("%s/v1/dashboard/billing/usage?start_date=%s&end_date=%s", baseURL, startDate, endDate)
  74. req, err = http.NewRequest("GET", url, nil)
  75. if err != nil {
  76. return 0, err
  77. }
  78. req.Header.Add("Authorization", auth)
  79. res, err = client.Do(req)
  80. if err != nil {
  81. return 0, err
  82. }
  83. body, err = io.ReadAll(res.Body)
  84. if err != nil {
  85. return 0, err
  86. }
  87. err = res.Body.Close()
  88. if err != nil {
  89. return 0, err
  90. }
  91. usage := OpenAIUsageResponse{}
  92. err = json.Unmarshal(body, &usage)
  93. if err != nil {
  94. return 0, err
  95. }
  96. balance := subscription.HardLimitUSD - usage.TotalUsage/100
  97. channel.UpdateBalance(balance)
  98. return balance, nil
  99. }
  100. func UpdateChannelBalance(c *gin.Context) {
  101. id, err := strconv.Atoi(c.Param("id"))
  102. if err != nil {
  103. c.JSON(http.StatusOK, gin.H{
  104. "success": false,
  105. "message": err.Error(),
  106. })
  107. return
  108. }
  109. channel, err := model.GetChannelById(id, true)
  110. if err != nil {
  111. c.JSON(http.StatusOK, gin.H{
  112. "success": false,
  113. "message": err.Error(),
  114. })
  115. return
  116. }
  117. balance, err := updateChannelBalance(channel)
  118. if err != nil {
  119. c.JSON(http.StatusOK, gin.H{
  120. "success": false,
  121. "message": err.Error(),
  122. })
  123. return
  124. }
  125. c.JSON(http.StatusOK, gin.H{
  126. "success": true,
  127. "message": "",
  128. "balance": balance,
  129. })
  130. return
  131. }
  132. func updateAllChannelsBalance() error {
  133. channels, err := model.GetAllChannels(0, 0, true)
  134. if err != nil {
  135. return err
  136. }
  137. for _, channel := range channels {
  138. if channel.Status != common.ChannelStatusEnabled {
  139. continue
  140. }
  141. // TODO: support Azure
  142. if channel.Type != common.ChannelTypeOpenAI && channel.Type != common.ChannelTypeCustom {
  143. continue
  144. }
  145. balance, err := updateChannelBalance(channel)
  146. if err != nil {
  147. continue
  148. } else {
  149. // err is nil & balance <= 0 means quota is used up
  150. if balance <= 0 {
  151. disableChannel(channel.Id, channel.Name, "余额不足")
  152. }
  153. }
  154. }
  155. return nil
  156. }
  157. func UpdateAllChannelsBalance(c *gin.Context) {
  158. // TODO: make it async
  159. err := updateAllChannelsBalance()
  160. if err != nil {
  161. c.JSON(http.StatusOK, gin.H{
  162. "success": false,
  163. "message": err.Error(),
  164. })
  165. return
  166. }
  167. c.JSON(http.StatusOK, gin.H{
  168. "success": true,
  169. "message": "",
  170. })
  171. return
  172. }