package controller import ( "bytes" "encoding/json" "errors" "fmt" "github.com/bytedance/gopkg/util/gopool" "io" "math" "net/http" "net/http/httptest" "net/url" "one-api/common" "one-api/dto" "one-api/middleware" "one-api/model" "one-api/relay" relaycommon "one-api/relay/common" "one-api/relay/constant" "one-api/service" "strconv" "strings" "sync" "time" "github.com/gin-gonic/gin" ) func testChannel(channel *model.Channel, testModel string) (err error, openAIErrorWithStatusCode *dto.OpenAIErrorWithStatusCode) { tik := time.Now() if channel.Type == common.ChannelTypeMidjourney { return errors.New("midjourney channel test is not supported"), nil } if channel.Type == common.ChannelTypeSunoAPI { return errors.New("suno channel test is not supported"), nil } w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = &http.Request{ Method: "POST", URL: &url.URL{Path: "/v1/chat/completions"}, Body: nil, Header: make(http.Header), } if testModel == "" { if channel.TestModel != nil && *channel.TestModel != "" { testModel = *channel.TestModel } else { if len(channel.GetModels()) > 0 { testModel = channel.GetModels()[0] } else { testModel = "gpt-3.5-turbo" } } } else { modelMapping := *channel.ModelMapping if modelMapping != "" && modelMapping != "{}" { modelMap := make(map[string]string) err := json.Unmarshal([]byte(modelMapping), &modelMap) if err != nil { return err, service.OpenAIErrorWrapperLocal(err, "unmarshal_model_mapping_failed", http.StatusInternalServerError) } if modelMap[testModel] != "" { testModel = modelMap[testModel] } } } c.Request.Header.Set("Authorization", "Bearer "+channel.Key) c.Request.Header.Set("Content-Type", "application/json") c.Set("channel", channel.Type) c.Set("base_url", channel.GetBaseURL()) middleware.SetupContextForSelectedChannel(c, channel, testModel) meta := relaycommon.GenRelayInfo(c) apiType, _ := constant.ChannelType2APIType(channel.Type) adaptor := relay.GetAdaptor(apiType) if adaptor == nil { return fmt.Errorf("invalid api type: %d, adaptor is nil", apiType), nil } request := buildTestRequest(testModel) meta.UpstreamModelName = testModel common.SysLog(fmt.Sprintf("testing channel %d with model %s", channel.Id, testModel)) adaptor.Init(meta) convertedRequest, err := adaptor.ConvertRequest(c, meta, request) if err != nil { return err, nil } jsonData, err := json.Marshal(convertedRequest) if err != nil { return err, nil } requestBody := bytes.NewBuffer(jsonData) c.Request.Body = io.NopCloser(requestBody) resp, err := adaptor.DoRequest(c, meta, requestBody) if err != nil { return err, nil } var httpResp *http.Response if resp != nil { httpResp = resp.(*http.Response) if httpResp.StatusCode != http.StatusOK { err := service.RelayErrorHandler(httpResp) return fmt.Errorf("status code %d: %s", httpResp.StatusCode, err.Error.Message), err } } usageA, respErr := adaptor.DoResponse(c, httpResp, meta) if respErr != nil { return fmt.Errorf("%s", respErr.Error.Message), respErr } if usageA == nil { return errors.New("usage is nil"), nil } usage := usageA.(dto.Usage) result := w.Result() respBody, err := io.ReadAll(result.Body) if err != nil { return err, nil } modelPrice, usePrice := common.GetModelPrice(testModel, false) modelRatio := common.GetModelRatio(testModel) completionRatio := common.GetCompletionRatio(testModel) ratio := modelRatio quota := 0 if !usePrice { quota = usage.PromptTokens + int(math.Round(float64(usage.CompletionTokens)*completionRatio)) quota = int(math.Round(float64(quota) * ratio)) if ratio != 0 && quota <= 0 { quota = 1 } } else { quota = int(modelPrice * common.QuotaPerUnit) } tok := time.Now() milliseconds := tok.Sub(tik).Milliseconds() consumedTime := float64(milliseconds) / 1000.0 other := service.GenerateTextOtherInfo(c, meta, modelRatio, 1, completionRatio, modelPrice) model.RecordConsumeLog(c, 1, channel.Id, usage.PromptTokens, usage.CompletionTokens, testModel, "模型测试", quota, "模型测试", 0, quota, int(consumedTime), false, other) common.SysLog(fmt.Sprintf("testing channel #%d, response: \n%s", channel.Id, string(respBody))) return nil, nil } func buildTestRequest(model string) *dto.GeneralOpenAIRequest { testRequest := &dto.GeneralOpenAIRequest{ Model: "", // this will be set later Stream: false, } if strings.HasPrefix(model, "o1-") { testRequest.MaxCompletionTokens = 1 } else { testRequest.MaxTokens = 1 } content, _ := json.Marshal("hi") testMessage := dto.Message{ Role: "user", Content: content, } testRequest.Model = model testRequest.Messages = append(testRequest.Messages, testMessage) return testRequest } func TestChannel(c *gin.Context) { channelId, err := strconv.Atoi(c.Param("id")) if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, "message": err.Error(), }) return } channel, err := model.GetChannelById(channelId, true) if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, "message": err.Error(), }) return } testModel := c.Query("model") tik := time.Now() err, _ = testChannel(channel, testModel) tok := time.Now() milliseconds := tok.Sub(tik).Milliseconds() go channel.UpdateResponseTime(milliseconds) consumedTime := float64(milliseconds) / 1000.0 if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, "message": err.Error(), "time": consumedTime, }) return } c.JSON(http.StatusOK, gin.H{ "success": true, "message": "", "time": consumedTime, }) return } var testAllChannelsLock sync.Mutex var testAllChannelsRunning bool = false func testAllChannels(notify bool) error { if common.RootUserEmail == "" { common.RootUserEmail = model.GetRootUserEmail() } testAllChannelsLock.Lock() if testAllChannelsRunning { testAllChannelsLock.Unlock() return errors.New("测试已在运行中") } testAllChannelsRunning = true testAllChannelsLock.Unlock() channels, err := model.GetAllChannels(0, 0, true, false) if err != nil { return err } var disableThreshold = int64(common.ChannelDisableThreshold * 1000) if disableThreshold == 0 { disableThreshold = 10000000 // a impossible value } gopool.Go(func() { for _, channel := range channels { isChannelEnabled := channel.Status == common.ChannelStatusEnabled tik := time.Now() err, openaiWithStatusErr := testChannel(channel, "") tok := time.Now() milliseconds := tok.Sub(tik).Milliseconds() shouldBanChannel := false // request error disables the channel if openaiWithStatusErr != nil { oaiErr := openaiWithStatusErr.Error err = errors.New(fmt.Sprintf("type %s, httpCode %d, code %v, message %s", oaiErr.Type, openaiWithStatusErr.StatusCode, oaiErr.Code, oaiErr.Message)) shouldBanChannel = service.ShouldDisableChannel(channel.Type, openaiWithStatusErr) } if milliseconds > disableThreshold { err = errors.New(fmt.Sprintf("响应时间 %.2fs 超过阈值 %.2fs", float64(milliseconds)/1000.0, float64(disableThreshold)/1000.0)) shouldBanChannel = true } // disable channel if isChannelEnabled && shouldBanChannel && channel.GetAutoBan() { service.DisableChannel(channel.Id, channel.Name, err.Error()) } // enable channel if !isChannelEnabled && service.ShouldEnableChannel(err, openaiWithStatusErr, channel.Status) { service.EnableChannel(channel.Id, channel.Name) } channel.UpdateResponseTime(milliseconds) time.Sleep(common.RequestInterval) } testAllChannelsLock.Lock() testAllChannelsRunning = false testAllChannelsLock.Unlock() if notify { err := common.SendEmail("通道测试完成", common.RootUserEmail, "通道测试完成,如果没有收到禁用通知,说明所有通道都正常") if err != nil { common.SysError(fmt.Sprintf("failed to send email: %s", err.Error())) } } }) return nil } func TestAllChannels(c *gin.Context) { err := testAllChannels(true) if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, "message": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "success": true, "message": "", }) return } func AutomaticallyTestChannels(frequency int) { for { time.Sleep(time.Duration(frequency) * time.Minute) common.SysLog("testing all channels") _ = testAllChannels(false) common.SysLog("channel test finished") } }