| 149 | } |
| 150 | |
| 151 | func testChannel(ctx context.Context, channel *model.Channel, request *relaymodel.GeneralOpenAIRequest) (responseMessage string, err error, openaiErr *relaymodel.Error, actualModel string) { |
| 152 | //startTime := time.Now() |
| 153 | w := httptest.NewRecorder() |
| 154 | c, _ := gin.CreateTestContext(w) |
| 155 | c.Request = &http.Request{ |
| 156 | Method: "POST", |
| 157 | URL: &url.URL{Path: "/v1/chat/completions"}, |
| 158 | Body: nil, |
| 159 | Header: make(http.Header), |
| 160 | } |
| 161 | c.Request.Header.Set("Authorization", "Bearer "+channel.Key) |
| 162 | c.Request.Header.Set("Content-Type", "application/json") |
| 163 | c.Set(ctxkey.Channel, channel.Type) |
| 164 | c.Set(ctxkey.BaseURL, channel.GetBaseURL()) |
| 165 | cfg, _ := channel.LoadConfig() |
| 166 | c.Set(ctxkey.Config, cfg) |
| 167 | middleware.SetupContextForSelectedChannel(c, channel, "") |
| 168 | meta := meta.GetByContext(c) |
| 169 | apiType := model.GetApiType(channel.Type) |
| 170 | meta.APIType = apiType |
| 171 | // apiType := channeltype.ToAPIType(channel.Type) |
| 172 | adaptor := service.GetAdaptor(meta.APIType) |
| 173 | err = service.SetCustomConfig(&adaptor, &custom.CustomConfig{ |
| 174 | ConversationId: "", |
| 175 | UserId: "53AIHub", |
| 176 | }) |
| 177 | if err != nil { |
| 178 | return "", err, nil, "" |
| 179 | } |
| 180 | // adaptor := relay.GetAdaptor(apiType) |
| 181 | if adaptor == nil { |
| 182 | return "", fmt.Errorf("invalid api type: %d, adaptor is nil", apiType), nil, "" |
| 183 | } |
| 184 | adaptor.Init(meta) |
| 185 | modelName := request.Model |
| 186 | modelMap := channel.GetModelMapping() |
| 187 | if modelName == "" || !strings.Contains(channel.Models, modelName) { |
| 188 | modelNames := strings.Split(channel.Models, ",") |
| 189 | if len(modelNames) > 0 { |
| 190 | modelName = modelNames[0] |
| 191 | } |
| 192 | } |
| 193 | if modelMap != nil && modelMap[modelName] != "" { |
| 194 | modelName = modelMap[modelName] |
| 195 | } |
| 196 | meta.OriginModelName, meta.ActualModelName = request.Model, modelName |
| 197 | request.Model = modelName |
| 198 | convertedRequest, err := adaptor.ConvertRequest(c, relaymode.ChatCompletions, request) |
| 199 | if err != nil { |
| 200 | return "", err, nil, "" |
| 201 | } |
| 202 | jsonData, err := json.Marshal(convertedRequest) |
| 203 | if err != nil { |
| 204 | return "", err, nil, "" |
| 205 | } |
| 206 | defer func() { |
| 207 | //logContent := fmt.Sprintf("渠道 %s 测试成功,响应:%s", channel.Name, responseMessage) |
| 208 | if err != nil || openaiErr != nil { |