diff --git a/middleware/distributor.go b/middleware/distributor.go index d0fd7ba5..0c4b04c3 100644 --- a/middleware/distributor.go +++ b/middleware/distributor.go @@ -67,26 +67,28 @@ func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, mode c.Set(ctxkey.BaseURL, channel.GetBaseURL()) cfg, _ := channel.LoadConfig() // this is for backward compatibility - switch channel.Type { - case channeltype.Azure: - if cfg.APIVersion == "" { - cfg.APIVersion = channel.Other - } - case channeltype.Xunfei: - if cfg.APIVersion == "" { - cfg.APIVersion = channel.Other - } - case channeltype.Gemini: - if cfg.APIVersion == "" { - cfg.APIVersion = channel.Other - } - case channeltype.AIProxyLibrary: - if cfg.LibraryID == "" { - cfg.LibraryID = channel.Other - } - case channeltype.Ali: - if cfg.Plugin == "" { - cfg.Plugin = channel.Other + if channel.Other != nil { + switch channel.Type { + case channeltype.Azure: + if cfg.APIVersion == "" { + cfg.APIVersion = *channel.Other + } + case channeltype.Xunfei: + if cfg.APIVersion == "" { + cfg.APIVersion = *channel.Other + } + case channeltype.Gemini: + if cfg.APIVersion == "" { + cfg.APIVersion = *channel.Other + } + case channeltype.AIProxyLibrary: + if cfg.LibraryID == "" { + cfg.LibraryID = *channel.Other + } + case channeltype.Ali: + if cfg.Plugin == "" { + cfg.Plugin = *channel.Other + } } } c.Set(ctxkey.Config, cfg) diff --git a/model/channel.go b/model/channel.go index ec52683e..a92f402c 100644 --- a/model/channel.go +++ b/model/channel.go @@ -27,7 +27,7 @@ type Channel struct { TestTime int64 `json:"test_time" gorm:"bigint"` ResponseTime int `json:"response_time"` // in milliseconds BaseURL *string `json:"base_url" gorm:"column:base_url;default:''"` - Other string `json:"other"` // DEPRECATED: please save config to field Config + Other *string `json:"other"` // DEPRECATED: please save config to field Config Balance float64 `json:"balance"` // in USD BalanceUpdatedTime int64 `json:"balance_updated_time" gorm:"bigint"` Models string `json:"models"` diff --git a/relay/adaptor/xunfei/adaptor.go b/relay/adaptor/xunfei/adaptor.go index 3af97831..b5967f26 100644 --- a/relay/adaptor/xunfei/adaptor.go +++ b/relay/adaptor/xunfei/adaptor.go @@ -27,14 +27,6 @@ func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) { func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error { adaptor.SetupCommonRequestHeader(c, req, meta) - version := parseAPIVersionByModelName(meta.ActualModelName) - if version == "" { - version = a.meta.Config.APIVersion - } - if version == "" { - version = "v1.1" - } - a.meta.Config.APIVersion = version // check DoResponse for auth part return nil } @@ -69,6 +61,14 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Met if a.request == nil { return nil, openai.ErrorWrapper(errors.New("request is nil"), "request_is_nil", http.StatusBadRequest) } + version := parseAPIVersionByModelName(meta.ActualModelName) + if version == "" { + version = a.meta.Config.APIVersion + } + if version == "" { + version = "v1.1" + } + a.meta.Config.APIVersion = version if meta.IsStream { err, usage = StreamHandler(c, meta, *a.request, splits[0], splits[1], splits[2]) } else { diff --git a/relay/adaptor/xunfei/main.go b/relay/adaptor/xunfei/main.go index c3e768b7..00122d5e 100644 --- a/relay/adaptor/xunfei/main.go +++ b/relay/adaptor/xunfei/main.go @@ -5,7 +5,14 @@ import ( "crypto/sha256" "encoding/base64" "encoding/json" + "errors" "fmt" + "io" + "net/http" + "net/url" + "strings" + "time" + "github.com/gin-gonic/gin" "github.com/gorilla/websocket" "github.com/songquanpeng/one-api/common" @@ -16,11 +23,6 @@ import ( "github.com/songquanpeng/one-api/relay/constant" "github.com/songquanpeng/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/model" - "io" - "net/http" - "net/url" - "strings" - "time" ) // https://console.xfyun.cn/services/cbm @@ -28,11 +30,7 @@ import ( func requestOpenAI2Xunfei(request model.GeneralOpenAIRequest, xunfeiAppId string, domain string) *ChatRequest { messages := make([]Message, 0, len(request.Messages)) - var lastToolCalls []model.Tool for _, message := range request.Messages { - if message.ToolCalls != nil { - lastToolCalls = message.ToolCalls - } messages = append(messages, Message{ Role: message.Role, Content: message.StringContent(), @@ -45,9 +43,10 @@ func requestOpenAI2Xunfei(request model.GeneralOpenAIRequest, xunfeiAppId string xunfeiRequest.Parameter.Chat.TopK = request.N xunfeiRequest.Parameter.Chat.MaxTokens = request.MaxTokens xunfeiRequest.Payload.Message.Text = messages - if len(lastToolCalls) != 0 { - for _, toolCall := range lastToolCalls { - xunfeiRequest.Payload.Functions.Text = append(xunfeiRequest.Payload.Functions.Text, toolCall.Function) + + if strings.HasPrefix(domain, "generalv3") { + xunfeiRequest.Payload.Functions = &Functions{ + Text: request.Tools, } } @@ -203,7 +202,7 @@ func Handler(c *gin.Context, meta *meta.Meta, textRequest model.GeneralOpenAIReq } } if len(xunfeiResponse.Payload.Choices.Text) == 0 { - return openai.ErrorWrapper(err, "xunfei_empty_response_detected", http.StatusInternalServerError), nil + return openai.ErrorWrapper(errors.New("xunfei empty response detected"), "xunfei_empty_response_detected", http.StatusInternalServerError), nil } xunfeiResponse.Payload.Choices.Text[0].Content = content diff --git a/relay/adaptor/xunfei/model.go b/relay/adaptor/xunfei/model.go index 5ee17cb0..681c0c05 100644 --- a/relay/adaptor/xunfei/model.go +++ b/relay/adaptor/xunfei/model.go @@ -9,6 +9,10 @@ type Message struct { Content string `json:"content"` } +type Functions struct { + Text []model.Tool `json:"text,omitempty"` +} + type ChatRequest struct { Header struct { AppId string `json:"app_id"` @@ -26,9 +30,7 @@ type ChatRequest struct { Message struct { Text []Message `json:"text"` } `json:"message"` - Functions *struct { - Text []model.Function `json:"text,omitempty"` - } `json:"functions,omitempty"` + Functions *Functions `json:"functions,omitempty"` } `json:"payload"` } diff --git a/web/default/src/pages/Channel/EditChannel.js b/web/default/src/pages/Channel/EditChannel.js index ac0fed60..fb80a726 100644 --- a/web/default/src/pages/Channel/EditChannel.js +++ b/web/default/src/pages/Channel/EditChannel.js @@ -181,9 +181,6 @@ const EditChannel = () => { if (localInputs.type === 3 && localInputs.other === '') { localInputs.other = '2024-03-01-preview'; } - if (localInputs.type === 18 && localInputs.other === '') { - localInputs.other = 'v2.1'; - } let res; localInputs.models = localInputs.models.join(','); localInputs.group = localInputs.groups.join(',');