channel-billing.go 13 KB

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