🎨 Change the method of getting channel parameters

This commit is contained in:
Martial BE
2023-12-26 16:40:50 +08:00
parent 47b72b850f
commit eeb867da10
34 changed files with 67 additions and 120 deletions

View File

@@ -55,10 +55,9 @@ func updateChannelBalance(channel *model.Channel) (float64, error) {
c, _ := gin.CreateTestContext(w)
c.Request = req
setChannelToContext(c, channel)
req.Header.Set("Content-Type", "application/json")
provider := providers.GetProvider(channel.Type, c)
provider := providers.GetProvider(channel, c)
if provider == nil {
return 0, errors.New("provider not found")
}
@@ -102,7 +101,6 @@ func UpdateChannelBalance(c *gin.Context) {
"message": "",
"balance": balance,
})
return
}
func updateAllChannelsBalance() error {
@@ -146,7 +144,6 @@ func UpdateAllChannelsBalance(c *gin.Context) {
"success": true,
"message": "",
})
return
}
func AutomaticallyUpdateChannels(frequency int) {

View File

@@ -29,7 +29,6 @@ func testChannel(channel *model.Channel, request types.ChatCompletionRequest) (e
c, _ := gin.CreateTestContext(w)
c.Request = req
setChannelToContext(c, channel)
// 创建映射
channelTypeToModel := map[int]string{
common.ChannelTypePaLM: "PaLM-2",
@@ -50,7 +49,7 @@ func testChannel(channel *model.Channel, request types.ChatCompletionRequest) (e
}
request.Model = model
provider := providers.GetProvider(channel.Type, c)
provider := providers.GetProvider(channel, c)
if provider == nil {
return errors.New("channel not implemented"), nil
}
@@ -74,7 +73,7 @@ func testChannel(channel *model.Channel, request types.ChatCompletionRequest) (e
}
if Usage.CompletionTokens == 0 {
return errors.New(fmt.Sprintf("channel %s, message 补全 tokens 非预期返回 0", channel.Name)), nil
return fmt.Errorf("channel %s, message 补全 tokens 非预期返回 0", channel.Name), nil
}
return nil, nil
@@ -132,7 +131,6 @@ func TestChannel(c *gin.Context) {
"message": "",
"time": consumedTime,
})
return
}
var testAllChannelsLock sync.Mutex

View File

@@ -1,12 +1,13 @@
package controller
import (
"github.com/gin-gonic/gin"
"net/http"
"one-api/common"
"one-api/model"
"strconv"
"strings"
"github.com/gin-gonic/gin"
)
func GetAllChannels(c *gin.Context) {
@@ -27,7 +28,6 @@ func GetAllChannels(c *gin.Context) {
"message": "",
"data": channels,
})
return
}
func SearchChannels(c *gin.Context) {
@@ -45,7 +45,6 @@ func SearchChannels(c *gin.Context) {
"message": "",
"data": channels,
})
return
}
func GetChannel(c *gin.Context) {
@@ -70,7 +69,6 @@ func GetChannel(c *gin.Context) {
"message": "",
"data": channel,
})
return
}
func AddChannel(c *gin.Context) {
@@ -106,7 +104,6 @@ func AddChannel(c *gin.Context) {
"success": true,
"message": "",
})
return
}
func DeleteChannel(c *gin.Context) {
@@ -124,7 +121,6 @@ func DeleteChannel(c *gin.Context) {
"success": true,
"message": "",
})
return
}
func DeleteDisabledChannel(c *gin.Context) {
@@ -141,7 +137,6 @@ func DeleteDisabledChannel(c *gin.Context) {
"message": "",
"data": rows,
})
return
}
func UpdateChannel(c *gin.Context) {
@@ -167,5 +162,4 @@ func UpdateChannel(c *gin.Context) {
"message": "",
"data": channel,
})
return
}

View File

