mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-10-21 08:53:42 +08:00
🎨 调整供应商目录结构,合并文本输出函数
This commit is contained in:
38
providers/openaisb/balance.go
Normal file
38
providers/openaisb/balance.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package openaisb
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (p *OpenaiSBProvider) Balance(channel *model.Channel) (float64, error) {
|
||||
fullRequestURL := p.GetFullRequestURL("/sb-api/user/status", "")
|
||||
fullRequestURL = fmt.Sprintf("%s?api_key=%s", fullRequestURL, channel.Key)
|
||||
headers := p.GetRequestHeaders()
|
||||
|
||||
client := common.NewClient()
|
||||
req, err := client.NewRequest("GET", fullRequestURL, common.WithBody(nil), common.WithHeader(headers))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
var response OpenAISBUsageResponse
|
||||
err = client.SendRequest(req, &response)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if response.Data == nil {
|
||||
return 0, errors.New(response.Msg)
|
||||
}
|
||||
balance, err := strconv.ParseFloat(response.Data.Credit, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
channel.UpdateBalance(balance)
|
||||
return balance, nil
|
||||
}
|
18
providers/openaisb/base.go
Normal file
18
providers/openaisb/base.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package openaisb
|
||||
|
||||
import (
|
||||
"one-api/providers/openai"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type OpenaiSBProvider struct {
|
||||
*openai.OpenAIProvider
|
||||
}
|
||||
|
||||
// 创建 OpenaiSBProvider
|
||||
func CreateOpenaiSBProvider(c *gin.Context) *OpenaiSBProvider {
|
||||
return &OpenaiSBProvider{
|
||||
OpenAIProvider: openai.CreateOpenAIProvider(c, "https://api.openai-sb.com"),
|
||||
}
|
||||
}
|
8
providers/openaisb/type.go
Normal file
8
providers/openaisb/type.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package openaisb
|
||||
|
||||
type OpenAISBUsageResponse struct {
|
||||
Msg string `json:"msg"`
|
||||
Data *struct {
|
||||
Credit string `json:"credit"`
|
||||
} `json:"data"`
|
||||
}
|
Reference in New Issue
Block a user