| 47 | } |
| 48 | |
| 49 | func testChannel(channel *model.Channel, request *relaymodel.GeneralOpenAIRequest) (err error, openaiErr *relaymodel.Error) { |
| 50 | w := httptest.NewRecorder() |
| 51 | c, _ := gin.CreateTestContext(w) |
| 52 | c.Request = &http.Request{ |
| 53 | Method: "POST", |
| 54 | URL: &url.URL{Path: "/v1/chat/completions"}, |
| 55 | Body: nil, |
| 56 | Header: make(http.Header), |
| 57 | } |
| 58 | c.Request.Header.Set("Authorization", "Bearer "+channel.Key) |
| 59 | c.Request.Header.Set("Content-Type", "application/json") |
| 60 | c.Set(ctxkey.Channel, channel.Type) |
| 61 | c.Set(ctxkey.BaseURL, channel.GetBaseURL()) |
| 62 | cfg, _ := channel.LoadConfig() |
| 63 | c.Set(ctxkey.Config, cfg) |
| 64 | middleware.SetupContextForSelectedChannel(c, channel, "") |
| 65 | meta := meta.GetByContext(c) |
| 66 | apiType := channeltype.ToAPIType(channel.Type) |
| 67 | adaptor := relay.GetAdaptor(apiType) |
| 68 | if adaptor == nil { |
| 69 | return fmt.Errorf("invalid api type: %d, adaptor is nil", apiType), nil |
| 70 | } |
| 71 | adaptor.Init(meta) |
| 72 | modelName := request.Model |
| 73 | modelMap := channel.GetModelMapping() |
| 74 | if modelName == "" || !strings.Contains(channel.Models, modelName) { |
| 75 | modelNames := strings.Split(channel.Models, ",") |
| 76 | if len(modelNames) > 0 { |
| 77 | modelName = modelNames[0] |
| 78 | } |
| 79 | } |
| 80 | if modelMap != nil && modelMap[modelName] != "" { |
| 81 | modelName = modelMap[modelName] |
| 82 | } |
| 83 | meta.OriginModelName, meta.ActualModelName = request.Model, modelName |
| 84 | request.Model = modelName |
| 85 | convertedRequest, err := adaptor.ConvertRequest(c, relaymode.ChatCompletions, request) |
| 86 | if err != nil { |
| 87 | return err, nil |
| 88 | } |
| 89 | jsonData, err := json.Marshal(convertedRequest) |
| 90 | if err != nil { |
| 91 | return err, nil |
| 92 | } |
| 93 | logger.SysLog(string(jsonData)) |
| 94 | requestBody := bytes.NewBuffer(jsonData) |
| 95 | c.Request.Body = io.NopCloser(requestBody) |
| 96 | resp, err := adaptor.DoRequest(c, meta, requestBody) |
| 97 | if err != nil { |
| 98 | return err, nil |
| 99 | } |
| 100 | if resp != nil && resp.StatusCode != http.StatusOK { |
| 101 | err := controller.RelayErrorHandler(resp) |
| 102 | return fmt.Errorf("status code %d: %s", resp.StatusCode, err.Error.Message), &err.Error |
| 103 | } |
| 104 | usage, respErr := adaptor.DoResponse(c, resp, meta) |
| 105 | if respErr != nil { |
| 106 | return fmt.Errorf("%s", respErr.Error.Message), &respErr.Error |