channel-billing.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. "strconv"
  11. "time"
  12. "github.com/gin-gonic/gin"
  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. type OpenAISBUsageResponse struct {
  35. Msg string `json:"msg"`
  36. Data *struct {
  37. Credit string `json:"credit"`
  38. } `json:"data"`
  39. }
  40. type AIProxyUserOverviewResponse struct {
  41. Success bool `json:"success"`
  42. Message string `json:"message"`
  43. ErrorCode int `json:"error_code"`
  44. Data struct {
  45. TotalPoints float64 `json:"totalPoints"`
  46. } `json:"data"`
  47. }
  48. type API2GPTUsageResponse struct {
  49. Object string `json:"object"`
  50. TotalGranted float64 `json:"total_granted"`
  51. TotalUsed float64 `json:"total_used"`
  52. TotalRemaining float64 `json:"total_remaining"`
  53. }
  54. // GetAuthHeader get auth header
  55. func GetAuthHeader(token string) http.Header {
  56. h := http.Header{}
  57. h.Add("Authorization", fmt.Sprintf("Bearer %s", token))
  58. return h
  59. }
  60. func GetResponseBody(method, url string, channel *model.Channel, headers http.Header) ([]byte, error) {
  61. client := &http.Client{}
  62. req, err := http.NewRequest(method, url, nil)
  63. if err != nil {
  64. return nil, err
  65. }
  66. for k := range headers {
  67. req.Header.Add(k, headers.Get(k))
  68. }
  69. res, err := client.Do(req)
  70. if err != nil {
  71. return nil, err
  72. }
  73. body, err := io.ReadAll(res.Body)
  74. if err != nil {
  75. return nil, err
  76. }
  77. err = res.Body.Close()
  78. if err != nil {
  79. return nil, err
  80. }
  81. return body, nil
  82. }
  83. func updateChannelOpenAISBBalance(channel *model.Channel) (float64, error) {
  84. url := fmt.Sprintf("https://api.openai-sb.com/sb-api/user/status?api_key=%s", channel.Key)
  85. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  86. if err != nil {
  87. return 0, err
  88. }
  89. response := OpenAISBUsageResponse{}
  90. err = json.Unmarshal(body, &response)
  91. if err != nil {
  92. return 0, err
  93. }
  94. if response.Data == nil {
  95. return 0, errors.New(response.Msg)
  96. }
  97. balance, err := strconv.ParseFloat(response.Data.Credit, 64)
  98. if err != nil {
  99. return 0, err
  100. }
  101. channel.UpdateBalance(balance)
  102. return balance, nil
  103. }
  104. func updateChannelAIProxyBalance(channel *model.Channel) (float64, error) {
  105. url := "https://aiproxy.io/api/report/getUserOverview"
  106. headers := http.Header{}
  107. headers.Add("Api-Key", channel.Key)
  108. body, err := GetResponseBody("GET", url, channel, headers)
  109. if err != nil {
  110. return 0, err
  111. }
  112. response := AIProxyUserOverviewResponse{}
  113. err = json.Unmarshal(body, &response)
  114. if err != nil {
  115. return 0, err
  116. }
  117. if !response.Success {
  118. return 0, fmt.Errorf("code: %d, message: %s", response.ErrorCode, response.Message)
  119. }
  120. channel.UpdateBalance(response.Data.TotalPoints)
  121. return response.Data.TotalPoints, nil
  122. }
  123. func updateChannelAPI2GPTBalance(channel *model.Channel) (float64, error) {
  124. url := "https://api.api2gpt.com/dashboard/billing/credit_grants"
  125. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  126. if err != nil {
  127. return 0, err
  128. }
  129. response := API2GPTUsageResponse{}
  130. err = json.Unmarshal(body, &response)
  131. if err != nil {
  132. return 0, err
  133. }
  134. channel.UpdateBalance(response.TotalRemaining)
  135. return response.TotalRemaining, nil
  136. }
  137. func updateChannelBalance(channel *model.Channel) (float64, error) {
  138. baseURL := common.ChannelBaseURLs[channel.Type]
  139. switch channel.Type {
  140. case common.ChannelTypeOpenAI:
  141. if channel.BaseURL != "" {
  142. baseURL = channel.BaseURL
  143. }
  144. case common.ChannelTypeAzure:
  145. return 0, errors.New("尚未实现")
  146. case common.ChannelTypeCustom:
  147. baseURL = channel.BaseURL
  148. case common.ChannelTypeOpenAISB:
  149. return updateChannelOpenAISBBalance(channel)
  150. case common.ChannelTypeAIProxy:
  151. return updateChannelAIProxyBalance(channel)
  152. case common.ChannelTypeAPI2GPT:
  153. return updateChannelAPI2GPTBalance(channel)
  154. default:
  155. return 0, errors.New("尚未实现")
  156. }
  157. url := fmt.Sprintf("%s/v1/dashboard/billing/subscription", baseURL)
  158. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  159. if err != nil {
  160. return 0, err
  161. }
  162. subscription := OpenAISubscriptionResponse{}
  163. err = json.Unmarshal(body, &subscription)
  164. if err != nil {
  165. return 0, err
  166. }
  167. now := time.Now()
  168. startDate := fmt.Sprintf("%s-01", now.Format("2006-01"))
  169. endDate := now.Format("2006-01-02")
  170. if !subscription.HasPaymentMethod {
  171. startDate = now.AddDate(0, 0, -100).Format("2006-01-02")
  172. }
  173. url = fmt.Sprintf("%s/v1/dashboard/billing/usage?start_date=%s&end_date=%s", baseURL, startDate, endDate)
  174. body, err = GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  175. if err != nil {
  176. return 0, err
  177. }
  178. usage := OpenAIUsageResponse{}
  179. err = json.Unmarshal(body, &usage)
  180. if err != nil {
  181. return 0, err
  182. }
  183. balance := subscription.HardLimitUSD - usage.TotalUsage/100
  184. channel.UpdateBalance(balance)
  185. return balance, nil
  186. }
  187. func UpdateChannelBalance(c *gin.Context) {
  188. id, err := strconv.Atoi(c.Param("id"))
  189. if err != nil {
  190. c.JSON(http.StatusOK, gin.H{
  191. "success": false,
  192. "message": err.Error(),
  193. })
  194. return
  195. }
  196. channel, err := model.GetChannelById(id, true)
  197. if err != nil {
  198. c.JSON(http.StatusOK, gin.H{
  199. "success": false,
  200. "message": err.Error(),
  201. })
  202. return
  203. }
  204. balance, err := updateChannelBalance(channel)
  205. if err != nil {
  206. c.JSON(http.StatusOK, gin.H{
  207. "success": false,
  208. "message": err.Error(),
  209. })
  210. return
  211. }
  212. c.JSON(http.StatusOK, gin.H{
  213. "success": true,
  214. "message": "",
  215. "balance": balance,
  216. })
  217. return
  218. }
  219. func updateAllChannelsBalance() error {
  220. channels, err := model.GetAllChannels(0, 0, true)
  221. if err != nil {
  222. return err
  223. }
  224. for _, channel := range channels {
  225. if channel.Status != common.ChannelStatusEnabled {
  226. continue
  227. }
  228. // TODO: support Azure
  229. if channel.Type != common.ChannelTypeOpenAI && channel.Type != common.ChannelTypeCustom {
  230. continue
  231. }
  232. balance, err := updateChannelBalance(channel)
  233. if err != nil {
  234. continue
  235. } else {
  236. // err is nil & balance <= 0 means quota is used up
  237. if balance <= 0 {
  238. disableChannel(channel.Id, channel.Name, "余额不足")
  239. }
  240. }
  241. }
  242. return nil
  243. }
  244. func UpdateAllChannelsBalance(c *gin.Context) {
  245. // TODO: make it async
  246. err := updateAllChannelsBalance()
  247. if err != nil {
  248. c.JSON(http.StatusOK, gin.H{
  249. "success": false,
  250. "message": err.Error(),
  251. })
  252. return
  253. }
  254. c.JSON(http.StatusOK, gin.H{
  255. "success": true,
  256. "message": "",
  257. })
  258. return
  259. }