@@ -5,13 +5,14 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"net/http"
"one-api/common"
"one-api/model"
"strconv"
"time"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
type GitHubOAuthResponse struct {
@@ -211,7 +212,6 @@ func GitHubBind(c *gin.Context) {
"success": true,
"message": "bind",
})
return
}
func GenerateOAuthCode(c *gin.Context) {

View File

@@ -9,7 +9,7 @@ import (
func GetGroups(c *gin.Context) {
groupNames := make([]string, 0)
for groupName, _ := range common.GroupRatio {
for groupName := range common.GroupRatio {
groupNames = append(groupNames, groupName)
}
c.JSON(http.StatusOK, gin.H{

View File

@@ -1,11 +1,12 @@
package controller
import (
"github.com/gin-gonic/gin"
"net/http"
"one-api/common"
"one-api/model"
"strconv"
"github.com/gin-gonic/gin"
)
func GetAllLogs(c *gin.Context) {
@@ -33,7 +34,6 @@ func GetAllLogs(c *gin.Context) {
"message": "",
"data": logs,
})
return
}
func GetUserLogs(c *gin.Context) {
@@ -60,7 +60,6 @@ func GetUserLogs(c *gin.Context) {
"message": "",
"data": logs,
})
return
}
func SearchAllLogs(c *gin.Context) {
@@ -78,7 +77,6 @@ func SearchAllLogs(c *gin.Context) {
"message": "",
"data": logs,
})
return
}
func SearchUserLogs(c *gin.Context) {
@@ -97,7 +95,6 @@ func SearchUserLogs(c *gin.Context) {
"message": "",
"data": logs,
})
return
}
func GetLogsStat(c *gin.Context) {
@@ -118,7 +115,6 @@ func GetLogsStat(c *gin.Context) {
//"token": tokenNum,
},
})
return
}
func GetLogsSelfStat(c *gin.Context) {
@@ -139,7 +135,6 @@ func GetLogsSelfStat(c *gin.Context) {
//"token": tokenNum,
},
})
return
}
func DeleteHistoryLogs(c *gin.Context) {
@@ -164,5 +159,4 @@ func DeleteHistoryLogs(c *gin.Context) {
"message": "",
"data": count,
})
return
}

View File

@@ -35,7 +35,6 @@ func GetStatus(c *gin.Context) {
"display_in_currency": common.DisplayInCurrencyEnabled,
},
})
return
}
func GetNotice(c *gin.Context) {
@@ -46,7 +45,6 @@ func GetNotice(c *gin.Context) {
"message": "",
"data": common.OptionMap["Notice"],
})
return
}
func GetAbout(c *gin.Context) {
@@ -57,7 +55,6 @@ func GetAbout(c *gin.Context) {
"message": "",
"data": common.OptionMap["About"],
})
return
}
func GetHomePageContent(c *gin.Context) {
@@ -68,7 +65,6 @@ func GetHomePageContent(c *gin.Context) {
"message": "",
"data": common.OptionMap["HomePageContent"],
})
return
}
func SendEmailVerification(c *gin.Context) {
@@ -121,7 +117,6 @@ func SendEmailVerification(c *gin.Context) {
"success": true,
"message": "",
})
return
}
func SendPasswordResetEmail(c *gin.Context) {
@@ -160,7 +155,6 @@ func SendPasswordResetEmail(c *gin.Context) {
"success": true,
"message": "",
})
return
}
type PasswordResetRequest struct {
@@ -200,5 +194,4 @@ func ResetPassword(c *gin.Context) {
"message": "",
"data": password,
})
return
}

View File

@@ -43,7 +43,7 @@ func RelayChat(c *gin.Context) {
}
// 获取供应商
provider, pass := getProvider(c, channel.Type, common.RelayModeChatCompletions)
provider, pass := getProvider(c, channel, common.RelayModeChatCompletions)
if pass {
return
}

View File

@@ -43,7 +43,7 @@ func RelayCompletions(c *gin.Context) {
}
// 获取供应商
provider, pass := getProvider(c, channel.Type, common.RelayModeCompletions)
provider, pass := getProvider(c, channel, common.RelayModeCompletions)
if pass {
return
}

View File

