2023-05-23 02:01:09 +00:00
|
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2024-04-27 07:53:20 +00:00
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
2024-01-28 11:38:58 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/common/config"
|
2024-04-21 11:43:23 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/common/ctxkey"
|
2024-01-28 11:38:58 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/common/logger"
|
2024-03-10 11:16:06 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/common/message"
|
2024-03-03 12:51:28 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/middleware"
|
2024-01-28 11:38:58 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/model"
|
2024-03-10 09:57:47 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/monitor"
|
2024-04-05 17:50:12 +00:00
|
|
|
|
relay "github.com/songquanpeng/one-api/relay"
|
2024-04-05 16:44:33 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/relay/channeltype"
|
2024-04-05 17:50:12 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/relay/controller"
|
2024-04-05 17:31:44 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/relay/meta"
|
2024-02-17 16:15:31 +00:00
|
|
|
|
relaymodel "github.com/songquanpeng/one-api/relay/model"
|
2024-04-05 16:44:33 +00:00
|
|
|
|
"github.com/songquanpeng/one-api/relay/relaymode"
|
2023-11-19 07:52:35 +00:00
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-05-23 02:01:09 +00:00
|
|
|
|
)
|
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
|
func buildTestRequest() *relaymodel.GeneralOpenAIRequest {
|
|
|
|
|
testRequest := &relaymodel.GeneralOpenAIRequest{
|
2024-03-14 15:17:19 +00:00
|
|
|
|
MaxTokens: 2,
|
2024-02-17 16:15:31 +00:00
|
|
|
|
Stream: false,
|
|
|
|
|
Model: "gpt-3.5-turbo",
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
2024-02-17 16:15:31 +00:00
|
|
|
|
testMessage := relaymodel.Message{
|
|
|
|
|
Role: "user",
|
|
|
|
|
Content: "hi",
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
2024-02-17 16:15:31 +00:00
|
|
|
|
testRequest.Messages = append(testRequest.Messages, testMessage)
|
|
|
|
|
return testRequest
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testChannel(channel *model.Channel) (err error, openaiErr *relaymodel.Error) {
|
|
|
|
|
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),
|
|
|
|
|
}
|
|
|
|
|
c.Request.Header.Set("Authorization", "Bearer "+channel.Key)
|
|
|
|
|
c.Request.Header.Set("Content-Type", "application/json")
|
2024-04-21 11:43:23 +00:00
|
|
|
|
c.Set(ctxkey.Channel, channel.Type)
|
|
|
|
|
c.Set(ctxkey.BaseURL, channel.GetBaseURL())
|
2024-04-26 15:05:48 +00:00
|
|
|
|
cfg, _ := channel.LoadConfig()
|
|
|
|
|
c.Set(ctxkey.Config, cfg)
|
2024-03-03 12:51:28 +00:00
|
|
|
|
middleware.SetupContextForSelectedChannel(c, channel, "")
|
2024-04-05 17:31:44 +00:00
|
|
|
|
meta := meta.GetByContext(c)
|
2024-04-05 16:44:33 +00:00
|
|
|
|
apiType := channeltype.ToAPIType(channel.Type)
|
2024-04-05 17:50:12 +00:00
|
|
|
|
adaptor := relay.GetAdaptor(apiType)
|
2024-02-17 16:15:31 +00:00
|
|
|
|
if adaptor == nil {
|
|
|
|
|
return fmt.Errorf("invalid api type: %d, adaptor is nil", apiType), nil
|
|
|
|
|
}
|
2024-02-18 08:17:19 +00:00
|
|
|
|
adaptor.Init(meta)
|
2024-04-21 09:59:57 +00:00
|
|
|
|
var modelName string
|
|
|
|
|
modelList := adaptor.GetModelList()
|
2024-04-27 07:53:20 +00:00
|
|
|
|
modelMap := channel.GetModelMapping()
|
2024-04-21 09:59:57 +00:00
|
|
|
|
if len(modelList) != 0 {
|
|
|
|
|
modelName = modelList[0]
|
|
|
|
|
}
|
|
|
|
|
if modelName == "" || !strings.Contains(channel.Models, modelName) {
|
2024-03-03 14:58:41 +00:00
|
|
|
|
modelNames := strings.Split(channel.Models, ",")
|
|
|
|
|
if len(modelNames) > 0 {
|
|
|
|
|
modelName = modelNames[0]
|
|
|
|
|
}
|
2024-04-27 07:53:20 +00:00
|
|
|
|
if modelMap != nil && modelMap[modelName] != "" {
|
|
|
|
|
modelName = modelMap[modelName]
|
|
|
|
|
}
|
2024-03-03 14:58:41 +00:00
|
|
|
|
}
|
2024-02-17 16:15:31 +00:00
|
|
|
|
request := buildTestRequest()
|
|
|
|
|
request.Model = modelName
|
|
|
|
|
meta.OriginModelName, meta.ActualModelName = modelName, modelName
|
2024-04-05 16:44:33 +00:00
|
|
|
|
convertedRequest, err := adaptor.ConvertRequest(c, relaymode.ChatCompletions, request)
|
2023-05-23 02:01:09 +00:00
|
|
|
|
if err != nil {
|
2023-07-22 10:15:30 +00:00
|
|
|
|
return err, nil
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
2024-02-17 16:15:31 +00:00
|
|
|
|
jsonData, err := json.Marshal(convertedRequest)
|
2023-05-23 02:01:09 +00:00
|
|
|
|
if err != nil {
|
2023-07-22 10:15:30 +00:00
|
|
|
|
return err, nil
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
2024-04-21 09:59:57 +00:00
|
|
|
|
logger.SysLog(string(jsonData))
|
2024-02-17 16:15:31 +00:00
|
|
|
|
requestBody := bytes.NewBuffer(jsonData)
|
|
|
|
|
c.Request.Body = io.NopCloser(requestBody)
|
|
|
|
|
resp, err := adaptor.DoRequest(c, meta, requestBody)
|
2023-05-23 02:01:09 +00:00
|
|
|
|
if err != nil {
|
2023-07-22 10:15:30 +00:00
|
|
|
|
return err, nil
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
2024-04-19 16:40:47 +00:00
|
|
|
|
if resp != nil && resp.StatusCode != http.StatusOK {
|
2024-04-05 17:50:12 +00:00
|
|
|
|
err := controller.RelayErrorHandler(resp)
|
2024-02-17 16:15:31 +00:00
|
|
|
|
return fmt.Errorf("status code %d: %s", resp.StatusCode, err.Error.Message), &err.Error
|
2023-11-17 13:45:55 +00:00
|
|
|
|
}
|
2024-02-17 16:15:31 +00:00
|
|
|
|
usage, respErr := adaptor.DoResponse(c, resp, meta)
|
|
|
|
|
if respErr != nil {
|
|
|
|
|
return fmt.Errorf("%s", respErr.Error.Message), &respErr.Error
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
2024-02-17 16:15:31 +00:00
|
|
|
|
if usage == nil {
|
|
|
|
|
return errors.New("usage is nil"), nil
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
2024-02-17 16:15:31 +00:00
|
|
|
|
result := w.Result()
|
|
|
|
|
// print result.Body
|
|
|
|
|
respBody, err := io.ReadAll(result.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err, nil
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
2024-02-17 16:15:31 +00:00
|
|
|
|
logger.SysLog(fmt.Sprintf("testing channel #%d, response: \n%s", channel.Id, string(respBody)))
|
|
|
|
|
return nil, nil
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestChannel(c *gin.Context) {
|
|
|
|
|
id, 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(id, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
tik := time.Now()
|
2024-02-17 16:15:31 +00:00
|
|
|
|
err, _ = testChannel(channel)
|
2023-05-23 02:01:09 +00:00
|
|
|
|
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
|
|
|
|
|
|
2024-03-10 10:34:57 +00:00
|
|
|
|
func testChannels(notify bool, scope string) error {
|
2024-01-21 15:21:42 +00:00
|
|
|
|
if config.RootUserEmail == "" {
|
|
|
|
|
config.RootUserEmail = model.GetRootUserEmail()
|
2023-05-31 07:24:40 +00:00
|
|
|
|
}
|
2023-05-23 02:01:09 +00:00
|
|
|
|
testAllChannelsLock.Lock()
|
|
|
|
|
if testAllChannelsRunning {
|
|
|
|
|
testAllChannelsLock.Unlock()
|
|
|
|
|
return errors.New("测试已在运行中")
|
|
|
|
|
}
|
|
|
|
|
testAllChannelsRunning = true
|
|
|
|
|
testAllChannelsLock.Unlock()
|
2024-03-10 10:34:57 +00:00
|
|
|
|
channels, err := model.GetAllChannels(0, 0, scope)
|
2023-05-23 02:01:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-01-21 15:21:42 +00:00
|
|
|
|
var disableThreshold = int64(config.ChannelDisableThreshold * 1000)
|
2023-05-23 02:01:09 +00:00
|
|
|
|
if disableThreshold == 0 {
|
|
|
|
|
disableThreshold = 10000000 // a impossible value
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
for _, channel := range channels {
|
2024-04-05 18:03:59 +00:00
|
|
|
|
isChannelEnabled := channel.Status == model.ChannelStatusEnabled
|
2023-05-23 02:01:09 +00:00
|
|
|
|
tik := time.Now()
|
2024-02-17 16:15:31 +00:00
|
|
|
|
err, openaiErr := testChannel(channel)
|
2023-05-23 02:01:09 +00:00
|
|
|
|
tok := time.Now()
|
|
|
|
|
milliseconds := tok.Sub(tik).Milliseconds()
|
2023-12-03 12:10:57 +00:00
|
|
|
|
if isChannelEnabled && milliseconds > disableThreshold {
|
2023-07-22 10:15:30 +00:00
|
|
|
|
err = errors.New(fmt.Sprintf("响应时间 %.2fs 超过阈值 %.2fs", float64(milliseconds)/1000.0, float64(disableThreshold)/1000.0))
|
2024-03-10 15:41:16 +00:00
|
|
|
|
if config.AutomaticDisableChannelEnabled {
|
|
|
|
|
monitor.DisableChannel(channel.Id, channel.Name, err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
_ = message.Notify(message.ByAll, fmt.Sprintf("渠道 %s (%d)测试超时", channel.Name, channel.Id), "", err.Error())
|
|
|
|
|
}
|
2023-07-22 10:15:30 +00:00
|
|
|
|
}
|
2024-04-05 17:50:12 +00:00
|
|
|
|
if isChannelEnabled && monitor.ShouldDisableChannel(openaiErr, -1) {
|
2024-03-10 09:57:47 +00:00
|
|
|
|
monitor.DisableChannel(channel.Id, channel.Name, err.Error())
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
2024-04-05 17:50:12 +00:00
|
|
|
|
if !isChannelEnabled && monitor.ShouldEnableChannel(err, openaiErr) {
|
2024-03-10 09:57:47 +00:00
|
|
|
|
monitor.EnableChannel(channel.Id, channel.Name)
|
2023-12-03 12:10:57 +00:00
|
|
|
|
}
|
2023-05-23 02:01:09 +00:00
|
|
|
|
channel.UpdateResponseTime(milliseconds)
|
2024-01-21 15:21:42 +00:00
|
|
|
|
time.Sleep(config.RequestInterval)
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
testAllChannelsLock.Lock()
|
|
|
|
|
testAllChannelsRunning = false
|
|
|
|
|
testAllChannelsLock.Unlock()
|
2023-06-22 14:01:03 +00:00
|
|
|
|
if notify {
|
2024-03-24 15:01:03 +00:00
|
|
|
|
err := message.Notify(message.ByAll, "渠道测试完成", "", "渠道测试完成,如果没有收到禁用通知,说明所有渠道都正常")
|
2023-06-22 14:01:03 +00:00
|
|
|
|
if err != nil {
|
2024-01-21 15:21:42 +00:00
|
|
|
|
logger.SysError(fmt.Sprintf("failed to send email: %s", err.Error()))
|
2023-06-22 14:01:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 10:34:57 +00:00
|
|
|
|
func TestChannels(c *gin.Context) {
|
|
|
|
|
scope := c.Query("scope")
|
|
|
|
|
if scope == "" {
|
|
|
|
|
scope = "all"
|
|
|
|
|
}
|
|
|
|
|
err := testChannels(true, scope)
|
2023-05-23 02:01:09 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
2023-06-22 14:01:03 +00:00
|
|
|
|
|
|
|
|
|
func AutomaticallyTestChannels(frequency int) {
|
|
|
|
|
for {
|
|
|
|
|
time.Sleep(time.Duration(frequency) * time.Minute)
|
2024-01-21 15:21:42 +00:00
|
|
|
|
logger.SysLog("testing all channels")
|
2024-03-10 10:34:57 +00:00
|
|
|
|
_ = testChannels(false, "all")
|
2024-01-21 15:21:42 +00:00
|
|
|
|
logger.SysLog("channel test finished")
|
2023-06-22 14:01:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|