Skip to content

Commit

Permalink
修复model_redirect的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fruitbars committed Jun 24, 2024
1 parent e809f3b commit 7807806
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
20 changes: 20 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/rand"
"os"
"simple-one-api/pkg/utils"
"sort"
"time"
)

Expand Down Expand Up @@ -120,12 +121,17 @@ func createModelToServiceMap(config Configuration) map[string][]ModelDetails {
//存储支持的模型名称列表
SuppertModels[modelName] = modelName
for k, v := range detail.ModelRedirect {
//support models
SuppertModels[k] = v

_, exists := SuppertModels[v]
if exists {
delete(SuppertModels, v)
}

//
modelToService[k] = append(modelToService[k], detail)
//delete(modelToService, modelName)
}
}
}
Expand Down Expand Up @@ -202,6 +208,9 @@ func InitConfig(configName string) error {
// 创建映射
ModelToService = createModelToServiceMap(conf)

//
ShowSupportModels()

return nil
}

Expand Down Expand Up @@ -291,3 +300,14 @@ func GetModelRedirect(s *ModelDetails, model string) string {
}
return model
}

func ShowSupportModels() {
keys := make([]string, 0, len(ModelToService))

for k := range SuppertModels {
keys = append(keys, k)
}
sort.Strings(keys) // 对keys进行排序

log.Println("other support models:", keys)
}
File renamed without changes.
9 changes: 9 additions & 0 deletions pkg/handler/openai_cozecn_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"net/http"
"simple-one-api/pkg/adapter"
"simple-one-api/pkg/common"
"simple-one-api/pkg/config"
"simple-one-api/pkg/llm/devplatform/cozecn"
"simple-one-api/pkg/mylog"
Expand Down Expand Up @@ -81,6 +82,12 @@ func sendRequest(c *gin.Context, token, url string, request interface{}, oaiReq
}
defer resp.Body.Close()

err = common.CheckStatusCode(resp)
if err != nil {

return err
}

return handleCozecnResponse(c, resp, oaiReq)
}

Expand All @@ -95,6 +102,8 @@ func handleCozecnResponse(c *gin.Context, resp *http.Response, oaiReq openai.Cha
return err
}

mylog.Logger.Info("response", zap.String("body", string(body)))

var respJson cozecn.Response
if err := json.Unmarshal(body, &respJson); err != nil {
mylog.Logger.Error(err.Error())
Expand Down
16 changes: 5 additions & 11 deletions pkg/handler/openai_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@ func OpenAIHandler(c *gin.Context) {

func handleOpenAIRequest(c *gin.Context, oaiReq openai.ChatCompletionRequest) {

clientModel := oaiReq.Model

s, modelName, err := getModelDetails(oaiReq)
if err != nil {
mylog.Logger.Error(err.Error())
sendErrorResponse(c, http.StatusBadRequest, err.Error())
return
}

oaiReq.Model = config.GetModelRedirect(s, modelName)

timeout := s.Timeout
if timeout <= 0 {
timeout = defaultReqTimeout // default timeout if not specified
Expand Down Expand Up @@ -172,18 +176,8 @@ func handleOpenAIRequest(c *gin.Context, oaiReq openai.ChatCompletionRequest) {

}

clientModel := oaiReq.Model
if clientModel == "random" {
oaiReq.Model = config.GetModelRedirect(s, clientModel)
}

//兼容之前的版本
if oaiReq.Model == clientModel {
oaiReq.Model = config.GetModelMapping(s, modelName)
}

// 假设 logger 是一个已经配置好的 zap.Logger 实例
mylog.Logger.Debug("Service details",
mylog.Logger.Info("Service details",
zap.String("service_name", s.ServiceName), // 假设 s.ServiceName 是 string 类型
zap.String("client_model", clientModel), // 假设 clientModel 是 string 类型
zap.String("real_model_name", modelName), // 假设 modelName 是 string 类型
Expand Down

0 comments on commit 7807806

Please sign in to comment.