channel-billing.go 13 KB

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