Skip to content

Commit

Permalink
feat: support channel type AIGC2D (songquanpeng#220)
Browse files Browse the repository at this point in the history
* feat: add AIGC2D Channel

* chore: remove console logging & update balance rendering

---------

Co-authored-by: Alone88 <[email protected]>
Co-authored-by: JustSong <[email protected]>
  • Loading branch information
3 people authored Jun 29, 2023
1 parent 8f6bd51 commit b1b3651
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ const (
ChannelTypeAIProxy = 10
ChannelTypePaLM = 11
ChannelTypeAPI2GPT = 12
ChannelTypeAIGC2D = 13
)

var ChannelBaseURLs = []string{
Expand All @@ -164,4 +165,5 @@ var ChannelBaseURLs = []string{
"https://api.aiproxy.io", // 10
"", // 11
"https://api.api2gpt.com", // 12
"https://api.aigc2d.com", // 13
}
25 changes: 25 additions & 0 deletions controller/channel-billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ type API2GPTUsageResponse struct {
TotalRemaining float64 `json:"total_remaining"`
}

type APGC2DGPTUsageResponse struct {
//Grants interface{} `json:"grants"`
Object string `json:"object"`
TotalAvailable float64 `json:"total_available"`
TotalGranted float64 `json:"total_granted"`
TotalUsed float64 `json:"total_used"`
}

// GetAuthHeader get auth header
func GetAuthHeader(token string) http.Header {
h := http.Header{}
Expand Down Expand Up @@ -150,6 +158,21 @@ func updateChannelAPI2GPTBalance(channel *model.Channel) (float64, error) {
return response.TotalRemaining, nil
}

func updateChannelAIGC2DBalance(channel *model.Channel) (float64, error) {
url := "https://api.aigc2d.com/dashboard/billing/credit_grants"
body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
if err != nil {
return 0, err
}
response := APGC2DGPTUsageResponse{}
err = json.Unmarshal(body, &response)
if err != nil {
return 0, err
}
channel.UpdateBalance(response.TotalAvailable)
return response.TotalAvailable, nil
}

func updateChannelBalance(channel *model.Channel) (float64, error) {
baseURL := common.ChannelBaseURLs[channel.Type]
switch channel.Type {
Expand All @@ -167,6 +190,8 @@ func updateChannelBalance(channel *model.Channel) (float64, error) {
return updateChannelAIProxyBalance(channel)
case common.ChannelTypeAPI2GPT:
return updateChannelAPI2GPTBalance(channel)
case common.ChannelTypeAIGC2D:
return updateChannelAIGC2DBalance(channel)
default:
return 0, errors.New("尚未实现")
}
Expand Down
2 changes: 2 additions & 0 deletions web/src/components/ChannelsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function renderBalance(type, balance) {
return <span>{renderNumber(balance)}</span>;
case 12: // API2GPT
return <span>¥{balance.toFixed(2)}</span>;
case 13: // AIGC2D
return <span>{renderNumber(balance)}</span>;
default:
return <span>不支持</span>;
}
Expand Down
3 changes: 2 additions & 1 deletion web/src/constants/channel.constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export const CHANNEL_OPTIONS = [
{ key: 7, text: 'OhMyGPT', value: 7, color: 'purple' },
{ key: 9, text: 'AI.LS', value: 9, color: 'yellow' },
{ key: 10, text: 'AI Proxy', value: 10, color: 'purple' },
{ key: 12, text: 'API2GPT', value: 12, color: 'blue' }
{ key: 12, text: 'API2GPT', value: 12, color: 'blue' },
{ key: 13, text: 'AIGC2D', value: 13, color: 'purple' }
];

0 comments on commit b1b3651

Please sign in to comment.