@@ -42,7 +42,7 @@ func RelayEmbeddings(c *gin.Context) {
}
// 获取供应商
provider, pass := getProvider(c, channel.Type, common.RelayModeEmbeddings)
provider, pass := getProvider(c, channel, common.RelayModeEmbeddings)
if pass {
return
}

View File

@@ -51,7 +51,7 @@ func RelayImageEdits(c *gin.Context) {
}
// 获取供应商
provider, pass := getProvider(c, channel.Type, common.RelayModeImagesEdits)
provider, pass := getProvider(c, channel, common.RelayModeImagesEdits)
if pass {
return
}

View File

@@ -54,7 +54,7 @@ func RelayImageGenerations(c *gin.Context) {
}
// 获取供应商
provider, pass := getProvider(c, channel.Type, common.RelayModeImagesGenerations)
provider, pass := getProvider(c, channel, common.RelayModeImagesGenerations)
if pass {
return
}

View File

@@ -46,7 +46,7 @@ func RelayImageVariations(c *gin.Context) {
}
// 获取供应商
provider, pass := getProvider(c, channel.Type, common.RelayModeImagesVariations)
provider, pass := getProvider(c, channel, common.RelayModeImagesVariations)
if pass {
return
}

View File

@@ -42,7 +42,7 @@ func RelayModerations(c *gin.Context) {
}
// 获取供应商
provider, pass := getProvider(c, channel.Type, common.RelayModeModerations)
provider, pass := getProvider(c, channel, common.RelayModeModerations)
if pass {
return
}

View File

@@ -38,7 +38,7 @@ func RelaySpeech(c *gin.Context) {
}
// 获取供应商
provider, pass := getProvider(c, channel.Type, common.RelayModeAudioSpeech)
provider, pass := getProvider(c, channel, common.RelayModeAudioSpeech)
if pass {
return
}

View File

@@ -38,7 +38,7 @@ func RelayTranscriptions(c *gin.Context) {
}
// 获取供应商
provider, pass := getProvider(c, channel.Type, common.RelayModeAudioTranscription)
provider, pass := getProvider(c, channel, common.RelayModeAudioTranscription)
if pass {
return
}

View File

@@ -38,7 +38,7 @@ func RelayTranslations(c *gin.Context) {
}
// 获取供应商
provider, pass := getProvider(c, channel.Type, common.RelayModeAudioTranslation)
provider, pass := getProvider(c, channel, common.RelayModeAudioTranslation)
if pass {
return
}

View File

@@ -45,7 +45,6 @@ func fetchChannel(c *gin.Context, modelName string) (channel *model.Channel, pas
return
}
setChannelToContext(c, channel)
return
}
@@ -84,8 +83,8 @@ func fetchChannelByModel(c *gin.Context, modelName string) (*model.Channel, bool
return channel, false
}
func getProvider(c *gin.Context, channelType int, relayMode int) (providersBase.ProviderInterface, bool) {
provider := providers.GetProvider(channelType, c)
func getProvider(c *gin.Context, channel *model.Channel, relayMode int) (providersBase.ProviderInterface, bool) {
provider := providers.GetProvider(channel, c)
if provider == nil {
common.AbortWithMessage(c, http.StatusNotImplemented, "channel not found")
return nil, true
@@ -99,27 +98,6 @@ func getProvider(c *gin.Context, channelType int, relayMode int) (providersBase.
return provider, false
}
func setChannelToContext(c *gin.Context, channel *model.Channel) {
// c.Set("channel", channel.Type)
c.Set("channel_id", channel.Id)
c.Set("channel_name", channel.Name)
c.Set("api_key", channel.Key)
c.Set("base_url", channel.GetBaseURL())
switch channel.Type {
case common.ChannelTypeAzure:
c.Set("api_version", channel.Other)
case common.ChannelTypeXunfei:
c.Set("api_version", channel.Other)
case common.ChannelTypeGemini:
c.Set("api_version", channel.Other)
case common.ChannelTypeAIProxyLibrary:
c.Set("library_id", channel.Other)
case common.ChannelTypeAli:
c.Set("plugin", channel.Other)
}
}
func shouldDisableChannel(err *types.OpenAIError, statusCode int) bool {
if !common.AutomaticDisableChannelEnabled {
return false

View File

@@ -1,11 +1,12 @@
package controller
import (
"github.com/gin-gonic/gin"
"net/http"
"one-api/common"
"one-api/model"
"strconv"
"github.com/gin-gonic/gin"
)
func GetAllTokens(c *gin.Context) {
@@ -27,7 +28,6 @@ func GetAllTokens(c *gin.Context) {
"message": "",
"data": tokens,
})
return
}
func SearchTokens(c *gin.Context) {
@@ -46,7 +46,6 @@ func SearchTokens(c *gin.Context) {
"message": "",
"data": tokens,
})
return
}
func GetToken(c *gin.Context) {
@@ -72,7 +71,6 @@ func GetToken(c *gin.Context) {
"message": "",
"data": token,
})
return
}
func GetTokenStatus(c *gin.Context) {
@@ -138,7 +136,6 @@ func AddToken(c *gin.Context) {
"success": true,
"message": "",
})
return
}
func DeleteToken(c *gin.Context) {
@@ -156,7 +153,6 @@ func DeleteToken(c *gin.Context) {
"success": true,
"message": "",
})
return
}
func UpdateToken(c *gin.Context) {
@@ -224,5 +220,4 @@ func UpdateToken(c *gin.Context) {
"message": "",
"data": cleanToken,
})
return
}

