| package helper |
|
|
| import ( |
| "encoding/json" |
| "errors" |
| "fmt" |
| "strings" |
|
|
| "github.com/QuantumNous/new-api/dto" |
| "github.com/QuantumNous/new-api/relay/common" |
| relayconstant "github.com/QuantumNous/new-api/relay/constant" |
| "github.com/QuantumNous/new-api/setting/ratio_setting" |
| "github.com/gin-gonic/gin" |
| ) |
|
|
| func ModelMappedHelper(c *gin.Context, info *common.RelayInfo, request dto.Request) error { |
| if info.ChannelMeta == nil { |
| info.ChannelMeta = &common.ChannelMeta{} |
| } |
|
|
| isResponsesCompact := info.RelayMode == relayconstant.RelayModeResponsesCompact |
| originModelName := info.OriginModelName |
| mappingModelName := originModelName |
| if isResponsesCompact && strings.HasSuffix(originModelName, ratio_setting.CompactModelSuffix) { |
| mappingModelName = strings.TrimSuffix(originModelName, ratio_setting.CompactModelSuffix) |
| } |
|
|
| |
| modelMapping := c.GetString("model_mapping") |
| if modelMapping != "" && modelMapping != "{}" { |
| modelMap := make(map[string]string) |
| err := json.Unmarshal([]byte(modelMapping), &modelMap) |
| if err != nil { |
| return fmt.Errorf("unmarshal_model_mapping_failed") |
| } |
|
|
| |
| currentModel := mappingModelName |
| visitedModels := map[string]bool{ |
| currentModel: true, |
| } |
| for { |
| if mappedModel, exists := modelMap[currentModel]; exists && mappedModel != "" { |
| |
| if visitedModels[mappedModel] { |
| if mappedModel == currentModel { |
| if currentModel == info.OriginModelName { |
| info.IsModelMapped = false |
| return nil |
| } else { |
| info.IsModelMapped = true |
| break |
| } |
| } |
| return errors.New("model_mapping_contains_cycle") |
| } |
| visitedModels[mappedModel] = true |
| currentModel = mappedModel |
| info.IsModelMapped = true |
| } else { |
| break |
| } |
| } |
| if info.IsModelMapped { |
| info.UpstreamModelName = currentModel |
| } |
| } |
|
|
| if isResponsesCompact { |
| finalUpstreamModelName := mappingModelName |
| if info.IsModelMapped && info.UpstreamModelName != "" { |
| finalUpstreamModelName = info.UpstreamModelName |
| } |
| info.UpstreamModelName = finalUpstreamModelName |
| info.OriginModelName = ratio_setting.WithCompactModelSuffix(finalUpstreamModelName) |
| } |
| if request != nil { |
| request.SetModelName(info.UpstreamModelName) |
| } |
| return nil |
| } |
|
|