2023-05-23 10:01:09 +08:00
|
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"one-api/common"
|
|
|
|
|
|
"one-api/model"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
"time"
|
2023-10-22 17:50:52 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-05-23 10:01:09 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-09-03 21:46:07 +08:00
|
|
|
|
func testChannel(channel *model.Channel, request ChatRequest) (err error, openaiErr *OpenAIError) {
|
2023-06-18 00:20:06 +08:00
|
|
|
|
switch channel.Type {
|
2023-07-23 23:23:56 +08:00
|
|
|
|
case common.ChannelTypePaLM:
|
|
|
|
|
|
fallthrough
|
|
|
|
|
|
case common.ChannelTypeAnthropic:
|
|
|
|
|
|
fallthrough
|
|
|
|
|
|
case common.ChannelTypeBaidu:
|
|
|
|
|
|
fallthrough
|
|
|
|
|
|
case common.ChannelTypeZhipu:
|
2023-07-29 23:54:09 +08:00
|
|
|
|
fallthrough
|
2023-08-26 13:30:21 +08:00
|
|
|
|
case common.ChannelTypeAli:
|
|
|
|
|
|
fallthrough
|
|
|
|
|
|
case common.ChannelType360:
|
|
|
|
|
|
fallthrough
|
2023-12-18 23:45:08 +08:00
|
|
|
|
case common.ChannelTypeGemini:
|
|
|
|
|
|
fallthrough
|
2023-07-29 23:54:09 +08:00
|
|
|
|
case common.ChannelTypeXunfei:
|
2023-07-23 23:23:56 +08:00
|
|
|
|
return errors.New("该渠道类型当前版本不支持测试,请手动测试"), nil
|
2023-06-18 00:20:06 +08:00
|
|
|
|
case common.ChannelTypeAzure:
|
2024-02-01 18:31:42 +08:00
|
|
|
|
if request.Model == "" {
|
|
|
|
|
|
request.Model = "gpt-35-turbo"
|
|
|
|
|
|
}
|
2023-09-03 21:46:07 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
err = errors.New("请确保已在 Azure 上创建了 gpt-35-turbo 模型,并且 apiVersion 已正确填写!")
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
2023-06-18 00:20:06 +08:00
|
|
|
|
default:
|
2024-02-01 18:31:42 +08:00
|
|
|
|
if request.Model == "" {
|
|
|
|
|
|
request.Model = "gpt-3.5-turbo"
|
|
|
|
|
|
}
|
2023-05-23 10:01:09 +08:00
|
|
|
|
}
|
2024-02-01 18:11:00 +08:00
|
|
|
|
requestURL := getFullRequestURL(channel.GetBaseURL(), "/v1/chat/completions", channel.Type)
|
|
|
|
|
|
|
2023-05-23 10:01:09 +08:00
|
|
|
|
if channel.Type == common.ChannelTypeAzure {
|
2024-02-01 17:48:55 +08:00
|
|
|
|
requestURL = getFullRequestURL(channel.GetBaseURL(), fmt.Sprintf("/openai/deployments/%s/chat/completions?api-version=2023-03-15-preview", request.Model), channel.Type)
|
2023-05-23 10:01:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
jsonData, err := json.Marshal(request)
|
|
|
|
|
|
if err != nil {
|
2023-07-22 18:15:30 +08:00
|
|
|
|
return err, nil
|
2023-05-23 10:01:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
req, err := http.NewRequest("POST", requestURL, bytes.NewBuffer(jsonData))
|
|
|
|
|
|
if err != nil {
|
2023-07-22 18:15:30 +08:00
|
|
|
|
return err, nil
|
2023-05-23 10:01:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
if channel.Type == common.ChannelTypeAzure {
|
|
|
|
|
|
req.Header.Set("api-key", channel.Key)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
req.Header.Set("Authorization", "Bearer "+channel.Key)
|
|
|
|
|
|
}
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2023-07-23 15:18:58 +08:00
|
|
|
|
resp, err := httpClient.Do(req)
|
2023-05-23 10:01:09 +08:00
|
|
|
|
if err != nil {
|
2023-07-22 18:15:30 +08:00
|
|
|
|
return err, nil
|
2023-05-23 10:01:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
var response TextResponse
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
|
|
|
|
if err != nil {
|
2023-07-22 18:15:30 +08:00
|
|
|
|
return err, nil
|
2023-05-23 10:01:09 +08:00
|
|
|
|
}
|
2023-06-08 14:09:39 +08:00
|
|
|
|
if response.Usage.CompletionTokens == 0 {
|
2023-07-22 18:15:30 +08:00
|
|
|
|
return errors.New(fmt.Sprintf("type %s, code %v, message %s", response.Error.Type, response.Error.Code, response.Error.Message)), &response.Error
|
2023-05-23 10:01:09 +08:00
|
|
|
|
}
|
2023-07-22 18:15:30 +08:00
|
|
|
|
return nil, nil
|
2023-05-23 10:01:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-22 22:01:03 +08:00
|
|
|
|
func buildTestRequest() *ChatRequest {
|
2023-05-23 10:01:09 +08:00
|
|
|
|
testRequest := &ChatRequest{
|
2023-06-22 22:01:03 +08:00
|
|
|
|
Model: "", // this will be set later
|
2023-05-23 10:01:09 +08:00
|
|
|
|
MaxTokens: 1,
|
|
|
|
|
|
}
|
2023-11-17 18:24:37 +08:00
|
|
|
|
content, _ := json.Marshal("hi")
|
2023-05-23 10:01:09 +08:00
|
|
|
|
testMessage := Message{
|
|
|
|
|
|
Role: "user",
|
2023-11-17 18:24:37 +08:00
|
|
|
|
Content: content,
|
2023-05-23 10:01:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
testRequest.Messages = append(testRequest.Messages, testMessage)
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
2024-02-01 18:31:42 +08:00
|
|
|
|
testModel := c.Param("model")
|
2023-05-23 10:01:09 +08:00
|
|
|
|
channel, err := model.GetChannelById(id, true)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-06-22 22:01:03 +08:00
|
|
|
|
testRequest := buildTestRequest()
|
2024-02-01 18:31:42 +08:00
|
|
|
|
if testModel != "" {
|
|
|
|
|
|
testRequest.Model = testModel
|
|
|
|
|
|
}
|
2023-05-23 10:01:09 +08:00
|
|
|
|
tik := time.Now()
|
2023-07-22 18:15:30 +08:00
|
|
|
|
err, _ = testChannel(channel, *testRequest)
|
2023-05-23 10:01:09 +08: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
|
|
|
|
|
|
|
|
|
|
|
|
// disable & notify
|
|
|
|
|
|
func disableChannel(channelId int, channelName string, reason string) {
|
|
|
|
|
|
if common.RootUserEmail == "" {
|
|
|
|
|
|
common.RootUserEmail = model.GetRootUserEmail()
|
|
|
|
|
|
}
|
2023-10-02 13:06:27 +08:00
|
|
|
|
model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled)
|
2023-05-23 10:01:09 +08:00
|
|
|
|
subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
|
|
|
|
|
|
content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
|
|
|
|
|
|
err := common.SendEmail(subject, common.RootUserEmail, content)
|
|
|
|
|
|
if err != nil {
|
2023-06-22 10:59:01 +08:00
|
|
|
|
common.SysError(fmt.Sprintf("failed to send email: %s", err.Error()))
|
2023-05-23 10:01:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-22 22:01:03 +08:00
|
|
|
|
func testAllChannels(notify bool) error {
|
2023-05-31 15:24:40 +08:00
|
|
|
|
if common.RootUserEmail == "" {
|
|
|
|
|
|
common.RootUserEmail = model.GetRootUserEmail()
|
|
|
|
|
|
}
|
2023-05-23 10:01:09 +08:00
|
|
|
|
testAllChannelsLock.Lock()
|
|
|
|
|
|
if testAllChannelsRunning {
|
|
|
|
|
|
testAllChannelsLock.Unlock()
|
|
|
|
|
|
return errors.New("测试已在运行中")
|
|
|
|
|
|
}
|
|
|
|
|
|
testAllChannelsRunning = true
|
|
|
|
|
|
testAllChannelsLock.Unlock()
|
2023-12-05 18:15:40 +08:00
|
|
|
|
channels, err := model.GetAllChannels(0, 0, true, false)
|
2023-05-23 10:01:09 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2023-06-22 22:01:03 +08:00
|
|
|
|
testRequest := buildTestRequest()
|
2023-05-23 10:01:09 +08:00
|
|
|
|
var disableThreshold = int64(common.ChannelDisableThreshold * 1000)
|
|
|
|
|
|
if disableThreshold == 0 {
|
|
|
|
|
|
disableThreshold = 10000000 // a impossible value
|
|
|
|
|
|
}
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
for _, channel := range channels {
|
|
|
|
|
|
if channel.Status != common.ChannelStatusEnabled {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
tik := time.Now()
|
2023-07-22 18:15:30 +08:00
|
|
|
|
err, openaiErr := testChannel(channel, *testRequest)
|
2023-05-23 10:01:09 +08:00
|
|
|
|
tok := time.Now()
|
|
|
|
|
|
milliseconds := tok.Sub(tik).Milliseconds()
|
2023-11-17 16:22:13 +08:00
|
|
|
|
|
|
|
|
|
|
ban := false
|
2023-07-22 18:15:30 +08:00
|
|
|
|
if milliseconds > disableThreshold {
|
|
|
|
|
|
err = errors.New(fmt.Sprintf("响应时间 %.2fs 超过阈值 %.2fs", float64(milliseconds)/1000.0, float64(disableThreshold)/1000.0))
|
2023-11-17 16:22:13 +08:00
|
|
|
|
ban = true
|
2023-07-22 18:15:30 +08:00
|
|
|
|
}
|
2023-11-17 18:24:37 +08:00
|
|
|
|
if openaiErr != nil {
|
|
|
|
|
|
err = errors.New(fmt.Sprintf("type %s, code %v, message %s", openaiErr.Type, openaiErr.Code, openaiErr.Message))
|
|
|
|
|
|
ban = true
|
|
|
|
|
|
}
|
2023-09-25 18:44:10 +08:00
|
|
|
|
// parse *int to bool
|
|
|
|
|
|
if channel.AutoBan != nil && *channel.AutoBan == 0 {
|
|
|
|
|
|
ban = false
|
|
|
|
|
|
}
|
|
|
|
|
|
if shouldDisableChannel(openaiErr, -1) && ban {
|
2023-05-23 10:01:09 +08:00
|
|
|
|
disableChannel(channel.Id, channel.Name, err.Error())
|
|
|
|
|
|
}
|
|
|
|
|
|
channel.UpdateResponseTime(milliseconds)
|
2023-06-22 22:01:03 +08:00
|
|
|
|
time.Sleep(common.RequestInterval)
|
2023-05-23 10:01:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
testAllChannelsLock.Lock()
|
|
|
|
|
|
testAllChannelsRunning = false
|
|
|
|
|
|
testAllChannelsLock.Unlock()
|
2023-06-22 22:01:03 +08: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 10:01:09 +08:00
|
|
|
|
}()
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestAllChannels(c *gin.Context) {
|
2023-06-22 22:01:03 +08:00
|
|
|
|
err := testAllChannels(true)
|
2023-05-23 10:01:09 +08: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 22:01:03 +08: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")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|