channel-billing.go 13 KB

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