channel-billing.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.ChannelTypeOpenAI:
  38. // do nothing
  39. case common.ChannelTypeAzure:
  40. return 0, errors.New("尚未实现")
  41. case common.ChannelTypeCustom:
  42. baseURL = channel.BaseURL
  43. default:
  44. return 0, errors.New("尚未实现")
  45. }
  46. url := fmt.Sprintf("%s/v1/dashboard/billing/subscription", baseURL)
  47. client := &http.Client{}
  48. req, err := http.NewRequest("GET", url, nil)
  49. if err != nil {
  50. return 0, err
  51. }
  52. auth := fmt.Sprintf("Bearer %s", channel.Key)
  53. req.Header.Add("Authorization", auth)
  54. res, err := client.Do(req)
  55. if err != nil {
  56. return 0, err
  57. }
  58. body, err := io.ReadAll(res.Body)
  59. if err != nil {
  60. return 0, err
  61. }
  62. err = res.Body.Close()
  63. if err != nil {
  64. return 0, err
  65. }
  66. subscription := OpenAISubscriptionResponse{}
  67. err = json.Unmarshal(body, &subscription)
  68. if err != nil {
  69. return 0, err
  70. }
  71. now := time.Now()
  72. startDate := fmt.Sprintf("%s-01", now.Format("2006-01"))
  73. endDate := now.Format("2006-01-02")
  74. if !subscription.HasPaymentMethod {
  75. startDate = now.AddDate(0, 0, -100).Format("2006-01-02")
  76. }
  77. url = fmt.Sprintf("%s/v1/dashboard/billing/usage?start_date=%s&end_date=%s", baseURL, startDate, endDate)
  78. req, err = http.NewRequest("GET", url, nil)
  79. if err != nil {
  80. return 0, err
  81. }
  82. req.Header.Add("Authorization", auth)
  83. res, err = client.Do(req)
  84. if err != nil {
  85. return 0, err
  86. }
  87. body, err = io.ReadAll(res.Body)
  88. if err != nil {
  89. return 0, err
  90. }
  91. err = res.Body.Close()
  92. if err != nil {
  93. return 0, err
  94. }
  95. usage := OpenAIUsageResponse{}
  96. err = json.Unmarshal(body, &usage)
  97. if err != nil {
  98. return 0, err
  99. }
  100. balance := subscription.HardLimitUSD - usage.TotalUsage/100
  101. channel.UpdateBalance(balance)
  102. return balance, nil
  103. }
  104. func UpdateChannelBalance(c *gin.Context) {
  105. id, err := strconv.Atoi(c.Param("id"))
  106. if err != nil {
  107. c.JSON(http.StatusOK, gin.H{
  108. "success": false,
  109. "message": err.Error(),
  110. })
  111. return
  112. }
  113. channel, err := model.GetChannelById(id, true)
  114. if err != nil {
  115. c.JSON(http.StatusOK, gin.H{
  116. "success": false,
  117. "message": err.Error(),
  118. })
  119. return
  120. }
  121. balance, err := updateChannelBalance(channel)
  122. if err != nil {
  123. c.JSON(http.StatusOK, gin.H{
  124. "success": false,
  125. "message": err.Error(),
  126. })
  127. return
  128. }
  129. c.JSON(http.StatusOK, gin.H{
  130. "success": true,
  131. "message": "",
  132. "balance": balance,
  133. })
  134. return
  135. }
  136. func updateAllChannelsBalance() error {
  137. channels, err := model.GetAllChannels(0, 0, true)
  138. if err != nil {
  139. return err
  140. }
  141. for _, channel := range channels {
  142. if channel.Status != common.ChannelStatusEnabled {
  143. continue
  144. }
  145. // TODO: support Azure
  146. if channel.Type != common.ChannelTypeOpenAI && channel.Type != common.ChannelTypeCustom {
  147. continue
  148. }
  149. balance, err := updateChannelBalance(channel)
  150. if err != nil {
  151. continue
  152. } else {
  153. // err is nil & balance <= 0 means quota is used up
  154. if balance <= 0 {
  155. disableChannel(channel.Id, channel.Name, "余额不足")
  156. }
  157. }
  158. }
  159. return nil
  160. }
  161. func UpdateAllChannelsBalance(c *gin.Context) {
  162. // TODO: make it async
  163. err := updateAllChannelsBalance()
  164. if err != nil {
  165. c.JSON(http.StatusOK, gin.H{
  166. "success": false,
  167. "message": err.Error(),
  168. })
  169. return
  170. }
  171. c.JSON(http.StatusOK, gin.H{
  172. "success": true,
  173. "message": "",
  174. })
  175. return
  176. }