(c *gin.Context, info *common.RelayInfo, request dto.Request)
| 14 | ) |
| 15 | |
| 16 | func ModelMappedHelper(c *gin.Context, info *common.RelayInfo, request dto.Request) error { |
| 17 | if info.ChannelMeta == nil { |
| 18 | info.ChannelMeta = &common.ChannelMeta{} |
| 19 | } |
| 20 | |
| 21 | isResponsesCompact := info.RelayMode == relayconstant.RelayModeResponsesCompact |
| 22 | originModelName := info.OriginModelName |
| 23 | mappingModelName := originModelName |
| 24 | if isResponsesCompact && strings.HasSuffix(originModelName, ratio_setting.CompactModelSuffix) { |
| 25 | mappingModelName = strings.TrimSuffix(originModelName, ratio_setting.CompactModelSuffix) |
| 26 | } |
| 27 | |
| 28 | // map model name |
| 29 | modelMapping := c.GetString("model_mapping") |
| 30 | if modelMapping != "" && modelMapping != "{}" { |
| 31 | modelMap := make(map[string]string) |
| 32 | err := json.Unmarshal([]byte(modelMapping), &modelMap) |
| 33 | if err != nil { |
| 34 | return fmt.Errorf("unmarshal_model_mapping_failed") |
| 35 | } |
| 36 | |
| 37 | // 支持链式模型重定向,最终使用链尾的模型 |
| 38 | currentModel := mappingModelName |
| 39 | visitedModels := map[string]bool{ |
| 40 | currentModel: true, |
| 41 | } |
| 42 | for { |
| 43 | if mappedModel, exists := modelMap[currentModel]; exists && mappedModel != "" { |
| 44 | // 模型重定向循环检测,避免无限循环 |
| 45 | if visitedModels[mappedModel] { |
| 46 | if mappedModel == currentModel { |
| 47 | if currentModel == info.OriginModelName { |
| 48 | info.IsModelMapped = false |
| 49 | return nil |
| 50 | } else { |
| 51 | info.IsModelMapped = true |
| 52 | break |
| 53 | } |
| 54 | } |
| 55 | return errors.New("model_mapping_contains_cycle") |
| 56 | } |
| 57 | visitedModels[mappedModel] = true |
| 58 | currentModel = mappedModel |
| 59 | info.IsModelMapped = true |
| 60 | } else { |
| 61 | break |
| 62 | } |
| 63 | } |
| 64 | if info.IsModelMapped { |
| 65 | info.UpstreamModelName = currentModel |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if isResponsesCompact { |
| 70 | finalUpstreamModelName := mappingModelName |
| 71 | if info.IsModelMapped && info.UpstreamModelName != "" { |
| 72 | finalUpstreamModelName = info.UpstreamModelName |
| 73 | } |
no test coverage detected