From 7d5c0959b2f901ba605ab3be4c1b39aca99ed6f8 Mon Sep 17 00:00:00 2001 From: Gk0Wk Date: Mon, 25 Nov 2024 15:59:39 +0800 Subject: [PATCH 1/2] Support Balance Query for DeepSeek --- controller/channel-billing.go | 40 +++++++++++++++++-- .../src/views/Channel/component/TableRow.js | 2 + web/default/src/components/ChannelsTable.js | 2 + 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/controller/channel-billing.go b/controller/channel-billing.go index a6ffaafe..3ebbf1c5 100644 --- a/controller/channel-billing.go +++ b/controller/channel-billing.go @@ -4,16 +4,17 @@ import ( "encoding/json" "errors" "fmt" + "io" + "net/http" + "strconv" + "time" + "github.com/songquanpeng/one-api/common/client" "github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/logger" "github.com/songquanpeng/one-api/model" "github.com/songquanpeng/one-api/monitor" "github.com/songquanpeng/one-api/relay/channeltype" - "io" - "net/http" - "strconv" - "time" "github.com/gin-gonic/gin" ) @@ -101,6 +102,16 @@ type SiliconFlowUsageResponse struct { } `json:"data"` } +type DeepSeekUsageResponse struct { + IsAvailable bool `json:"is_available"` + BalanceInfos []struct { + Currency string `json:"currency"` + TotalBalance string `json:"total_balance"` + GrantedBalance string `json:"granted_balance"` + ToppedUpBalance string `json:"topped_up_balance"` + } +} + // GetAuthHeader get auth header func GetAuthHeader(token string) http.Header { h := http.Header{} @@ -245,6 +256,25 @@ func updateChannelSiliconFlowBalance(channel *model.Channel) (float64, error) { return balance, nil } +func updateChannelDeepSeekBalance(channel *model.Channel) (float64, error) { + url := "https://api.deepseek.com/user/balance" + body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key)) + if err != nil { + return 0, err + } + response := DeepSeekUsageResponse{} + err = json.Unmarshal(body, &response) + if err != nil { + return 0, err + } + balance, err := strconv.ParseFloat(response.BalanceInfos[0].TotalBalance, 64) + if err != nil { + return 0, err + } + channel.UpdateBalance(balance) + return balance, nil +} + func updateChannelBalance(channel *model.Channel) (float64, error) { baseURL := channeltype.ChannelBaseURLs[channel.Type] if channel.GetBaseURL() == "" { @@ -271,6 +301,8 @@ func updateChannelBalance(channel *model.Channel) (float64, error) { return updateChannelAIGC2DBalance(channel) case channeltype.SiliconFlow: return updateChannelSiliconFlowBalance(channel) + case channeltype.DeepSeek: + return updateChannelDeepSeekBalance(channel) default: return 0, errors.New("尚未实现") } diff --git a/web/berry/src/views/Channel/component/TableRow.js b/web/berry/src/views/Channel/component/TableRow.js index 3114479d..525f9188 100644 --- a/web/berry/src/views/Channel/component/TableRow.js +++ b/web/berry/src/views/Channel/component/TableRow.js @@ -268,6 +268,8 @@ function renderBalance(type, balance) { return ¥{balance.toFixed(2)}; case 13: // AIGC2D return {renderNumber(balance)}; + case 36: // DeepSeek + return ¥{balance.toFixed(2)}; case 44: // SiliconFlow return ¥{balance.toFixed(2)}; default: diff --git a/web/default/src/components/ChannelsTable.js b/web/default/src/components/ChannelsTable.js index 6e0ec05d..e745814b 100644 --- a/web/default/src/components/ChannelsTable.js +++ b/web/default/src/components/ChannelsTable.js @@ -52,6 +52,8 @@ function renderBalance(type, balance) { return ¥{balance.toFixed(2)}; case 13: // AIGC2D return {renderNumber(balance)}; + case 36: // DeepSeek + return ¥{balance.toFixed(2)}; case 44: // SiliconFlow return ¥{balance.toFixed(2)}; default: From b3b3c59aacd60827bb0eb0e1b14c891b2c5b1f46 Mon Sep 17 00:00:00 2001 From: Gk0Wk Date: Tue, 26 Nov 2024 19:23:54 +0800 Subject: [PATCH 2/2] Fix --- controller/channel-billing.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/controller/channel-billing.go b/controller/channel-billing.go index 3ebbf1c5..9456aa9c 100644 --- a/controller/channel-billing.go +++ b/controller/channel-billing.go @@ -109,7 +109,7 @@ type DeepSeekUsageResponse struct { TotalBalance string `json:"total_balance"` GrantedBalance string `json:"granted_balance"` ToppedUpBalance string `json:"topped_up_balance"` - } + } `json:"balance_infos"` } // GetAuthHeader get auth header @@ -267,7 +267,17 @@ func updateChannelDeepSeekBalance(channel *model.Channel) (float64, error) { if err != nil { return 0, err } - balance, err := strconv.ParseFloat(response.BalanceInfos[0].TotalBalance, 64) + index := -1 + for i, balanceInfo := range response.BalanceInfos { + if balanceInfo.Currency == "CNY" { + index = i + break + } + } + if index == -1 { + return 0, errors.New("currency CNY not found") + } + balance, err := strconv.ParseFloat(response.BalanceInfos[index].TotalBalance, 64) if err != nil { return 0, err }