2023-05-23 02:01:09 +00:00
|
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
2023-11-28 10:32:26 +00:00
|
|
|
|
"net/http/httptest"
|
2023-05-23 02:01:09 +00:00
|
|
|
|
"one-api/common"
|
|
|
|
|
"one-api/model"
|
2023-11-29 08:13:33 +00:00
|
|
|
|
"one-api/providers"
|
|
|
|
|
providers_base "one-api/providers/base"
|
2023-11-28 10:32:26 +00:00
|
|
|
|
"one-api/types"
|
2023-05-23 02:01:09 +00:00
|
|
|
|
"strconv"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
2023-11-05 15:46:59 +00:00
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-05-23 02:01:09 +00:00
|
|
|
|
)
|
|
|
|
|
|
2023-11-28 10:32:26 +00:00
|
|
|
|
func testChannel(channel *model.Channel, request types.ChatCompletionRequest) (err error, openaiErr *types.OpenAIError) {
|
2023-12-29 08:23:25 +00:00
|
|
|
|
if channel.TestModel == "" {
|
|
|
|
|
return errors.New("请填写测速模型后再试"), nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-28 10:32:26 +00:00
|
|
|
|
// 创建一个 http.Request
|
|
|
|
|
req, err := http.NewRequest("POST", "/v1/chat/completions", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err, nil
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
c, _ := gin.CreateTestContext(w)
|
|
|
|
|
c.Request = req
|
2023-12-29 08:23:25 +00:00
|
|
|
|
request.Model = channel.TestModel
|
2023-05-23 02:01:09 +00:00
|
|
|
|
|
2023-12-26 08:40:50 +00:00
|
|
|
|
provider := providers.GetProvider(channel, c)
|
2023-11-29 08:13:33 +00:00
|
|
|
|
if provider == nil {
|
|
|
|
|
return errors.New("channel not implemented"), nil
|
|
|
|
|
}
|
|
|
|
|
chatProvider, ok := provider.(providers_base.ChatInterface)
|
|
|
|
|
if !ok {
|
|
|
|
|
return errors.New("channel not implemented"), nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:29:30 +00:00
|
|
|
|
modelMap, err := parseModelMapping(channel.GetModelMapping())
|
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
|
|
|
|
}
|
2023-11-28 10:32:26 +00:00
|
|
|
|
if modelMap != nil && modelMap[request.Model] != "" {
|
|
|
|
|
request.Model = modelMap[request.Model]
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
2023-11-28 10:32:26 +00:00
|
|
|
|
|
|
|
|
|
promptTokens := common.CountTokenMessages(request.Messages, request.Model)
|
2023-12-21 07:36:01 +00:00
|
|
|
|
Usage, openAIErrorWithStatusCode := chatProvider.ChatAction(&request, true, promptTokens)
|
2023-11-28 10:32:26 +00:00
|
|
|
|
if openAIErrorWithStatusCode != nil {
|
2023-12-29 08:23:25 +00:00
|
|
|
|
return errors.New(openAIErrorWithStatusCode.Message), &openAIErrorWithStatusCode.OpenAIError
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
2023-11-28 10:32:26 +00:00
|
|
|
|
|
2023-12-21 07:36:01 +00:00
|
|
|
|
if Usage.CompletionTokens == 0 {
|
2023-12-26 08:40:50 +00:00
|
|
|
|
return fmt.Errorf("channel %s, message 补全 tokens 非预期返回 0", channel.Name), nil
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
2023-12-21 07:36:01 +00:00
|
|
|
|
|
2023-12-29 08:23:25 +00:00
|
|
|
|
common.SysLog(fmt.Sprintf("测试模型 %s 返回内容为:%s", channel.Name, w.Body.String()))
|
|
|
|
|
|
2023-07-22 10:15:30 +00:00
|
|
|
|
return nil, nil
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-28 10:32:26 +00:00
|
|
|
|
func buildTestRequest() *types.ChatCompletionRequest {
|
|
|
|
|
testRequest := &types.ChatCompletionRequest{
|
|
|
|
|
Messages: []types.ChatCompletionMessage{
|
|
|
|
|
{
|
|
|
|
|
Role: "user",
|
|
|
|
|
Content: "You just need to output 'hi' next.",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Model: "",
|
2023-05-23 02:01:09 +00:00
|
|
|
|
MaxTokens: 1,
|
2023-11-28 10:32:26 +00:00
|
|
|
|
Stream: false,
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
return testRequest
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2023-06-22 14:01:03 +00:00
|
|
|
|
testRequest := buildTestRequest()
|
2023-05-23 02:01:09 +00:00
|
|
|
|
tik := time.Now()
|
2023-07-22 10:15:30 +00:00
|
|
|
|
err, _ = testChannel(channel, *testRequest)
|
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,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var testAllChannelsLock sync.Mutex
|
|
|
|
|
var testAllChannelsRunning bool = false
|
|
|
|
|
|
2023-12-03 12:10:57 +00:00
|
|
|
|
func notifyRootUser(subject string, content string) {
|
2023-05-23 02:01:09 +00:00
|
|
|
|
if common.RootUserEmail == "" {
|
|
|
|
|
common.RootUserEmail = model.GetRootUserEmail()
|
|
|
|
|
}
|
|
|
|
|
err := common.SendEmail(subject, common.RootUserEmail, content)
|
|
|
|
|
if err != nil {
|
2023-06-22 02:59:01 +00:00
|
|
|
|
common.SysError(fmt.Sprintf("failed to send email: %s", err.Error()))
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-03 12:10:57 +00:00
|
|
|
|
// disable & notify
|
|
|
|
|
func disableChannel(channelId int, channelName string, reason string) {
|
|
|
|
|
model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled)
|
|
|
|
|
subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
|
|
|
|
|
content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
|
|
|
|
|
notifyRootUser(subject, content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// enable & notify
|
|
|
|
|
func enableChannel(channelId int, channelName string) {
|
|
|
|
|
model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled)
|
|
|
|
|
subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
|
|
|
|
content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
|
|
|
|
notifyRootUser(subject, content)
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 14:01:03 +00:00
|
|
|
|
func testAllChannels(notify bool) error {
|
2023-05-31 07:24:40 +00:00
|
|
|
|
if common.RootUserEmail == "" {
|
|
|
|
|
common.RootUserEmail = model.GetRootUserEmail()
|
|
|
|
|
}
|
2023-05-23 02:01:09 +00:00
|
|
|
|
testAllChannelsLock.Lock()
|
|
|
|
|
if testAllChannelsRunning {
|
|
|
|
|
testAllChannelsLock.Unlock()
|
|
|
|
|
return errors.New("测试已在运行中")
|
|
|
|
|
}
|
|
|
|
|
testAllChannelsRunning = true
|
|
|
|
|
testAllChannelsLock.Unlock()
|
|
|
|
|
channels, err := model.GetAllChannels(0, 0, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-06-22 14:01:03 +00:00
|
|
|
|
testRequest := buildTestRequest()
|
2023-05-23 02:01:09 +00:00
|
|
|
|
var disableThreshold = int64(common.ChannelDisableThreshold * 1000)
|
|
|
|
|
if disableThreshold == 0 {
|
|
|
|
|
disableThreshold = 10000000 // a impossible value
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
for _, channel := range channels {
|
2023-12-03 12:10:57 +00:00
|
|
|
|
isChannelEnabled := channel.Status == common.ChannelStatusEnabled
|
2023-05-23 02:01:09 +00:00
|
|
|
|
tik := time.Now()
|
2023-07-22 10:15:30 +00:00
|
|
|
|
err, openaiErr := testChannel(channel, *testRequest)
|
2023-05-23 02:01:09 +00:00
|
|
|
|
tok := time.Now()
|
|
|
|
|
milliseconds := tok.Sub(tik).Milliseconds()
|
2023-07-22 10:15:30 +00:00
|
|
|
|
if milliseconds > disableThreshold {
|
2023-11-29 08:13:33 +00:00
|
|
|
|
err = fmt.Errorf("响应时间 %.2fs 超过阈值 %.2fs", float64(milliseconds)/1000.0, float64(disableThreshold)/1000.0)
|
2023-07-22 10:15:30 +00:00
|
|
|
|
disableChannel(channel.Id, channel.Name, err.Error())
|
|
|
|
|
}
|
2023-12-03 12:10:57 +00:00
|
|
|
|
if isChannelEnabled && shouldDisableChannel(openaiErr, -1) {
|
2023-05-23 02:01:09 +00:00
|
|
|
|
disableChannel(channel.Id, channel.Name, err.Error())
|
|
|
|
|
}
|
2023-12-03 12:10:57 +00:00
|
|
|
|
if !isChannelEnabled && shouldEnableChannel(err, openaiErr) {
|
|
|
|
|
enableChannel(channel.Id, channel.Name)
|
|
|
|
|
}
|
2023-05-23 02:01:09 +00:00
|
|
|
|
channel.UpdateResponseTime(milliseconds)
|
2023-06-22 14:01:03 +00:00
|
|
|
|
time.Sleep(common.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 {
|
|
|
|
|
err := common.SendEmail("通道测试完成", common.RootUserEmail, "通道测试完成,如果没有收到禁用通知,说明所有通道都正常")
|
|
|
|
|
if err != nil {
|
|
|
|
|
common.SysError(fmt.Sprintf("failed to send email: %s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-23 02:01:09 +00:00
|
|
|
|
}()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAllChannels(c *gin.Context) {
|
2023-06-22 14:01:03 +00:00
|
|
|
|
err := testAllChannels(true)
|
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": "",
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-06-22 14:01:03 +00:00
|
|
|
|
|
|
|
|
|
func AutomaticallyTestChannels(frequency int) {
|
|
|
|
|
for {
|
|
|
|
|
time.Sleep(time.Duration(frequency) * time.Minute)
|
|
|
|
|
common.SysLog("testing all channels")
|
|
|
|
|
_ = testAllChannels(false)
|
|
|
|
|
common.SysLog("channel test finished")
|
|
|
|
|
}
|
|
|
|
|
}
|