2023-04-22 12:39:27 +00:00
|
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
2023-05-15 02:48:52 +00:00
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2023-04-22 12:39:27 +00:00
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"net/http"
|
2023-04-22 13:14:09 +00:00
|
|
|
|
"one-api/common"
|
|
|
|
|
"one-api/model"
|
2023-04-22 12:39:27 +00:00
|
|
|
|
"strconv"
|
2023-05-13 09:08:13 +00:00
|
|
|
|
"strings"
|
2023-05-15 04:36:55 +00:00
|
|
|
|
"sync"
|
2023-05-15 02:48:52 +00:00
|
|
|
|
"time"
|
2023-04-22 12:39:27 +00:00
|
|
|
|
)
|
|
|
|
|
|
2023-04-22 13:41:16 +00:00
|
|
|
|
func GetAllChannels(c *gin.Context) {
|
2023-04-22 12:39:27 +00:00
|
|
|
|
p, _ := strconv.Atoi(c.Query("p"))
|
|
|
|
|
if p < 0 {
|
|
|
|
|
p = 0
|
|
|
|
|
}
|
2023-05-15 04:36:55 +00:00
|
|
|
|
channels, err := model.GetAllChannels(p*common.ItemsPerPage, common.ItemsPerPage, false)
|
2023-04-22 12:39:27 +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-04-22 14:02:59 +00:00
|
|
|
|
"data": channels,
|
2023-04-22 12:39:27 +00:00
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-22 13:41:16 +00:00
|
|
|
|
func SearchChannels(c *gin.Context) {
|
2023-04-22 12:39:27 +00:00
|
|
|
|
keyword := c.Query("keyword")
|
2023-04-22 14:02:59 +00:00
|
|
|
|
channels, err := model.SearchChannels(keyword)
|
2023-04-22 12:39:27 +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-04-22 14:02:59 +00:00
|
|
|
|
"data": channels,
|
2023-04-22 12:39:27 +00:00
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-22 14:02:59 +00:00
|
|
|
|
func GetChannel(c *gin.Context) {
|
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
2023-04-22 12:39:27 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-23 10:24:11 +00:00
|
|
|
|
channel, err := model.GetChannelById(id, false)
|
2023-04-22 14:02:59 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
2023-04-22 12:39:27 +00:00
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": true,
|
|
|
|
|
"message": "",
|
2023-04-22 14:02:59 +00:00
|
|
|
|
"data": channel,
|
2023-04-22 12:39:27 +00:00
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-22 14:02:59 +00:00
|
|
|
|
func AddChannel(c *gin.Context) {
|
|
|
|
|
channel := model.Channel{}
|
|
|
|
|
err := c.ShouldBindJSON(&channel)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2023-04-22 12:39:27 +00:00
|
|
|
|
"success": false,
|
2023-04-22 14:02:59 +00:00
|
|
|
|
"message": err.Error(),
|
2023-04-22 12:39:27 +00:00
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-23 07:42:23 +00:00
|
|
|
|
channel.CreatedTime = common.GetTimestamp()
|
2023-05-13 09:08:13 +00:00
|
|
|
|
keys := strings.Split(channel.Key, "\n")
|
|
|
|
|
channels := make([]model.Channel, 0)
|
|
|
|
|
for _, key := range keys {
|
|
|
|
|
if key == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
localChannel := channel
|
|
|
|
|
localChannel.Key = key
|
|
|
|
|
channels = append(channels, localChannel)
|
|
|
|
|
}
|
|
|
|
|
err = model.BatchInsertChannels(channels)
|
2023-04-22 14:02:59 +00:00
|
|
|
|
if err != nil {
|
2023-04-22 12:39:27 +00:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
2023-04-22 14:02:59 +00:00
|
|
|
|
"message": err.Error(),
|
2023-04-22 12:39:27 +00:00
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-22 14:02:59 +00:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": true,
|
|
|
|
|
"message": "",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeleteChannel(c *gin.Context) {
|
|
|
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
|
channel := model.Channel{Id: id}
|
|
|
|
|
err := channel.Delete()
|
2023-04-22 12:39:27 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2023-04-22 14:02:59 +00:00
|
|
|
|
"success": false,
|
2023-04-22 12:39:27 +00:00
|
|
|
|
"message": err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-22 14:02:59 +00:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": true,
|
|
|
|
|
"message": "",
|
|
|
|
|
})
|
|
|
|
|
return
|
2023-04-22 12:39:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-22 14:02:59 +00:00
|
|
|
|
func UpdateChannel(c *gin.Context) {
|
|
|
|
|
channel := model.Channel{}
|
|
|
|
|
err := c.ShouldBindJSON(&channel)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
})
|
2023-04-22 12:39:27 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-22 14:02:59 +00:00
|
|
|
|
err = channel.Update()
|
|
|
|
|
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-04-23 07:42:23 +00:00
|
|
|
|
"data": channel,
|
2023-04-22 14:02:59 +00:00
|
|
|
|
})
|
|
|
|
|
return
|
2023-04-22 12:39:27 +00:00
|
|
|
|
}
|
2023-05-15 02:48:52 +00:00
|
|
|
|
|
|
|
|
|
func testChannel(channel *model.Channel, request *ChatRequest) error {
|
|
|
|
|
if request.Model == "" {
|
|
|
|
|
request.Model = "gpt-3.5-turbo"
|
|
|
|
|
if channel.Type == common.ChannelTypeAzure {
|
|
|
|
|
request.Model = "gpt-35-turbo"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
requestURL := common.ChannelBaseURLs[channel.Type]
|
|
|
|
|
if channel.Type == common.ChannelTypeAzure {
|
|
|
|
|
requestURL = fmt.Sprintf("%s/openai/deployments/%s/chat/completions?api-version=2023-03-15-preview", channel.BaseURL, request.Model)
|
|
|
|
|
} else {
|
|
|
|
|
if channel.Type == common.ChannelTypeCustom {
|
|
|
|
|
requestURL = channel.BaseURL
|
|
|
|
|
}
|
|
|
|
|
requestURL += "/v1/chat/completions"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonData, err := json.Marshal(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
req, err := http.NewRequest("POST", requestURL, bytes.NewBuffer(jsonData))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
var response TextResponse
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if response.Error.Type != "" {
|
|
|
|
|
return errors.New(fmt.Sprintf("type %s, code %s, message %s", response.Error.Type, response.Error.Code, response.Error.Message))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 04:36:55 +00:00
|
|
|
|
func buildTestRequest(c *gin.Context) *ChatRequest {
|
|
|
|
|
model_ := c.Query("model")
|
|
|
|
|
testRequest := &ChatRequest{
|
2023-05-16 08:22:25 +00:00
|
|
|
|
Model: model_,
|
|
|
|
|
MaxTokens: 1,
|
2023-05-15 04:36:55 +00:00
|
|
|
|
}
|
|
|
|
|
testMessage := Message{
|
|
|
|
|
Role: "user",
|
2023-05-16 08:22:25 +00:00
|
|
|
|
Content: "hi",
|
2023-05-15 04:36:55 +00:00
|
|
|
|
}
|
|
|
|
|
testRequest.Messages = append(testRequest.Messages, testMessage)
|
|
|
|
|
return testRequest
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 02:48:52 +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
|
|
|
|
|
}
|
2023-05-15 04:36:55 +00:00
|
|
|
|
testRequest := buildTestRequest(c)
|
2023-05-15 02:48:52 +00:00
|
|
|
|
tik := time.Now()
|
2023-05-15 04:36:55 +00:00
|
|
|
|
err = testChannel(channel, testRequest)
|
2023-05-15 02:48:52 +00:00
|
|
|
|
tok := time.Now()
|
2023-05-15 03:35:38 +00:00
|
|
|
|
milliseconds := tok.Sub(tik).Milliseconds()
|
|
|
|
|
go channel.UpdateResponseTime(milliseconds)
|
|
|
|
|
consumedTime := float64(milliseconds) / 1000.0
|
2023-05-15 02:48:52 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
2023-05-15 04:36:55 +00:00
|
|
|
|
|
|
|
|
|
var testAllChannelsLock sync.Mutex
|
2023-05-15 08:19:39 +00:00
|
|
|
|
var testAllChannelsRunning bool = false
|
2023-05-15 04:36:55 +00:00
|
|
|
|
|
2023-05-15 09:34:09 +00:00
|
|
|
|
// disable & notify
|
2023-05-18 03:11:15 +00:00
|
|
|
|
func disableChannel(channelId int, channelName string, reason string) {
|
2023-05-15 09:34:09 +00:00
|
|
|
|
if common.RootUserEmail == "" {
|
|
|
|
|
common.RootUserEmail = model.GetRootUserEmail()
|
|
|
|
|
}
|
|
|
|
|
model.UpdateChannelStatusById(channelId, common.ChannelStatusDisabled)
|
|
|
|
|
subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
|
2023-05-18 03:11:15 +00:00
|
|
|
|
content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
|
|
|
|
|
err := common.SendEmail(subject, common.RootUserEmail, content)
|
2023-05-15 09:34:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
common.SysError(fmt.Sprintf("发送邮件失败:%s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 04:36:55 +00:00
|
|
|
|
func testAllChannels(c *gin.Context) error {
|
2023-05-15 08:19:39 +00:00
|
|
|
|
testAllChannelsLock.Lock()
|
|
|
|
|
if testAllChannelsRunning {
|
|
|
|
|
testAllChannelsLock.Unlock()
|
|
|
|
|
return errors.New("测试已在运行中")
|
2023-05-15 04:36:55 +00:00
|
|
|
|
}
|
2023-05-15 08:19:39 +00:00
|
|
|
|
testAllChannelsRunning = true
|
|
|
|
|
testAllChannelsLock.Unlock()
|
2023-05-15 04:36:55 +00:00
|
|
|
|
channels, err := model.GetAllChannels(0, 0, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
testRequest := buildTestRequest(c)
|
2023-05-15 09:34:09 +00:00
|
|
|
|
var disableThreshold = int64(common.ChannelDisableThreshold * 1000)
|
|
|
|
|
if disableThreshold == 0 {
|
|
|
|
|
disableThreshold = 10000000 // a impossible value
|
|
|
|
|
}
|
2023-05-15 04:36:55 +00:00
|
|
|
|
go func() {
|
|
|
|
|
for _, channel := range channels {
|
|
|
|
|
if channel.Status != common.ChannelStatusEnabled {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
tik := time.Now()
|
|
|
|
|
err := testChannel(channel, testRequest)
|
|
|
|
|
tok := time.Now()
|
|
|
|
|
milliseconds := tok.Sub(tik).Milliseconds()
|
|
|
|
|
if err != nil || milliseconds > disableThreshold {
|
|
|
|
|
if milliseconds > disableThreshold {
|
|
|
|
|
err = errors.New(fmt.Sprintf("响应时间 %.2fs 超过阈值 %.2fs", float64(milliseconds)/1000.0, float64(disableThreshold)/1000.0))
|
|
|
|
|
}
|
2023-05-18 03:11:15 +00:00
|
|
|
|
disableChannel(channel.Id, channel.Name, err.Error())
|
2023-05-15 04:36:55 +00:00
|
|
|
|
}
|
|
|
|
|
channel.UpdateResponseTime(milliseconds)
|
|
|
|
|
}
|
2023-05-15 09:34:09 +00:00
|
|
|
|
err := common.SendEmail("通道测试完成", common.RootUserEmail, "通道测试完成,如果没有收到禁用通知,说明所有通道都正常")
|
2023-05-15 04:36:55 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
common.SysError(fmt.Sprintf("发送邮件失败:%s", err.Error()))
|
|
|
|
|
}
|
2023-05-15 08:19:39 +00:00
|
|
|
|
testAllChannelsLock.Lock()
|
|
|
|
|
testAllChannelsRunning = false
|
|
|
|
|
testAllChannelsLock.Unlock()
|
2023-05-15 04:36:55 +00:00
|
|
|
|
}()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAllChannels(c *gin.Context) {
|
|
|
|
|
err := testAllChannels(c)
|
|
|
|
|
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
|
|
|
|
|
}
|