View File

@@ -174,7 +174,6 @@ func Register(c *gin.Context) {
"success": true,
"message": "",
})
return
}
func GetAllUsers(c *gin.Context) {
@@ -195,7 +194,6 @@ func GetAllUsers(c *gin.Context) {
"message": "",
"data": users,
})
return
}
func SearchUsers(c *gin.Context) {
@@ -213,7 +211,6 @@ func SearchUsers(c *gin.Context) {
"message": "",
"data": users,
})
return
}
func GetUser(c *gin.Context) {
@@ -246,7 +243,6 @@ func GetUser(c *gin.Context) {
"message": "",
"data": user,
})
return
}
func GetUserDashboard(c *gin.Context) {
@@ -306,7 +302,6 @@ func GenerateAccessToken(c *gin.Context) {
"message": "",
"data": user.AccessToken,
})
return
}
func GetAffCode(c *gin.Context) {
@@ -334,7 +329,6 @@ func GetAffCode(c *gin.Context) {
"message": "",
"data": user.AffCode,
})
return
}
func GetSelf(c *gin.Context) {
@@ -352,7 +346,6 @@ func GetSelf(c *gin.Context) {
"message": "",
"data": user,
})
return
}
func UpdateUser(c *gin.Context) {
@@ -416,7 +409,6 @@ func UpdateUser(c *gin.Context) {
"success": true,
"message": "",
})
return
}
func UpdateSelf(c *gin.Context) {
@@ -463,7 +455,6 @@ func UpdateSelf(c *gin.Context) {
"success": true,
"message": "",
})
return
}
func DeleteUser(c *gin.Context) {
@@ -525,7 +516,6 @@ func DeleteSelf(c *gin.Context) {
"success": true,
"message": "",
})
return
}
func CreateUser(c *gin.Context) {
@@ -574,7 +564,6 @@ func CreateUser(c *gin.Context) {
"success": true,
"message": "",
})
return
}
type ManageRequest struct {
@@ -691,7 +680,6 @@ func ManageUser(c *gin.Context) {
"message": "",
"data": clearUser,
})
return
}
func EmailBind(c *gin.Context) {
@@ -733,7 +721,6 @@ func EmailBind(c *gin.Context) {
"success": true,
"message": "",
})
return
}
type topUpRequest struct {
@@ -764,5 +751,4 @@ func TopUp(c *gin.Context) {
"message": "",
"data": quota,
})
return
}

View File

@@ -4,12 +4,13 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"one-api/common"
"one-api/model"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
type wechatLoginResponse struct {
@@ -160,5 +161,4 @@ func WeChatBind(c *gin.Context) {
"success": true,
"message": "",
})
return
}